Arduino Code Explanation
1. Importing Required Libraries
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
These libraries are essential for controlling motors, handling ultrasonic distance measurements,
and operating a servo motor.
AFMotor.h: Controls DC motors using Adafruit's motor shield.
NewPing.h: Handles ultrasonic sensor readings.
Servo.h: Allows control of a servo motor.
2. Defining Global Variables
#define TRIG_PIN A0
#define ECHO_PIN A1
#define MAX_DISTANCE 200
#define MAX_SPEED 190 // Sets speed of DC motors
#define MAX_SPEED_OFFSET 20
These constants define hardware pins and operational limits:
TRIG_PIN and ECHO_PIN: Assign analog pins for ultrasonic sensor.
MAX_DISTANCE: Defines the maximum range of the sensor.
MAX_SPEED: Sets the maximum motor speed.
MAX_SPEED_OFFSET: Adjusts speed difference between motors for better movement
control.
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
This initializes the ultrasonic sensor for distance measurement.
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
These lines initialize four motors using Adafruit's motor shield, each assigned to a different port.
Servo myservo;
boolean goesForward = false;
int distance = 100;
int speedSet = 0;
myservo: Controls a servo motor.
goesForward: Keeps track of movement direction.
distance: Stores the detected distance from the ultrasonic sensor.
speedSet: Controls motor speed.
3. Setup Function
void setup() {
[Link](10);
[Link](115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
The servo is attached to pin 10 and moved to position 115°.
The ultrasonic sensor takes three initial distance readings for calibration.
4. Main Loop
void loop() {
int distanceR, distanceL;
delay(40);
if (distance <= 15) {
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if (distanceR >= distanceL) {
turnRight();
moveStop();
} else {
turnLeft();
moveStop();
}
} else {
moveForward();
distance = readPing();
}
}
Functionality:
Reads the distance from the ultrasonic sensor.
If an obstacle is detected (distance <= 15 cm):
1. Stops movement.
2. Moves backward.
3. Stops again.
4. Checks the distance to the right and left.
5. Turns in the direction with more space.
If no obstacle is detected, it moves forward and keeps updating distance.
5. Sensor Functions
int lookRight() {
[Link](50);
delay(500);
int distance = readPing();
delay(100);
[Link](115);
return distance;
}
Moves the servo to 50°, reads distance, then returns it to the center (115°).
int lookLeft() {
[Link](170);
delay(500);
int distance = readPing();
delay(100);
[Link](115);
return distance;
}
Moves the servo to 170°, reads distance, then returns it to the center.
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if (cm == 0) cm = 250;
return cm;
}
Reads distance from the ultrasonic sensor. If no obstacle is detected, it returns 250 cm (out of
range).
6. Motor Control Functions
Stop Movement
void moveStop() {
[Link](RELEASE);
[Link](RELEASE);
[Link](RELEASE);
[Link](RELEASE);
}
Stops all four motors.
Move Forward
void moveForward() {
if (!goesForward) {
goesForward = true;
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2) {
[Link](speedSet);
[Link](speedSet);
[Link](speedSet);
[Link](speedSet);
delay(5);
}
}
}
Gradually increases motor speed when moving forward.
Move Backward
void moveBackward() {
goesForward = false;
[Link](BACKWARD);
[Link](BACKWARD);
[Link](BACKWARD);
[Link](BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2) {
[Link](speedSet);
[Link](speedSet);
[Link](speedSet);
[Link](speedSet);
delay(5);
}
}
Similar to moveForward(), but moves in reverse.
Turn Right
void turnRight() {
[Link](FORWARD);
[Link](FORWARD);
[Link](BACKWARD);
[Link](BACKWARD);
delay(500);
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
}
Rotates the robot to the right.
Turn Left
void turnLeft() {
[Link](BACKWARD);
[Link](BACKWARD);
[Link](FORWARD);
[Link](FORWARD);
delay(500);
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
[Link](FORWARD);
}
Rotates the robot to the left.
Summary
This Arduino program controls a robot that:
Uses an ultrasonic sensor to detect obstacles.
Moves forward, stops, or turns based on obstacle distance.
Uses a servo to scan left and right before making decisions.
Controls four DC motors for movement.
The logic ensures the robot avoids obstacles and navigates efficiently.