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
Connect the Ultrasonic ranger to TK1
Fix the sensor to the front of the robot
Upload the example, unplug USB and turn on power
Put the robot on the floor
The robot will move around, avoiding obstacles in front of it
If you want to change the detection range, in the code below, find the line
, 20 here means 20cm; change it to another value to try out other sensitivities.@@while (getDistance()<20)**
You can also change the robot speed, look at motorWrite() for a hint on how to do that.
Try it out
Code
1/* Runaway Robot2
3 Play tag with your robot! With an ultrasonic4
5 distance sensor, it's capable of detecting and avoiding6
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 Robot14
15 * US range finder like Maxbotix EZ10, with analog output16
17 created 1 May 201318
19 by X. Yang20
21 modified 12 May 201322
23 by D. Cuartielles24
25 This example is in the public domain26
27 */28
29// include the robot library30#include <ArduinoRobot.h>31#include <Wire.h>32
33int sensorPin = M1; // pin is used by the sensor34
35void setup() {36
37 // initialize the Robot, SD card, and display38
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 screen50
51 setFace(true);52}53
54void loop() {55
56 // If the robot is blocked, turn until free57
58 while (getDistance() < 40) { // If an obstacle is less than 20cm away59
60 setFace(false); //shows an unhappy face61
62 Robot.motorsStop(); // stop the motors63
64 delay(1000); // wait for a moment65
66 Robot.turn(90); // turn to the right and try again67
68 setFace(true); // happy face69
70 }71
72 // if there are no objects in the way, keep moving73
74 Robot.motorsWrite(255, 255);75
76 delay(100);77}78
79// return the distance in cm80float getDistance() {81
82 // read the value from the sensor83
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 face94void setFace(bool onOff) {95
96 if (onOff) {97
98 // if true show a happy face99
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 face113
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.