Bitkit - Remote Control
Robot cars come alive when you are able to control them remotely. It can be a simple project with a micro:bit, just making sure that you can move forwards, backwards, left and right or it can be a complicated affair.
This page outlines the basic principles for remote control, repeating and re-explaining code used on several previous cars. In this case, I am using the 4tronix Bit:Commander as my remote but the code can be adjusted for other circuits involving buttons and joysticks.
The first part of the process is to set up the device that is sending signals. I find that compass points are quite useful. Sending a short string is reasonably reliable for the radio module and makes code fairly easy to follow.
Whatever your circuit is for your remote, you need to be able to determine forward, backward, left and right. You can do that with 4 buttons or, as is done here with 2 buttons and a joystick.
from microbit import * import radio chnl = 10 radio.config(channel=chnl) radio.on() while True: a = pin12.read_digital() b = pin14.read_digital() dx = pin1.read_analog() if a and dx<150: # forwards left display.show(Image.ARROW_NW) radio.send("NW") elif a and dx>850: # forwards right display.show(Image.ARROW_NE) radio.send("NE") elif b and dx<150: # backwards left display.show(Image.ARROW_SW) radio.send("SW") elif b and dx>850: # backwards right display.show(Image.ARROW_SE) radio.send("SE") elif b: #backwards display.show(Image.ARROW_S) radio.send("S") elif a: # forwards display.show(Image.ARROW_N) radio.send("N") sleep(20)
The program here is reasonably simple to follow. The variables a and b are used to store the states of two of the buttons on the Bit:commander. The variable dx stores the value of the x axis on a joystick.
The receiving code, for the micro:bit with the car, is just a question of deciding what to do for each signal.
from microbit import * import radio chnl = 10 radio.config(channel=chnl) radio.on() def Drive(lft,rgt): ld, rd = 0, 0 if lft<0: ld = 0xff lft += 255 if rgt<0: rd = 0xff rgt += 255 i2c.write(0x28,bytes([1,lft,ld,rgt,rd])) while True: s = radio.receive() if s is not None: if s=="N": Drive(255,255) elif s=="S": Drive(-255,-255) elif s=="NE": Drive(255,63) elif s=="NW": Drive(63,255) elif s=="SE": Drive(-255,-63) elif s== "SW": Drive(-63,-255) else: Drive(0,0) sleep(20)
If you are lucky enough to have a Bit:Commander, then you could make use of the other inputs that are available to gain further control of the car. I like to be able to control the buzzer and lights too. If you don't have the Bit:Commander, you could explore some of the other options outlined below,
Just A micro:bit
You can get reasonable control of a car with just a micro:bit. I tend to use the accelerometer to determine forwards or backwards and the buttons to move left and right.
You can use a SNES Controller with the micro:bit. That gives you 12 inputs.
If you want to breadboard a remote control, you could go simple with just enough buttons to get things going or be a little more adventurous. Port expanders are pretty cheap and would allow a truckload of inputs that could allow you to have some pre-programmed moves. There are pages on the PCF8574 and the MCP23017, neither of which cost a great deal to purchase. You can also use an input shift register to achieve a similar effect.
One nice thing to consider is using less conventional inputs. For example, you might try out some slide potentiometers, some capacitive touch inputs or something even stranger.
If you have an old Wii Nunchuck controller, you can get a breakout for a couple of pounds and can use one of them as a controller. The Wii Nunchuck is quite a cool way to control the car because you only use one hand for it.
I got a wireless PS3 Controller working with the micro:bit. There are so many inputs on this one that make it an absolute luxury item for car control. It's not the cheapest setup but does give a lot of options.