Components and supplies
1
Breadboard (generic)
3
Color Sensor
1
Motor Shield V1
1
9V battery (generic)
4
DC motor (generic)
Tools and machines
1
Soldering iron (generic)
Apps and platforms
1
Arduino Web Editor
1
Arduino IDE
Project description
Code
Copie and Paste Auto Code V17
arduino
Thats the Coe for Copy and Paste. My Change at times.
1#include <AFMotor.h> 2AF_DCMotor motor1(1, MOTOR12_1KHZ); 3AF_DCMotor 4 motor2(3, MOTOR34_1KHZ); 5AF_DCMotor motor3(2, MOTOR12_1KHZ); 6AF_DCMotor motor4(4, 7 MOTOR34_1KHZ); 8//Farbe 9const int s0 = 47; //Conection for Sensor 10const 11 int s1 = 49; 12 13const int s2L = A14; 14const int s3L = A13; 15const int outL 16 = A15; 17 18int redL = 0; //Variablen for Left LEDs named 19int greenL = 0; 20int 21 blueL = 0; 22 23const int s2R = A11; 24const int s3R = A10; 25const int outR 26 = A12; 27 28int redR = 0; //Variablen for Right LEDs named 29int greenR = 0; 30int 31 blueR = 0; 32 33const int s2b = A8; 34const int s3b = A7; 35const int outb = 36 A9; 37 38int redb = 0; //Variablen for Right LEDs named 39int greenb = 0; 40int 41 blueb = 0; 42 43int sensor_left = 0; 44int sensor_right = 0; 45int sensor_back 46 = 0; 47 48void setup() 49 50{ 51 52 Serial.begin(9600); //Serielle Kommunikation 53 54 55 pinMode(s0, OUTPUT); 56 pinMode(s1, OUTPUT); 57 digitalWrite(s0, HIGH); 58 59 digitalWrite(s1, HIGH); 60 61 pinMode(s2L, OUTPUT); 62 pinMode(s3L, OUTPUT); 63 64 pinMode(outL, INPUT); 65 66 pinMode(s2R, OUTPUT); 67 pinMode(s3R, OUTPUT); 68 69 pinMode(outR, INPUT); 70 71 pinMode(s2b, OUTPUT); 72 pinMode(s3b, OUTPUT); 73 74 pinMode(outb, INPUT); 75 76 77 78} 79 80void loop() 81{ 82 color(); 83 84 85 { //detection 86 { //Sensor Left 87 if (blueL <= 10 && redL >= 11 && 88 greenL >= 11 && blueL < redL && blueL < greenL) 89 90 { 91 sensor_left 92 = 1; 93 } 94 else 95 { 96 sensor_left = 0; 97 } 98 99 } 100 101 { //Sensor Right 102 if (blueR <= 10 && redR >= 11 && greenR 103 >= 11 && blueR < redR && blueR < greenR) 104 105 { 106 sensor_right 107 = 1; 108 } 109 else 110 { 111 sensor_right = 0; 112 } 113 114 } 115 { //Sensor Back 116 if (blueb <= 10 && redb >= 11 && greenb 117 >= 11 && blueb < redb && blueb < greenb) 118 119 { 120 sensor_back = 121 1; 122 } 123 else 124 { 125 sensor_back = 0; 126 } 127 128 } 129 } 130 { //Drive 131if (sensor_left ==1 or sensor_right==1) 132{ 133 134 Found: 135if (sensor_left == 1 && sensor_right == 1 or sensor_left == 1 && sensor_right 136 == 1 && sensor_back == 0) 137 {//Straightforward 138 Straightforward: 139 140 motor1.setSpeed(150); 141 motor2.setSpeed(150); 142 motor3.setSpeed(150); 143 144 motor4.setSpeed(150); 145 motor1.run(FORWARD); 146 motor2.run(FORWARD); 147 148 motor3.run(FORWARD); 149 motor4.run(FORWARD); 150 delay(7); 151 152 motor1.run(RELEASE); 153 motor2.run(RELEASE); 154 motor3.run(RELEASE); 155 156 motor4.run(RELEASE); 157 Serial.print(" Straightforward "); 158 } 159 160 else if (sensor_left == 0 && sensor_right == 1) 161 {//Right Curve 162 motor1.setSpeed(225); 163 164 motor2.setSpeed(225); 165 motor3.setSpeed(200); 166 motor4.setSpeed(200); 167 168 motor1.run(BACKWARD); 169 motor2.run(BACKWARD); 170 motor3.run(FORWARD); 171 172 motor4.run(FORWARD); 173 delay(7); 174 motor1.run(RELEASE); 175 176 motor2.run(RELEASE); 177 motor3.run(RELEASE); 178 motor4.run(RELEASE); 179 180 Serial.print(" Right Curve "); 181 } 182 else if (sensor_left == 1 183 && sensor_right == 0) 184 {//Left Curve 185 motor1.setSpeed(200); 186 motor2.setSpeed(200); 187 188 motor3.setSpeed(225); 189 motor4.setSpeed(225); 190 motor1.run(FORWARD); 191 192 motor2.run(FORWARD); 193 motor3.run(BACKWARD); 194 motor4.run(BACKWARD); 195 196 delay(7); 197 motor1.run(RELEASE); 198 motor2.run(RELEASE); 199 200 motor3.run(RELEASE); 201 motor4.run(RELEASE); 202 Serial.print(" 203 Left Curve "); 204 } 205 } 206 else if (sensor_left == 0 && sensor_right 207 == 0 && sensor_back == 1) 208 { 209//Backward 210 motor1.setSpeed(160); 211 212 motor2.setSpeed(160); 213 motor3.setSpeed(150); 214 motor4.setSpeed(150); 215 216 motor1.run(BACKWARD); 217 motor2.run(BACKWARD); 218 motor3.run(BACKWARD); 219 220 motor4.run(BACKWARD); 221 delay (7); 222 Serial.print(" Backward 223 "); 224 } 225 else 226 { 227 228 } 229 } 230} 231void color() 232 233{ 234 235 236 { //Left Sensor 237 digitalWrite(s2L, LOW); 238 digitalWrite(s3L, LOW); 239 240 redL = pulseIn(outL, digitalRead(outL) == HIGH ? LOW : HIGH); 241 digitalWrite(s3L, 242 HIGH); 243 blueL = pulseIn(outL, digitalRead(outL) == HIGH ? LOW : HIGH); 244 245 digitalWrite(s2L, HIGH); 246 greenL = pulseIn(outL, digitalRead(outL) == 247 HIGH ? LOW : HIGH); 248 } 249 250 { //Right Sensor 251 digitalWrite(s2R, LOW); 252 253 digitalWrite(s3R, LOW); 254 redR = pulseIn(outR, digitalRead(outR) == HIGH 255 ? LOW : HIGH); 256 digitalWrite(s3R, HIGH); 257 blueR = pulseIn(outR, digitalRead(outR) 258 == HIGH ? LOW : HIGH); 259 digitalWrite(s2R, HIGH); 260 greenR = pulseIn(outR, 261 digitalRead(outR) == HIGH ? LOW : HIGH); 262 } 263 { //Back Sensor 264 digitalWrite(s2b, 265 LOW); 266 digitalWrite(s3b, LOW); 267 redb = pulseIn(outb, digitalRead(outb) 268 == HIGH ? LOW : HIGH); 269 digitalWrite(s3b, HIGH); 270 blueb = pulseIn(outb, 271 digitalRead(outb) == HIGH ? LOW : HIGH); 272 digitalWrite(s2b, HIGH); 273 greenb 274 = pulseIn(outb, digitalRead(outb) == HIGH ? LOW : HIGH); 275 } 276}
Copie and Paste Auto Code V17
arduino
Thats the Coe for Copy and Paste. My Change at times.
1#include <AFMotor.h> 2AF_DCMotor motor1(1, MOTOR12_1KHZ); 3AF_DCMotor motor2(3, MOTOR34_1KHZ); 4AF_DCMotor motor3(2, MOTOR12_1KHZ); 5AF_DCMotor motor4(4, MOTOR34_1KHZ); 6//Farbe 7const int s0 = 47; //Conection for Sensor 8const int s1 = 49; 9 10const int s2L = A14; 11const int s3L = A13; 12const int outL = A15; 13 14int redL = 0; //Variablen for Left LEDs named 15int greenL = 0; 16int blueL = 0; 17 18const int s2R = A11; 19const int s3R = A10; 20const int outR = A12; 21 22int redR = 0; //Variablen for Right LEDs named 23int greenR = 0; 24int blueR = 0; 25 26const int s2b = A8; 27const int s3b = A7; 28const int outb = A9; 29 30int redb = 0; //Variablen for Right LEDs named 31int greenb = 0; 32int blueb = 0; 33 34int sensor_left = 0; 35int sensor_right = 0; 36int sensor_back = 0; 37 38void setup() 39 40{ 41 42 Serial.begin(9600); //Serielle Kommunikation 43 44 pinMode(s0, OUTPUT); 45 pinMode(s1, OUTPUT); 46 digitalWrite(s0, HIGH); 47 digitalWrite(s1, HIGH); 48 49 pinMode(s2L, OUTPUT); 50 pinMode(s3L, OUTPUT); 51 pinMode(outL, INPUT); 52 53 pinMode(s2R, OUTPUT); 54 pinMode(s3R, OUTPUT); 55 pinMode(outR, INPUT); 56 57 pinMode(s2b, OUTPUT); 58 pinMode(s3b, OUTPUT); 59 pinMode(outb, INPUT); 60 61 62 63} 64 65void loop() 66{ 67 color(); 68 69 { //detection 70 { //Sensor Left 71 if (blueL <= 10 && redL >= 11 && greenL >= 11 && blueL < redL && blueL < greenL) 72 73 { 74 sensor_left = 1; 75 } 76 else 77 { 78 sensor_left = 0; 79 } 80 } 81 82 { //Sensor Right 83 if (blueR <= 10 && redR >= 11 && greenR >= 11 && blueR < redR && blueR < greenR) 84 85 { 86 sensor_right = 1; 87 } 88 else 89 { 90 sensor_right = 0; 91 } 92 } 93 { //Sensor Back 94 if (blueb <= 10 && redb >= 11 && greenb >= 11 && blueb < redb && blueb < greenb) 95 96 { 97 sensor_back = 1; 98 } 99 else 100 { 101 sensor_back = 0; 102 } 103 } 104 } 105 { //Drive 106if (sensor_left ==1 or sensor_right==1) 107{ 108 Found: 109if (sensor_left == 1 && sensor_right == 1 or sensor_left == 1 && sensor_right == 1 && sensor_back == 0) 110 {//Straightforward 111 Straightforward: 112 motor1.setSpeed(150); 113 motor2.setSpeed(150); 114 motor3.setSpeed(150); 115 motor4.setSpeed(150); 116 motor1.run(FORWARD); 117 motor2.run(FORWARD); 118 motor3.run(FORWARD); 119 motor4.run(FORWARD); 120 delay(7); 121 motor1.run(RELEASE); 122 motor2.run(RELEASE); 123 motor3.run(RELEASE); 124 motor4.run(RELEASE); 125 Serial.print(" Straightforward "); 126 } 127 else if (sensor_left == 0 && sensor_right == 1) 128 {//Right Curve 129 motor1.setSpeed(225); 130 motor2.setSpeed(225); 131 motor3.setSpeed(200); 132 motor4.setSpeed(200); 133 motor1.run(BACKWARD); 134 motor2.run(BACKWARD); 135 motor3.run(FORWARD); 136 motor4.run(FORWARD); 137 delay(7); 138 motor1.run(RELEASE); 139 motor2.run(RELEASE); 140 motor3.run(RELEASE); 141 motor4.run(RELEASE); 142 Serial.print(" Right Curve "); 143 } 144 else if (sensor_left == 1 && sensor_right == 0) 145 {//Left Curve 146 motor1.setSpeed(200); 147 motor2.setSpeed(200); 148 motor3.setSpeed(225); 149 motor4.setSpeed(225); 150 motor1.run(FORWARD); 151 motor2.run(FORWARD); 152 motor3.run(BACKWARD); 153 motor4.run(BACKWARD); 154 delay(7); 155 motor1.run(RELEASE); 156 motor2.run(RELEASE); 157 motor3.run(RELEASE); 158 motor4.run(RELEASE); 159 Serial.print(" Left Curve "); 160 } 161 } 162 else if (sensor_left == 0 && sensor_right == 0 && sensor_back == 1) 163 { 164//Backward 165 motor1.setSpeed(160); 166 motor2.setSpeed(160); 167 motor3.setSpeed(150); 168 motor4.setSpeed(150); 169 motor1.run(BACKWARD); 170 motor2.run(BACKWARD); 171 motor3.run(BACKWARD); 172 motor4.run(BACKWARD); 173 delay (7); 174 Serial.print(" Backward "); 175 } 176 else 177 { 178 179 } 180 } 181} 182void color() 183 184{ 185 186 { //Left Sensor 187 digitalWrite(s2L, LOW); 188 digitalWrite(s3L, LOW); 189 redL = pulseIn(outL, digitalRead(outL) == HIGH ? LOW : HIGH); 190 digitalWrite(s3L, HIGH); 191 blueL = pulseIn(outL, digitalRead(outL) == HIGH ? LOW : HIGH); 192 digitalWrite(s2L, HIGH); 193 greenL = pulseIn(outL, digitalRead(outL) == HIGH ? LOW : HIGH); 194 } 195 196 { //Right Sensor 197 digitalWrite(s2R, LOW); 198 digitalWrite(s3R, LOW); 199 redR = pulseIn(outR, digitalRead(outR) == HIGH ? LOW : HIGH); 200 digitalWrite(s3R, HIGH); 201 blueR = pulseIn(outR, digitalRead(outR) == HIGH ? LOW : HIGH); 202 digitalWrite(s2R, HIGH); 203 greenR = pulseIn(outR, digitalRead(outR) == HIGH ? LOW : HIGH); 204 } 205 { //Back Sensor 206 digitalWrite(s2b, LOW); 207 digitalWrite(s3b, LOW); 208 redb = pulseIn(outb, digitalRead(outb) == HIGH ? LOW : HIGH); 209 digitalWrite(s3b, HIGH); 210 blueb = pulseIn(outb, digitalRead(outb) == HIGH ? LOW : HIGH); 211 digitalWrite(s2b, HIGH); 212 greenb = pulseIn(outb, digitalRead(outb) == HIGH ? LOW : HIGH); 213 } 214}
Downloadable files
Schema
Schema in a Fritzing format
Schema
Schema
Schema in a JPG
Schema

Schema
Schema in a Fritzing format
Schema
Comments
Only logged in users can leave comments