Robot Project: Components | Chassis Assembly | Electronics | Wiring | Code

Writing Code For Your Robot

I have kept the code driving the robot fairly simple at this stage, it moves forward and if it senses an obstacle it turns randomly to the left or right.

The ultrasonic sensor is good at recognising large objects such as pieces of furniture but smaller or more irregularly shaped objects can go unseen. Also, just plugging the sensor into the breadboard means it isn't that stable and occasionally needs adjusting. I plan to create a proper mount for it at some stage to resolve this.

I have adjusted the infra-red sensors to look for objects on the ground at very short range. This works okay most of the time. Infra-red sensors have their limitations, not working well in direct sunlight or failing to recognise objects that poorly reflect infra-red light. At some stage I will get some more expensive sensors to see if they work better.

This article does a good job at comparing the relative merits of infra-red and ultrasonic sensors.

Adding hardware bump sensors would also be a good idea so that where the other sensors fail the robot doesn't plough on regardless potentially damaging itself and/or your furniture.

The current program just uses the accelerometer to sense if the robot has tipped over and then cuts the power. The accelerometer could also be used to sense gradual changes in gradient and to back away from potential problems. It could also be used to sense if the robot has stalled and to cut the power before any damage is done to the motors.

For the HC-SR04 Ultrasonic Sensor I used the NewPing library and for the MMA7361 3-axis Accelerometer I used the AcceleroMMA7361 library. You need to download those libraries and install them in the “libraries” directory within your Arduino development environment installation.

The sketch is available to download from Github. See the Arduino Getting Started pages if you are new to Arduino.

Return to home page.