Project: Micro:bit as a Remote controller
Sender
from microbit import *
import radio
chnl = 10
radio.config(channel=chnl)
radio.on()
while True:
y = accelerometer.get_y()
a = button_a.is_pressed()
b = button_b.is_pressed()
if a and y<-300:
# forwards left
display.show(Image.ARROW_NW)
radio.send("NW")
elif a and y>300:
# backwards left
display.show(Image.ARROW_SW)
radio.send("SW")
elif b and y<-300:
# forwards right
display.show(Image.ARROW_NE)
radio.send("NE")
elif b and y>300:
# backwards right
display.show(Image.ARROW_SE)
radio.send("SE")
elif y>300:
#backwards
display.show(Image.ARROW_S)
radio.send("S")
elif y<-300:
# forwards
display.show(Image.ARROW_N)
radio.send("N")
sleep(20)
Receiver
from microbit import *
import radio
chnl = 10
radio.config(channel=chnl)
radio.on()
def Drive(lft,rgt):
pin8.write_digital(0)
pin12.write_digital(0)
if lft<0:
pin8.write_digital(1)
lft = 1023 + lft
if rgt<0:
rgt = 1023 + rgt
pin12.write_digital(1)
pin0.write_analog(lft)
pin1.write_analog(rgt)
while True:
s = radio.receive()
if s is not None:
if s=="N":
Drive(800,800)
elif s=="S":
Drive(-800,-800)
elif s=="NE":
Drive(800,200)
elif s=="NW":
Drive(200,800)
elif s=="SE":
Drive(-800,-200)
elif s== "SW":
Drive(-200,-800)
else:
Drive(0,0)
sleep(20)