This tutorial aims to control the movement of the Maqueen by the computer via bluetooth.
The directional keys on the keyboard allow the Maqueen to move forward, backward, left, and right.
Thanks to the LED matrix of the micro: bit card, the direction taken by the Maqueen will be displayed.
We will need a micro: bit card, a Maqueen robot, an HC-06 bluetooth module and a computer equipped with bluetooth.
il faut connecter :
the VCC pin of the HC-06 module has an energy of 5V
the GND pin of the HC-06 module to GND of the Micro: bit card
the RX pin of the HC-06 module to the P0 pin of the Micro: bit card
the TR pin of the HC-06 module to the P1 pin of the Micro: bit card
Here is the makecode program which allows you to receive data from the HC-06 bluetooth module and control the Maqueen robot.
Here is the python program which allows you to connect the computer and send data to the HC-06 bluetooth module:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import serial import time from pynput import keyboard evenement="" print("Start") port="COM20" #This will be different for various devices and on windows it will probably be a COM port. bluetooth=serial.Serial(port, 9600)#Start communications with the bluetooth unit print("Connected") bluetooth.flushInput() #This gives the bluetooth a little kick result = str(0)+'\n' def on_key_release(key): global result #print('released %s' % key) #result ='released %s' % key+'\n' if result != str(0)+'\n': #or result == str(2)+'\n' or result == str(3)+'\n' or result == str(4)+'\n': result = str(0)+'\n' print(result) result_bytes = result.encode('utf_8') bluetooth.write(result_bytes) def on_key_pressed(key): global result print('pressed %s' % key) result1 ='%s' % key if result == str(0)+'\n': if result1=='Key.up' : result = str(1)+'\n' print(result) if result1=='Key.down' : result = str(2)+'\n' print(result) if result1=='Key.left' : result = str(3)+'\n' print(result) if result1=='Key.right' : result = str(4)+'\n' print(result) result_bytes = result.encode('utf_8') bluetooth.write(result_bytes) with keyboard.Listener(on_release = on_key_release,on_press=on_key_pressed) as listener: listener.join() |