0% found this document useful (0 votes)
50 views3 pages

CODE Robot Detect Obstacle

Uploaded by

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

CODE Robot Detect Obstacle

Uploaded by

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

[Link]

com/2023/11/12/obstacle-avoiding-robot-using-arduino-uno-and-l293d-with-hc-
sr04-sensor/

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

#define echopin A4 // echo pin


#define trigpin A5 // Trigger pin

Servo myservo;

const int MOTOR_1 = 1;


const int MOTOR_2 = 2;
const int MOTOR_3 = 3;
const int MOTOR_4 = 4;

AF_DCMotor motor1(MOTOR_1, MOTOR12_64KHZ); // create motor


object, 64KHz pwm
AF_DCMotor motor2(MOTOR_2, MOTOR12_64KHZ); // create motor
object, 64KHz pwm
AF_DCMotor motor3(MOTOR_3, MOTOR12_64KHZ); // create motor
object, 64KHz pwm
AF_DCMotor motor4(MOTOR_4, MOTOR12_64KHZ); // create motor
object, 64KHz pwm
//
=================================================================
==============
// Initialization
//
=================================================================
==============

int distance_L, distance_F, distance_R;


long distance;

int set = 20;

void setup() {
[Link](9600); // Initialize serial port
[Link]("Start");

[Link](10);
[Link](90);

pinMode (trigpin, OUTPUT);


pinMode (echopin, INPUT );

[Link](180); // set the motor speed to 0-255


[Link](180);
[Link](180);
[Link](180);
}
//
=================================================================
==============
// Main
//
=================================================================
==============
void loop() {
distance_F = data();
[Link]("S=");
[Link](distance_F);
if (distance_F > set){
[Link]("Forward");
[Link](FORWARD); // turn it on going forward
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
}
else{hc_sr4();}
}

long data(){
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
distance = pulseIn (echopin, HIGH);
return distance / 29 / 2;
}

void compareDistance(){
if (distance_L > distance_R){
[Link](BACKWARD); // turn it on going left
[Link](BACKWARD);
[Link](FORWARD);
[Link](FORWARD);
delay(350);
}
else if (distance_R > distance_L){
[Link](FORWARD); // the other right
[Link](FORWARD);
[Link](BACKWARD);
[Link](BACKWARD);
delay(350);
}
else{
[Link](BACKWARD); // the other way
[Link](BACKWARD);
[Link](BACKWARD);
[Link](BACKWARD);
delay(300);
[Link](BACKWARD); // turn it on going left
[Link](BACKWARD);
[Link](FORWARD);
[Link](FORWARD);
delay(500);
}
}

void hc_sr4(){
[Link]("Stop");
[Link](RELEASE); // stopped
[Link](RELEASE);
[Link](RELEASE);
[Link](RELEASE);

[Link](0);
delay(300);
distance_R = data();
delay(100);
[Link](170);
delay(500);
distance_L = data();
delay(100);
[Link](90);
delay(300);
compareDistance();
}

You might also like