Components and supplies
Ultrasonic Sensor - HC-SR04 (Generic)
DC motor (generic)
Adafruit Motor Driver
SG90 Micro-servo motor
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
ARDUINO_OBSTACLE_AVOIDING_CAR.ino
c_cpp
1//ARDUINO OBSTACLE AVOIDING CAR// 2// Before uploading the code you have to install the necessary library// 3//AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library//-install // 4//NewPing Library https://github.com/livetronic/Arduino-NewPing// 5//Servo Library https://github.com/arduino-libraries/Servo.git // 6// To Install the libraries go to sketch >> Include Library >> ManageLibrary 7//search and download library 8 9 10#include <AFMotor.h> 11#include <NewPing.h> 12#include <Servo.h> 13 14#define TRIG_PIN A0 15#define ECHO_PIN A1 16#define MAX_DISTANCE 200 17#define MAX_SPEED 190 // sets speed of DC motors 18#define MAX_SPEED_OFFSET 20 19 20NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 21 22AF_DCMotor motor1(1, MOTOR12_1KHZ); 23AF_DCMotor motor2(2, MOTOR12_1KHZ); 24AF_DCMotor motor3(3, MOTOR34_1KHZ); 25AF_DCMotor motor4(4, MOTOR34_1KHZ); 26Servo myservo; 27 28boolean goesForward=false; 29int distance = 100; 30int speedSet = 0; 31 32void setup() { 33 34 myservo.attach(10); 35 myservo.write(115); 36 delay(2000); 37 distance = readPing(); 38 delay(100); 39 distance = readPing(); 40 delay(100); 41 distance = readPing(); 42 delay(100); 43 distance = readPing(); 44 delay(100); 45} 46 47void loop() { 48 int distanceR = 0; 49 int distanceL = 0; 50 delay(40); 51 52 if(distance<=15) 53 { 54 moveStop(); 55 delay(100); 56 moveBackward(); 57 delay(300); 58 moveStop(); 59 delay(200); 60 distanceR = lookRight(); 61 delay(200); 62 distanceL = lookLeft(); 63 delay(200); 64 65 if(distanceR>=distanceL) 66 { 67 turnRight(); 68 moveStop(); 69 }else 70 { 71 turnLeft(); 72 moveStop(); 73 } 74 }else 75 { 76 moveForward(); 77 } 78 distance = readPing(); 79} 80 81int lookRight() 82{ 83 myservo.write(50); 84 delay(500); 85 int distance = readPing(); 86 delay(100); 87 myservo.write(115); 88 return distance; 89} 90 91int lookLeft() 92{ 93 myservo.write(170); 94 delay(500); 95 int distance = readPing(); 96 delay(100); 97 myservo.write(115); 98 return distance; 99 delay(100); 100} 101 102int readPing() { 103 delay(70); 104 int cm = sonar.ping_cm(); 105 if(cm==0) 106 { 107 cm = 250; 108 } 109 return cm; 110} 111 112void moveStop() { 113 motor1.run(RELEASE); 114 motor2.run(RELEASE); 115 motor3.run(RELEASE); 116 motor4.run(RELEASE); 117 } 118 119void moveForward() { 120 121 if(!goesForward) 122 { 123 goesForward=true; 124 motor1.run(FORWARD); 125 motor2.run(FORWARD); 126 motor3.run(FORWARD); 127 motor4.run(FORWARD); 128 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly 129 { 130 motor1.setSpeed(speedSet); 131 motor2.setSpeed(speedSet); 132 motor3.setSpeed(speedSet); 133 motor4.setSpeed(speedSet); 134 delay(5); 135 } 136 } 137} 138 139void moveBackward() { 140 goesForward=false; 141 motor1.run(BACKWARD); 142 motor2.run(BACKWARD); 143 motor3.run(BACKWARD); 144 motor4.run(BACKWARD); 145 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly 146 { 147 motor1.setSpeed(speedSet); 148 motor2.setSpeed(speedSet); 149 motor3.setSpeed(speedSet); 150 motor4.setSpeed(speedSet); 151 delay(5); 152 } 153} 154 155void turnRight() { 156 motor1.run(FORWARD); 157 motor2.run(FORWARD); 158 motor3.run(BACKWARD); 159 motor4.run(BACKWARD); 160 delay(500); 161 motor1.run(FORWARD); 162 motor2.run(FORWARD); 163 motor3.run(FORWARD); 164 motor4.run(FORWARD); 165} 166 167void turnLeft() { 168 motor1.run(BACKWARD); 169 motor2.run(BACKWARD); 170 motor3.run(FORWARD); 171 motor4.run(FORWARD); 172 delay(500); 173 motor1.run(FORWARD); 174 motor2.run(FORWARD); 175 motor3.run(FORWARD); 176 motor4.run(FORWARD); 177} 178
ARDUINO_OBSTACLE_AVOIDING_CAR.ino
c_cpp
1//ARDUINO OBSTACLE AVOIDING CAR// 2// Before uploading the code you 3 have to install the necessary library// 4//AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library//-install 5 // 6//NewPing Library https://github.com/livetronic/Arduino-NewPing// 7//Servo 8 Library https://github.com/arduino-libraries/Servo.git // 9// To Install the libraries 10 go to sketch >> Include Library >> ManageLibrary 11//search and download library 12 13 14#include 15 <AFMotor.h> 16#include <NewPing.h> 17#include <Servo.h> 18 19#define TRIG_PIN 20 A0 21#define ECHO_PIN A1 22#define MAX_DISTANCE 200 23#define MAX_SPEED 190 24 // sets speed of DC motors 25#define MAX_SPEED_OFFSET 20 26 27NewPing sonar(TRIG_PIN, 28 ECHO_PIN, MAX_DISTANCE); 29 30AF_DCMotor motor1(1, MOTOR12_1KHZ); 31AF_DCMotor 32 motor2(2, MOTOR12_1KHZ); 33AF_DCMotor motor3(3, MOTOR34_1KHZ); 34AF_DCMotor motor4(4, 35 MOTOR34_1KHZ); 36Servo myservo; 37 38boolean goesForward=false; 39int distance 40 = 100; 41int speedSet = 0; 42 43void setup() { 44 45 myservo.attach(10); 46 47 myservo.write(115); 48 delay(2000); 49 distance = readPing(); 50 delay(100); 51 52 distance = readPing(); 53 delay(100); 54 distance = readPing(); 55 delay(100); 56 57 distance = readPing(); 58 delay(100); 59} 60 61void loop() { 62 int distanceR 63 = 0; 64 int distanceL = 0; 65 delay(40); 66 67 if(distance<=15) 68 { 69 70 moveStop(); 71 delay(100); 72 moveBackward(); 73 delay(300); 74 moveStop(); 75 76 delay(200); 77 distanceR = lookRight(); 78 delay(200); 79 distanceL = lookLeft(); 80 81 delay(200); 82 83 if(distanceR>=distanceL) 84 { 85 turnRight(); 86 moveStop(); 87 88 }else 89 { 90 turnLeft(); 91 moveStop(); 92 } 93 }else 94 { 95 96 moveForward(); 97 } 98 distance = readPing(); 99} 100 101int lookRight() 102{ 103 104 myservo.write(50); 105 delay(500); 106 int distance = readPing(); 107 108 delay(100); 109 myservo.write(115); 110 return distance; 111} 112 113int 114 lookLeft() 115{ 116 myservo.write(170); 117 delay(500); 118 int distance 119 = readPing(); 120 delay(100); 121 myservo.write(115); 122 return distance; 123 124 delay(100); 125} 126 127int readPing() { 128 delay(70); 129 int cm = sonar.ping_cm(); 130 131 if(cm==0) 132 { 133 cm = 250; 134 } 135 return cm; 136} 137 138void moveStop() 139 { 140 motor1.run(RELEASE); 141 motor2.run(RELEASE); 142 motor3.run(RELEASE); 143 144 motor4.run(RELEASE); 145 } 146 147void moveForward() { 148 149 if(!goesForward) 150 151 { 152 goesForward=true; 153 motor1.run(FORWARD); 154 motor2.run(FORWARD); 155 156 motor3.run(FORWARD); 157 motor4.run(FORWARD); 158 for (speedSet = 159 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading 160 down the batteries too quickly 161 { 162 motor1.setSpeed(speedSet); 163 motor2.setSpeed(speedSet); 164 165 motor3.setSpeed(speedSet); 166 motor4.setSpeed(speedSet); 167 delay(5); 168 169 } 170 } 171} 172 173void moveBackward() { 174 goesForward=false; 175 motor1.run(BACKWARD); 176 177 motor2.run(BACKWARD); 178 motor3.run(BACKWARD); 179 motor4.run(BACKWARD); 180 181 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the 182 speed up to avoid loading down the batteries too quickly 183 { 184 motor1.setSpeed(speedSet); 185 186 motor2.setSpeed(speedSet); 187 motor3.setSpeed(speedSet); 188 motor4.setSpeed(speedSet); 189 190 delay(5); 191 } 192} 193 194void turnRight() { 195 motor1.run(FORWARD); 196 197 motor2.run(FORWARD); 198 motor3.run(BACKWARD); 199 motor4.run(BACKWARD); 200 201 delay(500); 202 motor1.run(FORWARD); 203 motor2.run(FORWARD); 204 motor3.run(FORWARD); 205 206 motor4.run(FORWARD); 207} 208 209void turnLeft() { 210 motor1.run(BACKWARD); 211 212 motor2.run(BACKWARD); 213 motor3.run(FORWARD); 214 motor4.run(FORWARD); 215 216 delay(500); 217 motor1.run(FORWARD); 218 motor2.run(FORWARD); 219 220 motor3.run(FORWARD); 221 motor4.run(FORWARD); 222} 223
Downloadable files
Circuit
Circuit
Comments
Only logged in users can leave comments
parushbansal007
0 Followers
•0 Projects
Table of contents
Intro
2
0