Thursday, August 15, 2019

Trackbot (part 3: arduino integration and control code)

So with the microbit sending HIGH to some jumpers, and an arduino setup to control the lego motors, time came to integrate them together.  I brought over four wires from the microbit; forward and reverse for each track.

My plan there was to just connect all four jumpers from the microbit to the analog input pins on the arduino.  This turned out to be trivially easy, in the code i just run analogread() for each pin and if the input is above a threshold, assume it's a signal and do something.

Connecting microbit to Arduino analog input pins.
We're then getting pretty close to having it working.  It took a few iterations in the arduino code before i settled on the following, one for each track:

const int pwmOutput = 255;  
const int moveThreshold = 125; // how much of a signal before we trigger  
// left  
if (analogRead(leftFord) > moveThreshold){  
} else if (analogRead(leftBack) > moveThreshold){  
} else stopLeft();  

This was probably the most time consuming element in the project.  I wanted the vehicle to only move if you were holding the button down, otherwise stop that motor.  I'd already tested and the microbit will happily accept multiple simultaneous commands, so we're all good.

So with the wires etc all bundled on top I drove around a bit and scared the cat :) job done, it's now just a matter of packaging it all up so that it's neat and tidy and kid proof.  I should also add some fun factor in, and with four more potential inputs on the controller i could add some sounds or lights or a gatling gun.. whatever is the most enjoyable.. ;)

No comments: