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

Arduino Code

The document contains an Arduino code for a robot that uses ultrasonic sensors to detect obstacles and control motors. It defines pin configurations for the motors and sensor, measures distance, and implements movement logic to avoid obstacles by stopping, reversing, and turning. The robot moves forward when no obstacles are detected and executes specific maneuvers when an obstacle is within 20 cm.

Uploaded by

kumar.sunilgs82
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)
5 views4 pages

Arduino Code

The document contains an Arduino code for a robot that uses ultrasonic sensors to detect obstacles and control motors. It defines pin configurations for the motors and sensor, measures distance, and implements movement logic to avoid obstacles by stopping, reversing, and turning. The robot moves forward when no obstacles are detected and executes specific maneuvers when an obstacle is within 20 cm.

Uploaded by

kumar.sunilgs82
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

// Pin definitions

#define trigPin 10

#define echoPin 9

#define motorA1 3

#define motorA2 4

#define motorB1 5

#define motorB2 6

long duration;

int distance;

int motorSpeed = 255; // Full speed

void setup() {

// Initialize the serial communication

[Link](9600);

// Initialize pins

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(motorA1, OUTPUT);

pinMode(motorA2, OUTPUT);

pinMode(motorB1, OUTPUT);

pinMode(motorB2, OUTPUT);

// Set motors to stop initially

stopMotors();
}

void loop() {

// Measure the distance

distance = measureDistance();

// Print distance to serial monitor

[Link]("Distance: ");

[Link](distance);

if (distance < 20) {

// Obstacle detected, stop and reverse

stopMotors();

delay(500);

reverseMotors();

delay(1000); // Move back for 1 second

stopMotors();

delay(500);

turnRight();

delay(1000); // Turn for 1 second

} else {

// Move forward if no obstacle is detected

moveForward();

delay(100);

}
long measureDistance() {

// Trigger the sensor to send pulse

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Measure the time it takes for the pulse to return

duration = pulseIn(echoPin, HIGH);

// Calculate distance based on the time the pulse took to return

distance = duration * 0.0344 / 2; // Speed of sound is 0.0344 cm/us

return distance;

void moveForward() {

digitalWrite(motorA1, HIGH);

digitalWrite(motorA2, LOW);

digitalWrite(motorB1, HIGH);

digitalWrite(motorB2, LOW);

void stopMotors() {

digitalWrite(motorA1, LOW);
digitalWrite(motorA2, LOW);

digitalWrite(motorB1, LOW);

digitalWrite(motorB2, LOW);

void reverseMotors() {

digitalWrite(motorA1, LOW);

digitalWrite(motorA2, HIGH);

digitalWrite(motorB1, LOW);

digitalWrite(motorB2, HIGH);

void turnRight() {

digitalWrite(motorA1, HIGH);

digitalWrite(motorA2, LOW);

digitalWrite(motorB1, LOW);

digitalWrite(motorB2, HIGH);

You might also like