Auto Obstacle Avoidance & Table Edge Detection Robot
A multi functional Arduino UNO based obstacle avoidance with Ultrasonic sensor and table edge detection using IR sensors.
Components and supplies
2
IR Sensor Module
10
Jumper wires (generic)
2
Battery, 3.7 V
2
DC Motor, 12 V
1
L293D Module
1
SG90 Micro-servo motor
1
Arduino UNO
1
Ultrasonic Sensor - HC-SR04 (Generic)
Tools and machines
1
Mastech MS8217 Autorange Digital Multimeter
1
Solder Wire, Lead Free
1
Soldering iron (generic)
1
Hot glue gun (generic)
1
Tape, Double Sided
Apps and platforms
1
RobotoApp
1
Arduino IDE
Project description
Code
code
c_cpp
1#include <Servo.h> 2#define BS 12 // back sensor 3#define FS 13 // front sensor 4#define L1 10 // left motor 5#define L2 11 // left motor 6#define R1 8 // right motor 7#define R2 9 // right motor 8#define E 2 // attach pin D2 Arduino to pin Echo of HC-SR04 9#define T 3 //attach pin D3 Arduino to pin Trig of HC-SR04 10#define S 5 // for speed of pin 5 11#define SR 4// Servo 12#define VIN A0 13Servo srv; // create servo object to control a servo 14boolean isAuto = false;// Manual Default 15 16 17void setup () 18{ 19 Serial.begin(9600); 20 pinMode(FS, INPUT); 21 pinMode(BS, INPUT); 22 pinMode(L1, OUTPUT); 23 pinMode(L2, OUTPUT); 24 pinMode(R1, OUTPUT); 25 pinMode(R2, OUTPUT); 26 pinMode(S, OUTPUT); 27 pinMode(T, OUTPUT); 28 pinMode(E, INPUT); 29 pinMode(VIN, INPUT); 30 31 srv.attach(SR); 32 melody(); 33 34} 35const unsigned short melodyArr[] = { 36 262, 196, 196, 220, 196, 0, 247, 262 37}; 38const byte noteDurations[] = { 39 250, 125, 125, 250, 250, 250, 250, 250 40}; 41void melody() { 42 for (int i = 0; i < 8; i++) { 43 int noteDuration = noteDurations[i]; 44 tone(6, melodyArr[i], noteDuration); 45 int pauseBetweenNotes = noteDuration * 1.30; 46 delay(pauseBetweenNotes); 47 noTone(6); 48 } 49} 50unsigned int s = 255; 51void loop() 52{ 53 String str = ""; 54 char c; 55 int di = ping(); 56 57 58 if (Serial.available()) { 59 while (Serial.available()) { 60 61 str = str + (char)Serial.read(); 62 63 } 64 if (str.length() > 0) { 65 c = str[0]; 66 } 67 68 if (c == '1' && str.length() == 1) { 69 move(true, false, true, false, s); 70 } else if (c == '2' && str.length() == 1) { 71 move(false, true, false, true, s); 72 } else if (c == '3' && str.length() == 1) { 73 move(true, false, false, true, s);// Left 74 } else if (c == '4' && str.length() == 1) { 75 move(false, true, true, false, s); // Right 76 } else if (c == '5' && str.length() == 1) { 77 move(false, false, false, false, s); 78 } else if (str.startsWith("sp:")) { 79 s = str.substring(3).toInt(); 80 } else if (str.startsWith("sr:")) { 81 srv.write(str.substring(3).toInt()); 82 } else if (str.startsWith("tn:")) { 83 tone(6, 480, str.substring(3).toInt()); 84 } else if (str.startsWith("mo:")) { 85 isAuto = str.substring(3).toInt() == 0 ? false : true; 86 } else if (str == "ml") { 87 melody(); 88 } else if (str == "di") { 89 Serial.println("di:" + String(di)); 90 } else if (str == "vin") { 91 float vin = (analogRead(VIN) * 5.0) / 1023; 92 Serial.println("vin:" + String(vin)); 93 } 94 95 } 96 char x = digitalRead(FS) ? '2' : '1'; 97 if ((di <= 20 || digitalRead(BS) || digitalRead(FS)) && c != x && !isAuto) { 98 move(false, false, false, false, s); 99 } else if (isAuto && (di <= 20 || digitalRead(FS))) { 100 move(false, false, false, false, s);//Stop 101 tone(6, 480, 1000); 102 delay(1000); 103 tone(6, 480, 1000); 104 noTone(6); 105 if ( ping() > 20 && !digitalRead(FS)) { 106 delay(100); 107 move(true, false, true, false, s); 108 } else { 109 delay(100); 110 move(false, true, false, true, s);//Back 111 delay(400); 112 move(false, false, false, false, s);//Stop 113 delay(300); 114 int dRight = lookRight(); 115 delay(300); 116 int dLeft = lookLeft(); 117 delay(300); 118 if (dRight >= 20) { 119 move(false, true, true, false, s); // Right 120 delay(600); 121 move(false, false, false, false, s); 122 } else if (dLeft >= 20) { 123 move(true, false, false, true, s);// Left 124 delay(600); 125 move(false, false, false, false, s); 126 } else { 127 move(false, false, false, false, s);//Stop 128 melody(); 129 } 130 131 } 132 133 } else if (isAuto) { 134 move(true, false, true, false, s); 135 } 136} 137int lookRight() { 138 srv.write(0); 139 delay(300); 140 int d = ping(); 141 srv.write(90); 142 return d; 143} 144int lookLeft() { 145 srv.write(180); 146 delay(300); 147 int d = ping(); 148 srv.write(90); 149 return d; 150} 151int ping() { 152 delay(100); 153 154 // Clears the trigPin condition 155 digitalWrite(T, LOW); 156 delayMicroseconds(2); 157 // Sets the trigPin HIGH (ACTIVE) for 10 microseconds 158 digitalWrite(T, HIGH); 159 delayMicroseconds(10); 160 digitalWrite(T, LOW); 161 // Reads the echoPin, returns the sound wave travel time in microseconds 162 int du = pulseIn(E, HIGH); 163 // Calculating the distance 164 return du * 0.034 / 2; // Speed of sound wave divided by 2 (go and back) 165} 166void move(bool l1, bool l2, bool r1, bool r2, int s) { 167 168 analogWrite(S, s); 169 digitalWrite(L1, l1); 170 digitalWrite(L2, l2); 171 digitalWrite(R1, r1); 172 digitalWrite(R2, r2); 173 174 175}
code
c_cpp
1#include <Servo.h> 2#define BS 12 // back sensor 3#define FS 13 // front sensor 4#define L1 10 // left motor 5#define L2 11 // left motor 6#define R1 8 // right motor 7#define R2 9 // right motor 8#define E 2 // attach pin D2 Arduino to pin Echo of HC-SR04 9#define T 3 //attach pin D3 Arduino to pin Trig of HC-SR04 10#define S 5 // for speed of pin 5 11#define SR 4// Servo 12#define VIN A0 13Servo srv; // create servo object to control a servo 14boolean isAuto = false;// Manual Default 15 16 17void setup () 18{ 19 Serial.begin(9600); 20 pinMode(FS, INPUT); 21 pinMode(BS, INPUT); 22 pinMode(L1, OUTPUT); 23 pinMode(L2, OUTPUT); 24 pinMode(R1, OUTPUT); 25 pinMode(R2, OUTPUT); 26 pinMode(S, OUTPUT); 27 pinMode(T, OUTPUT); 28 pinMode(E, INPUT); 29 pinMode(VIN, INPUT); 30 31 srv.attach(SR); 32 melody(); 33 34} 35const unsigned short melodyArr[] = { 36 262, 196, 196, 220, 196, 0, 247, 262 37}; 38const byte noteDurations[] = { 39 250, 125, 125, 250, 250, 250, 250, 250 40}; 41void melody() { 42 for (int i = 0; i < 8; i++) { 43 int noteDuration = noteDurations[i]; 44 tone(6, melodyArr[i], noteDuration); 45 int pauseBetweenNotes = noteDuration * 1.30; 46 delay(pauseBetweenNotes); 47 noTone(6); 48 } 49} 50unsigned int s = 255; 51void loop() 52{ 53 String str = ""; 54 char c; 55 int di = ping(); 56 57 58 if (Serial.available()) { 59 while (Serial.available()) { 60 61 str = str + (char)Serial.read(); 62 63 } 64 if (str.length() > 0) { 65 c = str[0]; 66 } 67 68 if (c == '1' && str.length() == 1) { 69 move(true, false, true, false, s); 70 } else if (c == '2' && str.length() == 1) { 71 move(false, true, false, true, s); 72 } else if (c == '3' && str.length() == 1) { 73 move(true, false, false, true, s);// Left 74 } else if (c == '4' && str.length() == 1) { 75 move(false, true, true, false, s); // Right 76 } else if (c == '5' && str.length() == 1) { 77 move(false, false, false, false, s); 78 } else if (str.startsWith("sp:")) { 79 s = str.substring(3).toInt(); 80 } else if (str.startsWith("sr:")) { 81 srv.write(str.substring(3).toInt()); 82 } else if (str.startsWith("tn:")) { 83 tone(6, 480, str.substring(3).toInt()); 84 } else if (str.startsWith("mo:")) { 85 isAuto = str.substring(3).toInt() == 0 ? false : true; 86 } else if (str == "ml") { 87 melody(); 88 } else if (str == "di") { 89 Serial.println("di:" + String(di)); 90 } else if (str == "vin") { 91 float vin = (analogRead(VIN) * 5.0) / 1023; 92 Serial.println("vin:" + String(vin)); 93 } 94 95 } 96 char x = digitalRead(FS) ? '2' : '1'; 97 if ((di <= 20 || digitalRead(BS) || digitalRead(FS)) && c != x && !isAuto) { 98 move(false, false, false, false, s); 99 } else if (isAuto && (di <= 20 || digitalRead(FS))) { 100 move(false, false, false, false, s);//Stop 101 tone(6, 480, 1000); 102 delay(1000); 103 tone(6, 480, 1000); 104 noTone(6); 105 if ( ping() > 20 && !digitalRead(FS)) { 106 delay(100); 107 move(true, false, true, false, s); 108 } else { 109 delay(100); 110 move(false, true, false, true, s);//Back 111 delay(400); 112 move(false, false, false, false, s);//Stop 113 delay(300); 114 int dRight = lookRight(); 115 delay(300); 116 int dLeft = lookLeft(); 117 delay(300); 118 if (dRight >= 20) { 119 move(false, true, true, false, s); // Right 120 delay(600); 121 move(false, false, false, false, s); 122 } else if (dLeft >= 20) { 123 move(true, false, false, true, s);// Left 124 delay(600); 125 move(false, false, false, false, s); 126 } else { 127 move(false, false, false, false, s);//Stop 128 melody(); 129 } 130 131 } 132 133 } else if (isAuto) { 134 move(true, false, true, false, s); 135 } 136} 137int lookRight() { 138 srv.write(0); 139 delay(300); 140 int d = ping(); 141 srv.write(90); 142 return d; 143} 144int lookLeft() { 145 srv.write(180); 146 delay(300); 147 int d = ping(); 148 srv.write(90); 149 return d; 150} 151int ping() { 152 delay(100); 153 154 // Clears the trigPin condition 155 digitalWrite(T, LOW); 156 delayMicroseconds(2); 157 // Sets the trigPin HIGH (ACTIVE) for 10 microseconds 158 digitalWrite(T, HIGH); 159 delayMicroseconds(10); 160 digitalWrite(T, LOW); 161 // Reads the echoPin, returns the sound wave travel time in microseconds 162 int du = pulseIn(E, HIGH); 163 // Calculating the distance 164 return du * 0.034 / 2; // Speed of sound wave divided by 2 (go and back) 165} 166void move(bool l1, bool l2, bool r1, bool r2, int s) { 167 168 analogWrite(S, s); 169 digitalWrite(L1, l1); 170 digitalWrite(L2, l2); 171 digitalWrite(R1, r1); 172 digitalWrite(R2, r2); 173 174 175}
Downloadable files
Schematic Diagram
Schematic Diagram

Comments
Only logged in users can leave comments