Introduction
In 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 Works
Sparki 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 Sparki
Please 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. This code requires integers, variables, basic programming, wheel code, if statements and infrared reflector sensors. 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <Sparki.h> // include the sparki library void setup() { } void loop() { int edgeLeft = sparki.edgeLeft(); // measure the left edge IR sensor int edgeRight = sparki.edgeRight(); // measure the right edge IR sensor int threshold = 200; // if below this value, no surface underneath if (edgeLeft < threshold) // if no surface underneath left sensor { sparki.moveBackward(5); sparki.moveRight(20); // turn right } if (edgeRight < threshold) // if no surface underneath right sensor { sparki.moveBackward(5); sparki.moveLeft(20); // turn left } sparki.moveForward(); // move forward delay(100); // wait 0.1 seconds } |
- The angle belonging to the turnLeft and turnRight commands, which has a value of 20 (degrees).
- The distance belonging to the moveBackward command, which has a value of 5 (centimeters).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <Sparki.h> // include the sparki library int backwardDistance = 5; int angle = 20; void setup() { } void loop() { int edgeLeft = sparki.edgeLeft(); // measure the left edge IR sensor int edgeRight = sparki.edgeRight(); // measure the right edge IR sensor int threshold = 200; // if below this value, no surface underneath if (edgeLeft < threshold) // if no surface underneath left sensor { sparki.moveBackward(backwardDistance); sparki.moveRight(angle); // turn right } if (edgeRight < threshold) // if no surface underneath right sensor { sparki.moveBackward(backwardDistance); sparki.moveLeft(angle); // turn left } sparki.moveForward(); // move forward delay(100); // wait 0.1 seconds } |