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

avoiding

The document contains Arduino code for controlling a robot's movement using servo motors and ultrasonic sensors. It defines functions for moving forward, backward, turning, and stopping based on distance measurements. The code utilizes the NewPing library to read distances and make decisions on movement direction to avoid obstacles.

Uploaded by

rendy pamungkas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

avoiding

The document contains Arduino code for controlling a robot's movement using servo motors and ultrasonic sensors. It defines functions for moving forward, backward, turning, and stopping based on distance measurements. The code utilizes the NewPing library to read distances and make decisions on movement direction to avoid obstacles.

Uploaded by

rendy pamungkas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Servo.

h> int distanceLeft = 0;

#include <NewPing.h> delay(50);

#define trig_pin A1 if (distance <= 30){

#define echo_pin A2 moveStop();

#define maximum_distance 200 delay(300);

const int LeftMotorForward = 7; moveBackward();

const int LeftMotorBackward = 6; delay(200);

const int RightMotorForward = 5; moveStop();

const int RightMotorBackward = 4; delay(300);

boolean goesForward = false; distanceRight = lookRight();

int distance = 100; delay(200);

NewPing sonar(trig_pin, echo_pin, distanceLeft = lookLeft();


maximum_distance);
delay(200);
Servo servo_motor;
if (distance >= distanceLeft){
void setup(){
turnRight();
pinMode(RightMotorForward, OUTPUT);
moveStop();
pinMode(LeftMotorForward, OUTPUT);
}
pinMode(LeftMotorBackward, OUTPUT);
else{
pinMode(RightMotorBackward, OUTPUT);
turnLeft();
servo_motor.attach(10);
moveStop();
servo_motor.write(115);
}}
delay(2000);
else{
distance = readPing();
moveForward();
delay(100);
}
distance = readPing();
distance = readPing();
delay(100);
}
distance = readPing();
int lookRight(){
delay(100);
servo_motor.write(50);
distance = readPing();
delay(500);
delay(100);
int distance = readPing();
}
delay(100);
void loop(){
servo_motor.write(115);
int distanceRight = 0;
return distance;

1
} goesForward=false;

int lookLeft(){ digitalWrite(LeftMotorBackward, HIGH);

servo_motor.write(170); digitalWrite(RightMotorBackward, HIGH);

delay(500); digitalWrite(LeftMotorForward, LOW);

int distance = readPing(); digitalWrite(RightMotorForward, LOW);

delay(100); }

servo_motor.write(115); void turnRight(){

return distance; digitalWrite(LeftMotorForward, HIGH);

delay(100); digitalWrite(RightMotorBackward, HIGH);

} digitalWrite(LeftMotorBackward, LOW);

int readPing(){ digitalWrite(RightMotorForward, LOW);

delay(70); delay(150);

int cm = sonar.ping_cm(); digitalWrite(LeftMotorForward, HIGH);

if (cm==0){ digitalWrite(RightMotorForward, HIGH);

cm=250; digitalWrite(LeftMotorBackward, LOW);

} digitalWrite(RightMotorBackward, LOW);

return cm; }

} void turnLeft(){

void moveStop(){ digitalWrite(LeftMotorBackward, HIGH);

digitalWrite(RightMotorForward, LOW); digitalWrite(RightMotorForward, HIGH);

digitalWrite(LeftMotorForward, LOW); digitalWrite(LeftMotorForward, LOW);

digitalWrite(RightMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW);

digitalWrite(LeftMotorBackward, LOW); delay(150);

} digitalWrite(LeftMotorForward, HIGH);

void moveForward(){ digitalWrite(RightMotorForward, HIGH);

if(!goesForward){ digitalWrite(LeftMotorBackward, LOW);

goesForward=true; digitalWrite(RightMotorBackward, LOW);

digitalWrite(LeftMotorForward, HIGH); }

digitalWrite(RightMotorForward, HIGH);

digitalWrite(LeftMotorBackward, LOW);

digitalWrite(RightMotorBackward, LOW);

}}

void moveBackward(){

You might also like