Components and supplies
Dual H-Bridge motor drivers L298
HC-05 Bluetooth Module
Arduino UNO
DC Motor, 12 V
Jumper wires (generic)
Breadboard (generic)
Battery, 12 V
Ultrasonic Sensor - HC-SR04 (Generic)
Tools and machines
Mastech MS8217 Autorange Digital Multimeter
Soldering iron (generic)
Apps and platforms
MIT App Inventor
Arduino IDE
Project description
Code
CODE
c_cpp
1const int trigPin1 = 6;//front 2const int echoPin1 = 7; 3const int 4 trigPin2 = 10; //left 5const int echoPin2 = 11; 6const int trigPin3 = 8;//right 7const 8 int echoPin3 = 9; 9const int in1 = 2; 10const int in2 = 3; 11const int in3 = 12 4; 13const int in4 = 5; 14 15#define DIS 7 16 17void setup() 18{ 19pinMode(trigPin1, 20 OUTPUT); 21pinMode(echoPin1, INPUT); 22pinMode(trigPin2, OUTPUT); 23pinMode(echoPin2, 24 INPUT); 25pinMode(trigPin3, OUTPUT); 26pinMode(echoPin3, INPUT); 27pinMode (in1, 28 OUTPUT); 29pinMode (in2, OUTPUT); 30pinMode (in3, OUTPUT); 31pinMode (in4, OUTPUT); 32 33} 34void 35 loop() 36{ 37 if (FrontSensor ()>DIS && RightSensor () >DIS && LeftSensor () 38 >DIS) 39 {forward();} 40 //else if ( FrontSensor () > DIS && RightSensor () < 41 DIS && LeftSensor ()< DIS) 42//{ 43 //forward(); 44//} 45else if ( FrontSensor() 46 < DIS && RightSensor () <DIS && LeftSensor ()<DIS) // obstacle infront of all 3 47 sides 48{ 49reverse (); 50delay(500); 51if((LeftSensor())>(RightSensor()) ) 52turn_left(); 53else 54turn_right(); 55//then 56 reverse 57} 58else if (FrontSensor() <DIS && RightSensor () <DIS && LeftSensor 59 ()>DIS) // obstacle on right and front sides 60{ 61turn_left (); // turn left 62 side 63} 64else if (FrontSensor() <DIS && RightSensor () >DIS && LeftSensor ()<DIS) 65 // obstacle on left and front sides 66{ 67turn_right (); // turn right side 68} 69else 70 if (FrontSensor() <DIS && RightSensor () >DIS && LeftSensor ()>DIS) // obstacle 71 on front sides 72{ 73turn_left (); 74delay(500); 75forward();// then turn right 76 //******************** 77} 78else if (FrontSensor() >DIS && RightSensor () >DIS 79 && LeftSensor ()<DIS) // obstacle on left sides 80{ 81turn_right(); // then turn 82 right and then forward 83delay(500); 84forward(); 85} 86else if (FrontSensor() 87 >DIS && RightSensor () <DIS && LeftSensor ()>DIS) // obstacle on right sides 88{ 89turn_left 90 (); // then turn left and then right 91delay(500); 92forward(); 93} 94else 95{ 96forward(); 97} 98} 99void 100 forward () 101{ 102digitalWrite(in1, HIGH); 103digitalWrite(in2, LOW); 104digitalWrite(in3, 105 HIGH); 106digitalWrite(in4, LOW); 107} 108void turn_left () 109{ 110digitalWrite(in1, 111 HIGH); 112digitalWrite(in2, LOW); 113digitalWrite(in3, LOW); 114digitalWrite(in4, 115 HIGH); 116 117} 118void turn_right () 119{ 120digitalWrite(in1, LOW); 121digitalWrite(in2, 122 HIGH); 123digitalWrite(in3, HIGH); 124digitalWrite(in4, LOW); 125 126} 127void reverse 128 () 129{ 130digitalWrite(in1, LOW); 131digitalWrite(in2, HIGH); 132digitalWrite(in3, 133 LOW); 134digitalWrite(in4, HIGH); 135 136} 137void stop() 138{ 139digitalWrite(in1, 140 LOW); 141digitalWrite(in2, LOW); 142digitalWrite(in3, LOW); 143digitalWrite(in4, 144 LOW); 145 146} 147long FrontSensor () 148{ 149long dur; 150digitalWrite(trigPin1, 151 LOW); 152delayMicroseconds(2); // delays are required for a succesful sensor operation. 153digitalWrite(trigPin1, 154 HIGH); 155delayMicroseconds(10); //this delay is required as well! 156digitalWrite(trigPin1, 157 LOW); 158dur = pulseIn(echoPin1, HIGH); 159return (dur/58);// convert the distance 160 to centimeters. 161} 162 163long RightSensor () 164{ 165long dur; 166digitalWrite(trigPin2, 167 LOW); 168delayMicroseconds(2); // delays are required for a succesful sensor operation. 169digitalWrite(trigPin2, 170 HIGH); 171delayMicroseconds(10); //this delay is required as well! 172digitalWrite(trigPin2, 173 LOW); 174dur = pulseIn(echoPin2, HIGH); 175return (dur/58);// convert the distance 176 to centimeters. 177} 178long LeftSensor () 179{ 180long dur; 181digitalWrite(trigPin3, 182 LOW); 183delayMicroseconds(2); // delays are required for a succesful sensor operation. 184digitalWrite(trigPin3, 185 HIGH); 186delayMicroseconds(10); //this delay is required as well! 187digitalWrite(trigPin3, 188 LOW); 189dur = pulseIn(echoPin3, HIGH); 190return (dur/58);// convert the distance 191 to centimeters. 192}
CODE
c_cpp
1const int trigPin1 = 6;//front 2const int echoPin1 = 7; 3const int trigPin2 = 10; //left 4const int echoPin2 = 11; 5const int trigPin3 = 8;//right 6const int echoPin3 = 9; 7const int in1 = 2; 8const int in2 = 3; 9const int in3 = 4; 10const int in4 = 5; 11 12#define DIS 7 13 14void setup() 15{ 16pinMode(trigPin1, OUTPUT); 17pinMode(echoPin1, INPUT); 18pinMode(trigPin2, OUTPUT); 19pinMode(echoPin2, INPUT); 20pinMode(trigPin3, OUTPUT); 21pinMode(echoPin3, INPUT); 22pinMode (in1, OUTPUT); 23pinMode (in2, OUTPUT); 24pinMode (in3, OUTPUT); 25pinMode (in4, OUTPUT); 26 27} 28void loop() 29{ 30 if (FrontSensor ()>DIS && RightSensor () >DIS && LeftSensor () >DIS) 31 {forward();} 32 //else if ( FrontSensor () > DIS && RightSensor () < DIS && LeftSensor ()< DIS) 33//{ 34 //forward(); 35//} 36else if ( FrontSensor() < DIS && RightSensor () <DIS && LeftSensor ()<DIS) // obstacle infront of all 3 sides 37{ 38reverse (); 39delay(500); 40if((LeftSensor())>(RightSensor()) ) 41turn_left(); 42else 43turn_right(); 44//then reverse 45} 46else if (FrontSensor() <DIS && RightSensor () <DIS && LeftSensor ()>DIS) // obstacle on right and front sides 47{ 48turn_left (); // turn left side 49} 50else if (FrontSensor() <DIS && RightSensor () >DIS && LeftSensor ()<DIS) // obstacle on left and front sides 51{ 52turn_right (); // turn right side 53} 54else if (FrontSensor() <DIS && RightSensor () >DIS && LeftSensor ()>DIS) // obstacle on front sides 55{ 56turn_left (); 57delay(500); 58forward();// then turn right //******************** 59} 60else if (FrontSensor() >DIS && RightSensor () >DIS && LeftSensor ()<DIS) // obstacle on left sides 61{ 62turn_right(); // then turn right and then forward 63delay(500); 64forward(); 65} 66else if (FrontSensor() >DIS && RightSensor () <DIS && LeftSensor ()>DIS) // obstacle on right sides 67{ 68turn_left (); // then turn left and then right 69delay(500); 70forward(); 71} 72else 73{ 74forward(); 75} 76} 77void forward () 78{ 79digitalWrite(in1, HIGH); 80digitalWrite(in2, LOW); 81digitalWrite(in3, HIGH); 82digitalWrite(in4, LOW); 83} 84void turn_left () 85{ 86digitalWrite(in1, HIGH); 87digitalWrite(in2, LOW); 88digitalWrite(in3, LOW); 89digitalWrite(in4, HIGH); 90 91} 92void turn_right () 93{ 94digitalWrite(in1, LOW); 95digitalWrite(in2, HIGH); 96digitalWrite(in3, HIGH); 97digitalWrite(in4, LOW); 98 99} 100void reverse () 101{ 102digitalWrite(in1, LOW); 103digitalWrite(in2, HIGH); 104digitalWrite(in3, LOW); 105digitalWrite(in4, HIGH); 106 107} 108void stop() 109{ 110digitalWrite(in1, LOW); 111digitalWrite(in2, LOW); 112digitalWrite(in3, LOW); 113digitalWrite(in4, LOW); 114 115} 116long FrontSensor () 117{ 118long dur; 119digitalWrite(trigPin1, LOW); 120delayMicroseconds(2); // delays are required for a succesful sensor operation. 121digitalWrite(trigPin1, HIGH); 122delayMicroseconds(10); //this delay is required as well! 123digitalWrite(trigPin1, LOW); 124dur = pulseIn(echoPin1, HIGH); 125return (dur/58);// convert the distance to centimeters. 126} 127 128long RightSensor () 129{ 130long dur; 131digitalWrite(trigPin2, LOW); 132delayMicroseconds(2); // delays are required for a succesful sensor operation. 133digitalWrite(trigPin2, HIGH); 134delayMicroseconds(10); //this delay is required as well! 135digitalWrite(trigPin2, LOW); 136dur = pulseIn(echoPin2, HIGH); 137return (dur/58);// convert the distance to centimeters. 138} 139long LeftSensor () 140{ 141long dur; 142digitalWrite(trigPin3, LOW); 143delayMicroseconds(2); // delays are required for a succesful sensor operation. 144digitalWrite(trigPin3, HIGH); 145delayMicroseconds(10); //this delay is required as well! 146digitalWrite(trigPin3, LOW); 147dur = pulseIn(echoPin3, HIGH); 148return (dur/58);// convert the distance to centimeters. 149}
Downloadable files
Circuit Diagram
Circuit Diagram
Comments
Only logged in users can leave comments