In this lesson, we will learn how to avoid terrible accidents. We will create an accident to stop the robot from falling off the edge of the table. To do this, we will be using the IR sensors at the front of the robot. We have seen in a previous lesson that the infrared sensor can distinguish between black and white colours. In actual fact, when there is nothing below the sensor (for example when it reaches the edge and is about to fall off), it is the same as when it sees black. So all we need to do to program our don´t fall off robot is make it detect black. Let´s get to work!
First we need to declare the variables for the IR sensors and the servos.
Once we have done this, our robot will move in a straight line when there is ground or a surface beneath it and it will turn 2 seconds before it falls off. We will know when it´s about to fall because one of the IR sensors will detect BLACK (NEGRO).
Whoa, this is complicated! Don´t worry, we will break it down, bit by bit.
- We want to be able to detect when the Renacuajo is about to fall, which means either the right or the left IR sees blacks. This is how we do it:
- When one of the two detects the gap, we have to turn by stopping the right servo and turning towards the left servo. We also pause it for 2000 milliseconds, i.e. 2 seconds.
- On the other hand, if neither of the IRs detects a gap, meaning we´re on safe ground, then we can move forward. To do this, the left servo moves clockwise, whereas the right servo will move anti-clockwise.
Did you see? Easy! Program it yourself on bitbloq and then send us a video so that everyone can see it. See you next time robonauts.