Building on from my work in my last post, I purchased another 2 ultrasonic distance sensors and added some additional logic to my existing code to make it easier for the robot to avoid obstacles in its path.

The new program allows the robot to act in 2 different modes depending on the direction the obstacle is in. I've called these modes Passive Avoidance and Active Avoidance.

While in Passive Avoidance mode the robot simply drives forwards and scans its environment for obstacles. If it encounters an obstacle within 30cm of its left or right sensor it turns slightly away from the obstacle before continuing forwards again.

If the robot detects an obstacle within 30cm of its front sensor then it pauses and switches to Active Avoidance mode, measures the distance to the nearest object on its left and right, and then turns in the direction where the nearest object is furthest away.

These 2 modes work perfectly as long as the robot doesn't drive itself directly into the center of a 90 degree corner. If it does then it will become problematic because both the left and right sensor will detect the same distance value and could cause the robot to get stuck. During testing the robot always managed to wiggle itself out of a corner, but a long term fix could be to include an additional function to force the robot to back up and turn a random amount/in a random direction before continuing.

You'll note from the pictures above that I've added 2 new switches to the robot. The one on the robot's right rear is the main power switch for its battery box, while the switch on the robot's left side, when wired in, will act as a program interrupt (using a while loop) to prevent the motors from spinning while the robot is being programmed.

You can find the Arduino code for this version of the robot on GitHub, along with some other sample code from my previous work.