0% found this document useful (0 votes)
12 views

Update Code (1)

This document contains an Arduino sketch for controlling a robot with motors and a servo for obstacle avoidance. It uses a sonar sensor to measure distance and make decisions to move forward, backward, or turn based on detected obstacles. The code includes functions for motor control, distance reading, and adjusting the servo position to scan for obstacles on the left and right.
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)
12 views

Update Code (1)

This document contains an Arduino sketch for controlling a robot with motors and a servo for obstacle avoidance. It uses a sonar sensor to measure distance and make decisions to move forward, backward, or turn based on detected obstacles. The code includes functions for motor control, distance reading, and adjusting the servo position to scan for obstacles on the left and right.
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
You are on page 1/ 3

#include <Servo.

h>
#include <NewPing.h>

const int LeftMotorForward = 5;


const int LeftMotorBackward = 4;
const int RightMotorForward = 3;
const int RightMotorBackward = 2;

#define trig_pin 7 // Changed to digital pin 7


#define echo_pin 8 // Changed to digital pin 8

#define maximum_distance 200


boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance);
Servo servo_motor;

void setup() {
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);

servo_motor.attach(11);
servo_motor.write(90);
delay(2000);

// Initialize distance reading with retries


for (int i = 0; i < 4; i++) {
distance = readPing();
delay(100);
}
}

void loop() {
int distanceRight = 0;
int distanceLeft = 0;
delay(50);

if (distance <= 20) {


moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);

distanceRight = lookRight();
delay(300);
distanceLeft = lookLeft();
delay(300);

if (distanceRight >= distanceLeft) {


turnRight();
} else {
turnLeft();
}
moveStop();
delay(200);
} else {
moveForward();
}

distance = readPing();
}

int lookRight() {
servo_motor.write(30); // Adjusted from 10 to 30
delay(500);
int dist = readPing();
delay(100);
servo_motor.write(90);
return dist;
}

int lookLeft() {
servo_motor.write(150); // Adjusted from 170 to 150
delay(500);
int dist = readPing();
delay(100);
servo_motor.write(90);
return dist;
}

int readPing() {
delay(70);
int cm = sonar.ping_cm();
if (cm == 0) {
cm = 200; // Adjusted default value from 250 to 200
}
return cm;
}

void moveStop() {
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}

void moveForward() {
if (!goesForward) {
goesForward = true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}

void moveBackward() {
goesForward = false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
void turnRight() {
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(500);
moveStop();
delay(200);
}

void turnLeft() {
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(500);
moveStop();
delay(200);
}

You might also like