Get Help
Wall Avoidance
Introduction
Sparki can use its ultrasonic sensor to avoid running into walls. In this lesson you will program Sparki to do just that!What You’ll Need
- A Sparki.
- Some space for it to run around in and some walls. You can also use solid objects like cardboard boxes for your walls. Try to have hard walls (instead of soft things like pillows or cloth) since the ultrasonic range finder thinks those are easier to see.
How It Works
Here is the ultrasonic sensor or ultrasonic range finder. Sparki uses it to see what is in front of it in a similar way to how a dolphin or a bat uses echolocation. If you are not familiar with this sensor, please read this previous lesson. Also, please remember (you can find more information here) that this sensor has a conic detection zone, like this: 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. In this example, we’re going to code up something where Sparki will wander with a green light on (using its RGB LED) until it finds a wall is shortly ahead of it. Sparki then puts on the red light, moves backward, turns, puts the LED back to green, and then continues on. These are the basic tasks that we want the robot to do in order to advance while avoiding obstacles:- Center the ultrasonic sensor so it points to the front.
- Change the color of the LED to green.
- Make Sparki go forward.
- Sense distance with the rangefinder.
- When Sparki’s rangefinder finds an obstacle:
- Make Sparki beep using the buzzer.
- Change the LED to red.
- Make Sparki move backward.
- Make Sparki turn.
Coding the Wall Avoidance Program
Now that we know what to do, let’s figure out how to do it. Looking at the parts list we can look up how to do the following things and find the following codes:- Center the ultrasonic sensor so it points to the front:
1 |
sparki.servo(SERVO_CENTER); |
- Change the color of the LED to green:
1 |
sparki.RGB(RGB_GREEN); |
- Make Sparki go forward:
1 |
sparki.moveForward() |
- Sense distance with the rangefinder:
1 |
sparki.ping(); |
- Make Sparki beep:
1 |
sparki.beep(); |
- Change the LED to red:
1 |
sparki.RGB(RGB_RED); |
- Make Sparki move backward:
1 |
sparki.moveBackward(10) |
- Make Sparki turn:
1 |
sparki.moveRight(30); |
- Change the color of the LED to green.
- Go forward.
1 2 |
sparki.moveForward(); // move forward sparki.RGB(RGB_GREEN); // turn the light green |
- Make the LED red
- Beep
- Move backward
- Turn
1 2 3 4 |
sparki.RGB(RGB_RED); // turn the light red sparki.beep(); // beep! sparki.moveBackward(10); // back up 10 centimeters sparki.moveRight(30); // rotate right 30 degrees |
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 |
#include <Sparki.h> // include the sparki library void setup() { sparki.servo(SERVO_CENTER); // Center the Servo } void loop() { sparki.RGB(RGB_GREEN); // turn the light green sparki.moveForward(); // move Sparki forward int cm = sparki.ping(); // measures the distance with Sparki's eyes if(cm != -1) // make sure its not too close or too far { if(cm < 20) // if the distance measured is less than 20 centimeters { sparki.RGB(RGB_RED); // turn the light red sparki.beep(); // beep! sparki.moveBackward(10); // back up 10 centimeters sparki.moveRight(30); // rotate right 30 degrees delay(1000); } } delay(100); // wait 0.1 seconds (100 milliseconds) } |
Related Lessons
To learn more about the ultrasonic range finder, take a look at the following lessons:- Using the Ultrasonic Distance Sensor
- Theremin
- Also, a similar technique but with different sensors is used in the Edge Avoidance lesson.