Requires Ultra Sonic sensor
In this tutorial we will create a obstacle avoidance robot using the HC-SR04 Ultra sonic sensor. This is a cheap sensor that uses sound waves to calculate distances to objects. The function sends out a 10 microsecond pulse, starts a timer then stops the timer when an echo of the pulse is received, Thge round trip time is then used to calculate the distance to the object.
Now we have a function to detect objects we need to create a simple path finding algorithm so the robot knows how to behave when an object is detected. This will be a simple path finding algorithm that will tell the robot to turn when an object is within 50cm of the sensor. The code below is the comlete code listing where the code under the while loop is the path finding algorithm.