Cleaning Robot

Fire that Roomba! Get Sparki cleaning up the room.



Now that we have learned a lot of things about the Sparki, like moving it or using its distance, light and line sensors, gripper, and buzzer, we can try to make a slightly more complex application. So let’s program the Sparki to clean an area where someone has forgotten a few trash receptacles: 

What You’ll Need

  • Sparki.
  • convex polygon with its perimeter line printed in black over a white background.
  • Some lightweight obstacles, such as cardboard boxes, cylinders, or even balls.

How It Works

The cleaning robot program is easier to be understand if we first think about what the steps are that the robot should actually do. The overall idea is to program the Sparki to do the following steps:
  1. Sparki rotates in its place, searching for an obstacle with its ultrasonic distance sensor (also called ultrasonic range finder).
  2. Once Sparki finds the obstacle, it advances (moving forward) towards it in order to expel that object outside the perimeter.
  3. The movement in the forward direction continues until a line sensor (or infrared reflectance sensor) detects the black line. That’s when the robot stops.
  4. Now, the robot goes backward for a few milliseconds and rotates to the left a bit more to avoid detecting the same object again.
  5. The program now goes back to step 1.


So, let’s start programming one step at a time, in order to better understand the program.Warning_Triangle 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.First, let’s make Sparki rotate until it finds an obstacle:As we have learned from the Moving the Robot lesson, this implies to rotate one wheel clockwise and the other counterclockwise, each of them at the same speed. This can be accomplished using the following blocks (the first to rotate to the left and the second to rotate to the right):

Also, we need to move the robot forward when its ultrasonic sensor detects a close object in front of the robot using the following block:Here is the initial code to find the object and move towards it (please take a look at the lesson about the ultrasonic ranger if you are not familiar with that sensor):Note: The complete code for the final version of this program is available at the end of this lesson.As you can see in the previous code snippet, everything is executed inside an infinite cycle (equivalent to a while(true) function), which means that the code will be repeating until the robot is turned off (or until its batteries goes discharged). And to be sure that we detect the object in front of the robot, we run another cycle (a while cycle) inside the first (infinite) while, which ends each time the sensor detects an obstacle close enough (we set the detection distance in the while‘s condition). You will see that the program also makes a small beep when it finds the object, just to help us to know what is happening. Finally, as the ultrasonic ranger has a conic detection zone, we added another rotation block after the while, just to let the robot to center towards the detected object: Now we can make Sparki move forward in the direction of the detected object, but making sure not to go outside the perimeter: Here is the complete code:As you can see in the previous code, once the line is detected, the robot stops to rotate, and goes backward for 2 centimeters (you can change this value to experiment!) and then it moves 30 degrees to the left again, just to avoid detecting the same object that it just expelled a few moments ago. 

Extra Activities

  • Try to use the right, left and edge infrared reflectance sensors to improve the line detection. Experiment with them a bit, since they may not be actually that useful in this example.
  • What about adding line detection to the robot’s rotation also (in the “object detection phase”)?  This way, we make sure that it never goes outside the perimeter by error.
  • Another possible place to add line detection is when the Sparki is going backward. You can try it!
  • If you want to completely change the program, you can also take advantage of the capability of Sparki’s ultrasonic sensor to be moved with the servo. So why not code a new program where you move Sparki’s “head” to find the objects?