An Arduino-powered robot that uses an ultrasonic sensor mounted on a servo motor to navigate. It features a robust state-machine logic to scan surroundings and navigate around obstacles efficiently.
- State-Machine Logic: Replaces messy
if-elsechains with a cleanenumstructure. - 180Β° Environmental Scanning: Uses a servo to "look" left and right when blocked.
- Real-time Decision Making: Calculates the best path based on sensor data.
- Safety Stop: Automatically halts motors to prevent collisions.
| Component | Arduino Pin | Function |
|---|---|---|
| IN1 | D2 | Left Motor Forward |
| IN2 | D3 | Left Motor Backward |
| IN3 | D4 | Right Motor Forward |
| IN4 | D5 | Right Motor Backward |
| Component | Arduino Pin |
|---|---|
| Servo Signal | D9 |
| Ultrasonic Trig | D13 |
| Ultrasonic Echo | D12 |
The robot follows a systematic "Think-then-Act" process:
- Scan Front: Constantly measures distance while moving.
- Detection: If an object is detected within 20cm, the robot stops.
- Scan Right: Servo turns to 20Β° to check clearance.
- Scan Left: If Right is blocked, servo turns to 160Β° to check clearance.
- Execute: The robot turns toward the open path and resumes forward movement.
- Clone this repository:
git clone [https://github.com/yourusername/obstacle-avoiding-robot.git](https://github.com/yourusername/obstacle-avoiding-robot.git)
- Open the
.inofile in the Arduino IDE. - Ensure the
Servo.hlibrary is installed. - Select your board (Arduino Uno) and the correct Port.
- Click Upload.
The project is structured using a switch-case state machine:
enum RobotState {
SCAN_FRONT,
DECIDE_PATH,
MOVE_FORWARD,
STOP_AND_SCAN_RIGHT,
CHECK_RIGHT,
TURN_RIGHT,
SCAN_LEFT,
CHECK_LEFT,
TURN_LEFT
};
π€ Contributing
Contributions are welcome! If you'd like to improve the movement algorithms or add speed control (PWM), feel free to fork the repo and submit a pull request.