Note: this page refers to a product that is retired.

RobotRunawayRobot

Play tag with your robot using a distance sensor.

Runaway Robot

Is your robot bumping into walls? This quickly solves that problem. By attaching a ultrasonic rangefinder, the robot can find out if it's too close to obstacles, and turn around to avoid collision.

Hardware Required

  • Arduino Robot

  • ultrasonic rangefinder

  • cable to plug the Ultrasonic sensor

Instruction

  1. Connect the Ultrasonic ranger to TK1

  2. Fix the sensor to the front of the robot

  3. Upload the example, unplug USB and turn on power

  4. Put the robot on the floor

  5. The robot will move around, avoiding obstacles in front of it

  6. If you want to change the detection range, in the code below, find the line

    @@while (getDistance()<20)**
    , 20 here means 20cm; change it to another value to try out other sensitivities.

  7. You can also change the robot speed, look at motorWrite() for a hint on how to do that.

Try it out

Runaway robot
Runaway robot

Code

1/* Runaway Robot
2
3 Play tag with your robot! With an ultrasonic
4
5 distance sensor, it's capable of detecting and avoiding
6
7 obstacles, never bumping into walls again!
8
9 You'll need to attach an untrasonic range finder to M1.
10
11 Circuit:
12
13 * Arduino Robot
14
15 * US range finder like Maxbotix EZ10, with analog output
16
17 created 1 May 2013
18
19 by X. Yang
20
21 modified 12 May 2013
22
23 by D. Cuartielles
24
25 This example is in the public domain
26
27 */
28
29// include the robot library
30#include <ArduinoRobot.h>
31#include <Wire.h>
32
33int sensorPin = M1; // pin is used by the sensor
34
35void setup() {
36
37 // initialize the Robot, SD card, and display
38
39 Serial.begin(9600);
40
41 Robot.begin();
42
43 Robot.beginTFT();
44
45 Robot.beginSD();
46
47 Robot.displayLogos();
48
49 // draw a face on the LCD screen
50
51 setFace(true);
52}
53
54void loop() {
55
56 // If the robot is blocked, turn until free
57
58 while (getDistance() < 40) { // If an obstacle is less than 20cm away
59
60 setFace(false); //shows an unhappy face
61
62 Robot.motorsStop(); // stop the motors
63
64 delay(1000); // wait for a moment
65
66 Robot.turn(90); // turn to the right and try again
67
68 setFace(true); // happy face
69
70 }
71
72 // if there are no objects in the way, keep moving
73
74 Robot.motorsWrite(255, 255);
75
76 delay(100);
77}
78
79// return the distance in cm
80float getDistance() {
81
82 // read the value from the sensor
83
84 int sensorValue = Robot.analogRead(sensorPin);
85
86 //Convert the sensor input to cm.
87
88 float distance_cm = sensorValue * 1.27;
89
90 return distance_cm;
91}
92
93// make a happy or sad face
94void setFace(bool onOff) {
95
96 if (onOff) {
97
98 // if true show a happy face
99
100 Robot.background(0, 0, 255);
101
102 Robot.setCursor(44, 60);
103
104 Robot.stroke(0, 255, 0);
105
106 Robot.setTextSize(4);
107
108 Robot.print(":)");
109
110 } else {
111
112 // if false show an upset face
113
114 Robot.background(255, 0, 0);
115
116 Robot.setCursor(44, 60);
117
118 Robot.stroke(0, 255, 0);
119
120 Robot.setTextSize(4);
121
122 Robot.print("X(");
123
124 }
125}

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.