Task
Create a program such that the rotation speed and direction of a motor is dependent on the distance from an obstacle as follows.
Distance from Obstacle | Motor Rotation |
---|---|
Distance > 50 cm | Clockwise, full-speed |
25 < Distance <= 50 cm | Clockwise, quarter-speed |
20 < Distance <= 25 cm | No rotation |
Distance <= 20 cm | Anticlockwise, quarter-speed |
Hints
- Combine the codes and circuits in Ultrasonic Distance Sensor and DC Motors, and use multiple if conditions. You will need ifs to be nested, i.e., ifs within ifs.
- analogWrite() with values 255,0 (dir1speed,dir2speed) and 64,0 respectively can be used for full-speed and quarter-speed clockwise rotation. 0,64 can be used for quarter speed anticlockwise rotation. The speed is not perfectly linearly related to the value (duty cycle), and that is ok for this exercise.