Obstacle Avoiding Bot ( Maze Solver )
This is the Obstacle Avoiding / Maze Solver Bot Code & Circuit Diagrams
Main Components Required
1 . Arduino UNO / UNO R3 - 01
2 . L298 Motor Driver - 01
3 . N20 300rpm Motor - 04
4 . Caster wheel - 04
5 . Ultrasonic Senser - 03
6 . Some Jumper Wires
7 . 12V Battery
**---------------code starts here---------------**
// Pin definitions for motors and ultrasonic sensors
const int leftMotorPin = 2;
const int rightMotorPin = 4;
const int frontUltrasonicTrigPin = 11;
const int frontUltrasonicEchoPin = 12;
const int leftUltrasonicTrigPin = 9;
const int leftUltrasonicEchoPin = 10;
const int rightUltrasonicTrigPin = 7;
const int rightUltrasonicEchoPin = 8;
const int motorPinA = 3;
const int motorPinB = 5;
// Maximum distance to consider an obstacle (in centimeters)
const int obstacleDistance = 15;
void setup() {
// Initialize motor pins as outputs
pinMode(leftMotorPin, OUTPUT);
pinMode(rightMotorPin, OUTPUT);
pinMode(motorPinA, OUTPUT);
pinMode(motorPinB, OUTPUT);
// Initialize ultrasonic sensor trigger pins as outputs and echo pins as inputs
pinMode(frontUltrasonicTrigPin, OUTPUT);
pinMode(frontUltrasonicEchoPin, INPUT);
pinMode(leftUltrasonicTrigPin, OUTPUT);
pinMode(leftUltrasonicEchoPin, INPUT);
pinMode(rightUltrasonicTrigPin, OUTPUT);
pinMode(rightUltrasonicEchoPin, INPUT);
// Start with motors off
digitalWrite(leftMotorPin, LOW);
digitalWrite(rightMotorPin, LOW);
digitalWrite(motorPinA, LOW);
digitalWrite(motorPinB, LOW);
// Initialize Serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Measure distances from ultrasonic sensors
int frontDistance = measureDistance(frontUltrasonicTrigPin, frontUltrasonicEchoPin);
int leftDistance = measureDistance(leftUltrasonicTrigPin, leftUltrasonicEchoPin);
int rightDistance = measureDistance(rightUltrasonicTrigPin, rightUltrasonicEchoPin);
// Print distances for debugging
Serial.print("Front: ");
Serial.print(frontDistance);
Serial.print(" cm, Left: ");
Serial.print(leftDistance);
Serial.print(" cm, Right: ");
Serial.println(rightDistance);
// Set default motor speeds
int motorSpeedA = 30; // adjust to 200
int motorSpeedB = 30; // adjust to 200
int motorSpeedLA = 35; // adjust to 205
int motorSpeedRB = 35; // adjust to 205
// Adjust motor speeds based on distances
if (frontDistance <= 25) {
//FOR NORMAL CONDITIONS
if (rightDistance < leftDistance) {
// Obstacle detected on the left, turn right
digitalWrite(leftMotorPin, LOW);
digitalWrite(rightMotorPin, HIGH);
analogWrite(motorPinA, motorSpeedLA);
analogWrite(motorPinB, motorSpeedRB);
} else if (leftDistance < rightDistance) {
// Obstacle detected on the right, turn left
digitalWrite(leftMotorPin, HIGH);
digitalWrite(rightMotorPin, LOW);
analogWrite(motorPinA, motorSpeedLA);
analogWrite(motorPinB, motorSpeedRB);
} else {
// No obstacle detected, move forward
digitalWrite(leftMotorPin, HIGH);
digitalWrite(rightMotorPin, HIGH);
analogWrite(motorPinA, motorSpeedA);
analogWrite(motorPinB, motorSpeedB);
}
} else if (leftDistance <= obstacleDistance && frontDistance >= 40) {
// Obstacle detected in front, avoid it
digitalWrite(leftMotorPin, HIGH);
digitalWrite(rightMotorPin, LOW);
analogWrite(motorPinA, motorSpeedLA);
analogWrite(motorPinB, motorSpeedRB);
} else if (rightDistance <= obstacleDistance && frontDistance >= 40) {
// Obstacle detected on the left, turn right
digitalWrite(leftMotorPin, LOW);
digitalWrite(rightMotorPin, HIGH);
analogWrite(motorPinA, motorSpeedLA);
analogWrite(motorPinB, motorSpeedRB);
} else {
// No obstacle detected, move forward
digitalWrite(leftMotorPin, HIGH);
digitalWrite(rightMotorPin, HIGH);
analogWrite(motorPinA, motorSpeedA);
analogWrite(motorPinB, motorSpeedB);
}
}
// Function to measure distance using ultrasonic sensor
int measureDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
unsigned long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2; // Calculate distance in centimeters
return distance;
}
**---------------code ends here---------------**