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

Arduino Robot with Ultrasonic Sensor

MS

Uploaded by

surya murali
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)
43 views4 pages

Arduino Robot with Ultrasonic Sensor

MS

Uploaded by

surya murali
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

//please subscribe our youtube channel for more projects

[Link]

#include <NewPing.h> //Ultrasonic sensor function library. You must install


this library
#include <Servo.h> //Servo motor library. This is standard library

const int LeftMotorForward = 2;


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

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 200


boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function


Servo servo_motor; //our servo name

void setup(){

pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);

servo_motor.attach(8); //our servo pin

servo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
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 (distance >= distanceLeft){


turnRight();
moveStop();
}
else{
turnLeft();
moveStop();
}
}
else{
moveForward();
}
distance = readPing();
}

int lookRight(){
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}

int lookLeft(){
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
delay(100);
}

int readPing(){
delay(70);
int cm = sonar.ping_cm();
if (cm==0){
cm=250;
}
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);

digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);

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

void turnLeft(){

digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);

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

delay(500);

digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);

digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}

Common questions

Powered by AI

The turning functions turnRight() and turnLeft() control the motor by setting appropriate HIGH/LOW signals on motor pins to achieve the desired directional turn. To turn right, the left motor is moved forward and the backward motion is adjusted accordingly, while for turning left, the opposite occurs. Each turn includes a delay to ensure adequate rotation before moving forward again. These carefully timed actions allow the robot to pivot effectively to avoid obstacles, contributing significantly to accurate and efficient navigation .

Not setting a maximum distance in the NewPing object could lead to inefficient sensor operation, causing delayed response times in reading echoes or incorrect distance measurements due to ambiguous echoes. This inefficiency might result in the robot failing to react promptly to obstacles or miscalculating distances, potentially leading to collisions. Defining a maximum distance limits echo detection to manageable, relevant distances, allowing for quick, reliable readings essential for real-time navigation .

The repeated calls to readPing() during the setup phase serve to calibrate the ultrasonic sensor and stabilize the initial distance readings. By taking multiple measurements in short succession, any outlier readings can be averaged out or corrected, ensuring the robot starts with a reliable and stable distance measurement. This step helps in dealing with transient electronic noise or initial calibration errors when initializing the setup .

The servo motor is set to a default position of 115 degrees at the start of the setup function to establish a reference or neutral position for the ultrasonic sensor to collect accurate distance data. This positioning ensures that the sensor's readings are consistent as it moves between left and right measurements, providing a reliable starting point for making navigation decisions .

The NewPing library is used to handle ultrasonic sensors in the robotic system, providing functions that facilitate distance measurements using the ping method. It simplifies the process of sending pulses from the sensor and receiving echo pulse duration to calculate distance, crucial for navigation decisions and avoiding obstacles. This library is essential because it abstracts away low-level processing of pulse measurements, thereby simplifying the implementation and ensuring reliable readings .

Delay mechanisms within the loop function are crucial because they ensure adequate time for the sensors to stabilize and provide accurate readings, while also allowing motors the necessary period to complete their actions without premature interruptions. Delays facilitate synchronous operation between sensors and motors, preventing erratic movements or missteps due to hasty actions. This coordination is essential for achieving smooth and controlled navigation paths .

The robot's logic predominantly relies on simple distance comparison using a single ultrasonic sensor, which may not capture complex environments or multiple obstacles effectively. The binary decision-making process could result in suboptimal navigation, such as failed escape from dead-ends or inadequate response in dynamic environments. Furthermore, varying surfaces and ambient conditions might affect the consistency of distance readings, leading to erratic behavior if not accounted for by more sophisticated algorithms or additional sensors .

The goesForward boolean variable acts as a state indicator, determining whether the robot is currently moving forward. It prevents redundant motor commands by ensuring that moveForward() is only executed when goesForward is false, thus not moving or changing the motor states unnecessarily. This reduces wear on the hardware and improves efficient control, as it only initiates forward movement from a stop or reverse state .

The robot determines when to stop by checking if the distance from an obstacle is less than or equal to 20 cm using the ultrasonic sensor. Upon detecting an obstacle within this range, it executes the moveStop(), delay, moveBackward(), another delay, and moveStop() methods sequentially to halt and back away. It then assesses the distances on its right and left using the lookRight() and lookLeft() functions deciding which direction to turn based on the comparison of these distances .

After encountering an obstacle, the robot uses a simple decision-making logic based on comparing distances on its left and right. It stops, moves backward briefly, then checks the distance to the right using lookRight(), restores to the initial position, and checks the distance to the left with lookLeft(). If the current distance is greater than or equal to the distance on the left, it turns right; otherwise, it turns left. This comparative logic aims to avoid the obstacle by choosing the path with greater clearance .

You might also like