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

Arduino Robot Navigation Code

The document contains an Arduino code for a robotic project that uses a servo motor and ultrasonic sensors for navigation. It defines functions for moving forward, backward, turning, and stopping based on distance measurements from obstacles. The robot adjusts its path by looking left and right to determine the best direction to avoid obstacles within a specified range.

Uploaded by

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

Arduino Robot Navigation Code

The document contains an Arduino code for a robotic project that uses a servo motor and ultrasonic sensors for navigation. It defines functions for moving forward, backward, turning, and stopping based on distance measurements from obstacles. The robot adjusts its path by looking left and right to determine the best direction to avoid obstacles within a specified range.

Uploaded by

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

Project Coding

#include <Servo.h>
#include <Wire.h>

#define R1 2
#define R2 3
#define L1 4
#define L2 5

#define trigPin 9
#define echoPin 10

#define servoPin 6
Servo myServo;

long duration;
int distance, distanceRight, distanceLeft;

void setup() {
// Set motor control pins as output
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);

// Set ultrasonic sensor pins


pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

// Attach the servo motor


[Link](servoPin);
[Link](120); // Set servo to forward position (90 degrees)

// Initialize serial for debugging


[Link](9600);

void loop() {
distance = getDistance();

if (distance > 0 && distance <= 60) {


moveStop();
delay(50);
moveBackward();
delay(1000);
moveStop();
distanceRight = lookRight();
distanceLeft = lookLeft();
delay(1000);

if (distanceRight > distanceLeft) {


turnRight();
} else {
turnLeft();
}

delay(1000);
moveStop();
} else {
moveForward();
}

delay(1000);
}

// Function to get the distance from the ultrasonic sensor


int getDistance() {
// Trigger the ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the echo time with a timeout of 20000 microseconds


duration = pulseIn(echoPin, HIGH, 20000); // Timeout in case of no echo

// If duration is zero, no echo was received


if (duration == 0) {
[Link]("No echo received");
return 0;
}

// Calculate the distance in cm


int distance = duration * 0.034 / 2;

// Print the distance to the Serial Monitor


[Link]("Distance: ");
[Link](distance);

return distance;
}
// Look to the right
int lookRight() {
[Link](0);
delay(1000);
int distance = getDistance();
[Link](90); // Reset to forward position
return distance;
}

// Look to the left


int lookLeft() {
[Link](180);
delay(1000);
int distance = getDistance();
[Link](90); // Reset to forward position
return distance;
}

// Move functions
void moveForward() {
digitalWrite(R1, HIGH);
digitalWrite(R2, LOW);
digitalWrite(L1, HIGH);
digitalWrite(L2, LOW);
}

void moveBackward() {
digitalWrite(R1, LOW);
digitalWrite(R2, HIGH);
digitalWrite(L1, LOW);
digitalWrite(L2, HIGH);
}

void turnRight() {
digitalWrite(R1, LOW);
digitalWrite(R2, HIGH);
digitalWrite(L1, HIGH);
digitalWrite(L2, LOW);
}

void turnLeft() {
digitalWrite(R1, HIGH);
digitalWrite(R2, LOW);
digitalWrite(L1, LOW);
digitalWrite(L2, HIGH);
}

void moveStop() {
digitalWrite(R1, LOW);
digitalWrite(R2, LOW);
digitalWrite(L1, LOW);
digitalWrite(L2, LOW);
}

You might also like