Step 8: Set the BaW-Bot Rolling

Picture of Set the BaW-Bot Rolling
Now that we’ve finished with all the sensors, let’s get the BaW-Bot moving.  All you need to do now is to upload the sketch below and power the Bot up.

The logic in brief:
As the Bot approaches an obstacle, it slows down and pings the ultrasound more frequently.
When the Bot comes within 10cm (approx. 4 inches) of an obstacle it reverses and turns left.
When the left lever-switch activates it brakes, then reverses and turns right.
When the right lever-switch activates it brakes, then reverses and turns left.

Some comments on the sketch:
All reverse and turn manoeuvres are based on time, not distance – so depending on your motor speed and wheel diameter you may need to alter these.  I’m using slow motors and small wheels, so everything takes longer.

The ultrasound sensor doesn’t always give accurate readings, so I’ve used a simple algorithm I borrowed sometime in the past to calculate an average of 5 values – this smoothes the Bot’s behaviour to an extent.

The obstacle-avoidance logic is far from perfect – it’s meant as a basic platform to get the BaW-Bot running and is there to for you to build on – please share any clever algorithms you come up with!

The sketch is close to utilising the maximum available memory – if you add a whole bunch of variables and Serial.print commands for debugging, it will start overflowing and likely hang at odd moments.  There are ways around this – but that’s for another instructable.

The lever microswitches are connected to the Arduino pins that are interrupt-enabled.  You could expand the sketch by using interrupts to drive the behaviour when the lever-switches are activated.

Thanks for building this with me - please let me have any comments

Coming up next - blinging the BaW-Bot up a little.