OBSTACLE AVOIDANCE CAR
The obstacle avoidance car is just basically which overcome the obsctacles comes in front it.It avoid obstacle with help of the ultrasonic sensor and changes the direction.
Components Required:
1)Arduino
2)Ultrasonic sensor
3)Motor Shield
4)Motors
5)Chassis kit
6)Connecting wires
Connection:
Ultrasonic = Arduino
+vcc = +5v
Gnd = Gnd
Trig = A1
Echo = A2
Light = A0
Procedure:
Insert Motor shield to Arduino and next connect the ultrasonic sensor as shown as per above connection and connect the led to A0 pin and next connect the Motors the Motor Shield.Now just copy the code and paste it on Arduino.The distance of ultrasonic could be defined.
delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
u can set as u want in 25 as your wish
Source code:
#include <AFMotor.h> //NBSL COMMERCIAL
#
#
#define trigPin A1
#define echoPin A2
AF_DCMotor motor1(1,MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
#define echoPin A2
AF_DCMotor motor1(1,MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
void setup() {
Serial.begin(9600); // begin serial communitication
Serial.println("Motor test!");
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
motor1.setSpeed(255); //set the speed of the motors, between 0-255
motor2.setSpeed (255);
}
Serial.begin(9600); // begin serial communitication
Serial.println("Motor test!");
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
motor1.setSpeed(255); //set the speed of the motors, between 0-255
motor2.setSpeed (255);
}
void loop()
{
pinMode(A0, OUTPUT);//for Led
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);
{
pinMode(A0, OUTPUT);//for Led
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
digitalWrite(A0, HIGH); //for Led
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.
Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
motor2.run(FORWARD);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
digitalWrite(A0, HIGH);
delay(50);
digitalWrite(A0, LOW);
delay(50);
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.
Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
motor2.run(FORWARD);
}
else {
Serial.println ("No obstacle detected. going forward");
delay (15);;
motor1.run(FORWARD); // Turn as long as there's an obstacle ahead.
motor2.run (FORWARD);
}
}
else {
Serial.println ("No obstacle detected. going forward");
delay (15);;
motor1.run(FORWARD); // Turn as long as there's an obstacle ahead.
motor2.run (FORWARD);
}
}
Comments
Post a Comment