Video
Arduino Uno Smart Line Following Robot With Obstacle Detection
Here is all the documentation. Make your own robot!
CODE:
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
#include <Servo.h> Servo left; Servo right; const int leftSensor=A4; //LeftLineSensor const int rightSensor=A5; //RightLineSensor const int leftLED=10; const int rightLED=4; const int Sensivity=475; // Sensivity is a number which you take as a reference to identify the color change. Normally, qrd1114(line sensor) sends analog signal which varies 0-100 for white color. This output of qrd1114 increases up to 650. This means there is a color change or the sensor is on the black surface. int i=0; int FL=1700; // Left Servo Forward int FR=1300; //Right Servo Forward int R=0; int L=0; void setup() { left.attach(12); //left servo motor right.attach(13); //right servo motor pinMode(leftSensor, INPUT); pinMode(rightSensor, INPUT); pinMode(3, INPUT); pinMode(2, OUTPUT); // Left IR LED & Receiver pinMode(7, INPUT); pinMode(6, OUTPUT); // Right IR LED & Receiver pinMode(leftLED, OUTPUT); pinMode(rightLED, OUTPUT); // Left&Right LED Serial.begin(9600); // Set data rate to 9600 bps tone(11, 3000, 1000); // Play tone for 1 second delay(1000); // Delay to finish tone } void loop() { int leftSens = ReadSens_L(); int rightSens = ReadSens_R(); int irLeft = irDetect(2, 3, 38000); // Check for object on left int irRight = irDetect(6, 7, 38000); // Check for object on right digitalWrite(leftLED, !irLeft); delay(1); digitalWrite(rightLED, !irRight); delay(1); if((irLeft=irDetect(2, 3, 38000))==0) // Obstacle Avoidance and Navigation Section { while((irLeft=irDetect(2, 3, 38000))==0) { left.writeMicroseconds(FR); right.writeMicroseconds(FR); Serial.println(" Obstacle Detected on the Path! Turning Left"); delay(1); Serial.print(irLeft); // Display 1/0 no detect/detect Serial.print(" "); // Display 1/0 no detect/detect Serial.println(irRight); // Display 1/0 no detect/detect tone(11, 300, 100); // Play tone for 1 second delay(300); // Delay to finish tone digitalWrite(leftLED, !irLeft); delay(1); digitalWrite(rightLED, !irRight); delay(1); } if((irRight = irDetect(6, 7, 38000))==1) { while((irRight = irDetect(6, 7, 38000))==1) { left.writeMicroseconds(FR); right.writeMicroseconds(FR); Serial.println("Continue Turning Left"); Serial.print(irLeft); // Display 1/0 no detect/detect Serial.print(" "); // Display 1/0 no detect/detect Serial.println(irRight); // Display 1/0 no detect/detect digitalWrite(leftLED, !irLeft); delay(1); digitalWrite(rightLED, !irRight); delay(1); } while((irRight = irDetect(6, 7, 38000))==0) { left.writeMicroseconds(FR); right.writeMicroseconds(FR); Serial.println("Turn Left!"); Serial.print(irLeft); // Display 1/0 no detect/detect Serial.print(" "); // Display 1/0 no detect/detect Serial.println(irRight); // Display 1/0 no detect/detect digitalWrite(leftLED, !irLeft); delay(1); digitalWrite(rightLED, !irRight); delay(1); } } else { while((irRight = irDetect(6, 7, 38000))==0) { left.writeMicroseconds(FL); right.writeMicroseconds(FR); Serial.println("Forward!"); Serial.print(irLeft); // Display 1/0 no detect/detect Serial.print(" "); // Display 1/0 no detect/detect Serial.println(irRight); // Display 1/0 no detect/detect digitalWrite(leftLED, !irLeft); delay(1); digitalWrite(rightLED, !irRight); delay(1); } } while( (leftSens = ReadSens_L())<Sensivity && (rightSens = ReadSens_R())<Sensivity ) { while((irRight = irDetect(6, 7, 38000))==0 && (leftSens = ReadSens_L())<Sensivity && (rightSens = ReadSens_R())<Sensivity) { left.writeMicroseconds(1600); right.writeMicroseconds(1400); digitalWrite(rightLED, !irRight); } while((irRight = irDetect(6, 7, 38000))==1 && (leftSens = ReadSens_L())<Sensivity && (rightSens = ReadSens_R())<Sensivity) { left.writeMicroseconds(1550); right.writeMicroseconds(1550); digitalWrite(rightLED, !irRight); delay(1); } } left.writeMicroseconds(1500); right.writeMicroseconds(1500); if((leftSens = ReadSens_L())<Sensivity && (rightSens = ReadSens_R())>Sensivity) // Entering Line { while((leftSens = ReadSens_L())<Sensivity && (rightSens = ReadSens_R())<Sensivity) { left.writeMicroseconds(FR); right.writeMicroseconds(FR); digitalWrite(leftLED, !irLeft); delay(1); digitalWrite(rightLED, !irRight); delay(1); }} else { if((leftSens = ReadSens_L())>Sensivity && (rightSens = ReadSens_R())<Sensivity) { while((rightSens = ReadSens_R())<Sensivity) { left.writeMicroseconds(FR); right.writeMicroseconds(FR); digitalWrite(leftLED, !irLeft); delay(1); digitalWrite(rightLED, !irRight); delay(1); } while((rightSens = ReadSens_R())>Sensivity) { left.writeMicroseconds(FR); right.writeMicroseconds(FR); delay(2); } }}} else //Line Following Section { //---------------------------------------------------------------- // This is Line Sensor Test /* Serial.print(leftSens); delay(1); Serial.print(" <--L R--> "); delay(1); Serial.println(rightSens); delay(1000); */ //---------------------------------------------------------------- while( (leftSens = ReadSens_L())>Sensivity && (rightSens = ReadSens_R())>Sensivity ) { left.writeMicroseconds(FR); right.writeMicroseconds(FR); delay(2); } Serial.println(" Following Line"); delay(1); Serial.print(irLeft); // Display 1/0 no detect/detect Serial.print(" "); // Display 1/0 no detect/detect Serial.println(irRight); // Display 1/0 no detect/detect if( (leftSens = ReadSens_L())<Sensivity && (rightSens = ReadSens_R())<Sensivity ) { left.writeMicroseconds(FL); right.writeMicroseconds(FR); delay(2); } else { if( (leftSens = ReadSens_L())>Sensivity && (rightSens = ReadSens_R())<Sensivity ) { left.writeMicroseconds(FR); right.writeMicroseconds(FR); delay(2); } else { if( (leftSens = ReadSens_L())<Sensivity && (rightSens = ReadSens_R())>Sensivity ) { left.writeMicroseconds(1700); right.writeMicroseconds(1700); delay(2); } }}} } int ReadSens_L(){ int i; int L = 0; for (i = 0; i < 5; i++){ L = L + analogRead(A4); // sensor on analog pin A2 } L = L / 5; // average return L; } int ReadSens_R(){ int j; int R = 0; for (j = 0; j < 5; j++){ R = R + analogRead(A5); // sensor on analog pin A3 } R = R / 5; // avarage return R; } // IR Object Detection Function int irDetect(int irLedPin, int irReceiverPin, long frequency) { tone(irLedPin, frequency, 8); // IRLED 38 kHz for at least 1 ms delay(1); // Wait 1 ms int ir = digitalRead(irReceiverPin); // IR receiver -> ir variable delay(1); // Down time before recheck return ir; // Return 1 no detect, 0 detect } |
Isa Hatipoglu © WordPress Post
Advertisements