Note: Requires 2 Microbits SENDER CODE: from microbit import * import radio chnl = 10 radio.config(channel=chnl) radio.on() while True: y = accelerometer.get_y() x = accelerometer.get_x() a = button_a.is_pressed() b = button_b.is_pressed() if (a or x<-300) and y<-300: # forwards left display.show(Image.ARROW_NW) radio.send("NW") elif (a or x<-300) and y>300: # backwards left display.show(Image.ARROW_SW) radio.send("SW") elif (b or x>300) and y<-300: # forwards right display.show(Image.ARROW_NE) radio.send("NE") elif (b or x>300) 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 CODE: (inside the BitBot) from microbit import * import radio chnl = 10 radio.config(channel=chnl) radio.on() def Drive(lft,rgt): pin8.write_digital(0) ## Go forward pin12.write_digital(0) ## Go forward if lft<0: pin8.write_digital(1) ## if power < 0 then go backward lft = 1023 + lft ## Power has to be 1023 - leftpower 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) |
BBC Micro:bit MicroPython > BitBot Driving >
