IntroductionIn this tutorial, you’ll learn how to make Sparki avoid falling off a table using the sensors on the underside of the robot:
What You’ll Need
- A Sparki
- A table (preferably a light colored non-transparent one).
How It WorksSparki has an array of infrared sensors underneath that it uses to detect if there is anything underneath it. We’re going to program Sparki so that it knows it should stop moving and turn around if it detects something underneath it. This will help Sparki not fall off tables. Here you can see the sensors in the bottom of the robot:
Infrared reflectance sensors on Sparki’s underside
Infrared reflectance sensors can be thought of as infrared light (which humans can not see), and some special eyes that look at that light. The eyes are made out of a photo-sensible material (a material which changes its electrical properties in response to changes in the incident light) and an LED (or Light Emitting Diode):
Here are the parts again, but with pictures showing what you’d normally think of them as!
When the infrared light shines off the edge of the table, almost nothing comes back!
Programming It With SparkiPlease remember to check that the batteries are properly connected (and charged!). And as we are going to use the motors here, please check that the On/Off Switch is on. Another important thing to take care of when playing with the robot’s motors is to be careful not to be working over a table. A fall from that table could permanently damage your Sparki. If you are not familiar with the commands to move the robot, please take a look to the Moving the Robot lesson. The actual code is relatively simple. We set a threshold beyond which we assume so little light is coming back that we are shining the lights off the table. Then, if the right sensor sees that, we go left, and if the left sensor sees that, we go right. After that we go forward. Looping this behavior causes Sparki to wander around a table without falling off. If you’re having trouble getting Sparki to tell the difference between the table and the edge, consider readjusting your threshold variable. Here is the code: Great job! You have now programmed Sparki to avoid running off edges! Taking a look at the previous code, there are two important numeric values that could be useful to experiment with:
- The angle belonging to the turnLeft and turnRight blocks, which has a value of 20 (degrees).
- The distance belonging to the moveBackward block, which has a value of 5 (centimeters).