0% found this document useful (0 votes)
19 views4 pages

Arduino Obstacle Avoiding Car Code

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Arduino Obstacle Avoiding Car Code

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//ARDUINO OBSTACLE AVOIDING CAR Video - [Link]

be/h-B42_HXL00//
// Before uploading the code you have to install the necessary library//
//AFMotor Library [Link]
install //
//NewPing Library [Link] //
//Servo Library [Link] //

#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>

#define TRIG_PIN A0
#define ECHO_PIN A1
#define MAX_DISTANCE 100
#define MAX_SPEED 150 // sets speed of DC motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

AF_DCMotor motor1(1, MOTOR12_64KHZ);


AF_DCMotor motor2(2, MOTOR12_64KHZ);
AF_DCMotor motor3(3, MOTOR34_64KHZ);
AF_DCMotor motor4(4, MOTOR34_64KHZ);
Servo myservo;

boolean goesForward=false;
int distance = 100;
int speedSet = 0;

void setup() {

[Link](10);
[Link](115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}

void loop() {
int distanceR = 0;
int distanceL = 0;
delay(40);

if(distance<=25)
{
moveStop();
delay(100);
moveBackward();
delay(200);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);

if(distanceR>=distanceL)
{
turnRight();
moveStop();
}

else

{
turnLeft();
moveStop();
}
}

else
{
moveForward();
}
distance = readPing();
}

int lookRight()
{
[Link](50);
delay(500);
int distance = readPing();
delay(100);
[Link](115);
return distance;
}

int lookLeft()
{
[Link](170);
delay(500);
int distance = readPing();
delay(100);
[Link](115);
return distance;
delay(100);
}

int readPing() {
delay(100);
int cm = sonar.ping_cm();
if(cm==0)
{
cm = 250;
}
return cm;
}

void moveStop() {
[Link](RELEASE);
[Link](RELEASE);
[Link](RELEASE);
[Link](RELEASE);
}

void moveForward() {

if(!goesForward)
{
goesForward=true;
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed
up to avoid loading down the batteries too quickly
{
[Link](speedSet);
[Link](speedSet);
[Link](speedSet);
[Link](speedSet);
delay(5);
}
}
}

void moveBackward() {
goesForward=false;
[Link](BACKWARD);
[Link](BACKWARD);
[Link](BACKWARD);
[Link](BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed
up to avoid loading down the batteries too quickly
{
[Link](speedSet);
[Link](speedSet);
[Link](speedSet);
[Link](speedSet);
delay(5);
}
}

void turnRight() {
[Link](FORWARD);
[Link](FORWARD);
[Link](BACKWARD);
[Link](BACKWARD);
delay(500);
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
}

void turnLeft() {
[Link](BACKWARD);
[Link](BACKWARD);
[Link](FORWARD);
[Link](FORWARD);
delay(500);
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
}

Common questions

Powered by AI

The Arduino obstacle avoiding car uses a servo motor to rotate the ultrasonic sensor to scan distances on both the right and left sides. When an obstacle is detected within 25 cm, the algorithm first stops the car, moves it backward briefly, and then scans the right and left directions by turning the servo to 50 degrees for right and 170 degrees for left. It calculates the distance to objects using the ultrasonic sensor and NewPing library. The car then turns to the side with the greater measured distance, either right or left, based on these measurements .

You might also like