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

exp 4

The document outlines the algorithm for building an obstacle avoidance robot using sensors. It details the steps for setting up the robot, including library inclusion, object creation, and the main loop for obstacle detection and navigation. The robot uses ultrasonic sensors to measure distance and make decisions to avoid obstacles by moving backward and turning based on available space.

Uploaded by

shalinimonisha73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

exp 4

The document outlines the algorithm for building an obstacle avoidance robot using sensors. It details the steps for setting up the robot, including library inclusion, object creation, and the main loop for obstacle detection and navigation. The robot uses ultrasonic sensors to measure distance and make decisions to avoid obstacles by moving backward and turning based on available space.

Uploaded by

shalinimonisha73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exp. No.

: 4 Building an Obstacle Avoidance Robot Using Sensors

Algorithm:

Step 1: Start
Step 2: Include required libraries
• AFMotor (for controlling motors with Adafruit Motor Shield)
• NewPing (for ultrasonic distance measurement)
• Servo (for rotating the ultrasonic sensor)

Step 3: Define constants and create objects


• Define TRIG and ECHO pins for the ultrasonic sensor.
• Set MAX_DISTANCE and MAX_SPEED.
• Create motor objects for motor1 to motor4.
• Create a Servo object and a NewPing sonar object.

Step 4: Setup function


1. Attach the servo to pin 10.
2. Move servo to front-facing position (115°).
3. Read initial distance values 3–4 times using readPing() to
stabilize sensor reading.

Step 5: Loop begins (repeats continuously)


1. Read current distance using readPing().
2. If distance is less than or equal to 15 cm:
o Stop the robot.
o Move backward for 300 ms.
o Stop again.
o Rotate servo to the right (50°) → check distance.
o Rotate servo to the left (170°) → check distance.
o Compare distances on both sides:
▪ If right side has more space → turn right.
▪ Else → turn left.
o After turning, stop the robot.
3. If no obstacle is detected (distance > 15 cm):
o Move forward.

Step 6: Sub-functions
• readPing() → Measures distance using ultrasonic sensor. If
reading is 0, assume it’s 250 cm (no object).
• moveForward() → Starts motors in forward direction and
gradually increases speed.
• moveBackward() → Moves robot backward with gradual speed
increase.
• moveStop() → Stops all motors.
• turnRight() / turnLeft() → Spins wheels in opposite directions to
rotate the robot.
Output:

You might also like