How to make Obstacle Avoiding Robot using Arduino !!

preview_player
Показать описание
In this video I have shown how to build an obstacle avoiding robot using Arduino Uno microcontroller. If an object comes ahead of it the robot stops, move back a little bit, look left/right and then it turns that side which has greater distance.

---------------------------------------
Components required

Video gear

----------------------------------------
if you liked the video then don't forget to subscribe my channel for more such content.


#diyprojects #robot #electronic #smartcars
Рекомендации по теме
Комментарии
Автор

//Code

#include <AFMotor.h> // libraray for motor shield
#include <Servo.h> // libraray for servo motor
#include <NewPing.h> // libraray for ultrasonic sensor

const int Trig_Pin =A0;
const int Echo_Pin =A1;
const int Max_Speed =200;
const int Max_Dist = 250;

NewPing ultra_sonic(Trig_Pin, Echo_Pin, Max_Dist);

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

Servo myservo;

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

void setup() {

myservo.attach(10);
myservo.write(0);
delay(2000);
distance=readDistance();
delay(100);
moveForward();
}

void loop() {
int dist_R = 0;
int dist_L = 0;
delay(100);

if(distance<=25){
moveStop();
delay(100);

moveBackward();
delay(200);

moveStop();
delay(100);

dist_R = right_Distance();
delay(100);
dist_L = left_Distance();
delay(100);

if(dist_R <= dist_L){
turnLeft();
moveStop();
}
else{
turnRight();
moveStop();
}
}
else{
moveForward();
}
distance = readDistance();

}

int left_Distance(){
myservo.write(170);
delay(500);
int dist = readDistance();
delay(100);
myservo.write(90);
return dist;
delay(100);
}

int right_Distance(){
myservo.write(10);
delay(500);
int dist = readDistance();
delay(100);
myservo.write(90);
return dist;
delay(100);
}

int readDistance(){
int cm = ultra_sonic.ping_cm();
if(cm<=0){ // this is done in case when sensor gives -ve value which is absurd
cm=250;
}
return cm;
}

void moveStop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}

void moveForward() {

if(!goesForward)
{
goesForward=true;
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
for (speedSet = 0; speedSet < Max_Speed; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
}
void moveBackward() {
goesForward=false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
for (speedSet = 0; speedSet < Max_Speed; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}

void turnRight() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}

void turnLeft() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}

prateekverma
Автор

Excelent video, thanks for sharing it!!

AlejandroTortolini
Автор

Great work, Can you share an online link to buy all the components? Thanks, COuld not find a few of the exact items.

rameshkarthikeyan
Автор

Bro I am noob no knowledge of this first project u see your video only pls tell what are libraries that you included in //code pls tell do we have to write them or just write after libraries section

sudheeshbehl
Автор

Literally saved my degree bro thanks a lot

kartikeysingh
Автор

M apka Ahsan Kabhi nhi bhulunga please sir 🙏🙏

VikashJatav-frnb
Автор

I want to use a H bridge instead of the arduino shield and an esp32 instead of using arduino. Will the code be the same after i changes the libraries?

LaribiRayen
Автор

what is the sticky stuff called you use to keep your micro servo and sensor in place?

christiangeraghty
Автор

Can I purchase the whole implementation from you ?

usmanbello
Автор

Can we use 9v battery ... please reply

kunalkalaskar
Автор

while coding the battery is connected or not ?

amansayyed
Автор

how to add extra male pin in motor shield

SillySquadron-sk
Автор

Why our motor driver got sparked when we connect to 7 volt bettery plzzz help

aashututorial
Автор

This is good, but you could have made this much better if you had shown each detail and connection. A little more time would have made this go from watching someone throw parts together to a follow each step with me and learn how and why I'm doing this. And lower the awful background music.

mikestrivia
Автор

Somehow the code doesn't work properly, there is always an AFmotor error message

RAVEGAMESTUDIO
Автор

My son did this for school project and the sensor is not working . Please help. Thank you.

tinsuneg
Автор

i am making this project but servo motor us not working and all four motor are rotating for only one second it get stop.What should i do

bilalpatel
Автор

There are showing an error " AFMotor such file or directory not found

anandaseal
Автор

Hi bro

Everything is fine I'm using 2 ×3.7 v cells but sensor is working but motor is not working please help

knightk
Автор

Awesome project... What's the budget?

AmitKumar-olky