...
Create a program such that the motor rotates clockwise at the maximum speed when the rotation speed and direction of a motor is dependent on the distance from an obstacle is less than 30cm, and rotates anticlockwise at half the maximum speed when the distance from the obstacle is greater than or equal to 30cmas given in the table below. The motor has to be powered using a separate battery, and not from Arduino (Arduino only controls the motor, does not power it).
Distance from Obstacle | Motor Rotation |
---|---|
Distance > 50 cm | Clockwise, full-speed |
35 < Distance <= 50 cm | Clockwise, quarter-speed |
30 < Distance <= 35 cm | No rotation |
20 < Distance <= 30 cm | Anticlockwise, quarter-speed |
Distance <= 20 cm | Anticlockwise, full-speed |
Hints
Combine the codes and circuits in
slides 3 and 10Ultrasonic Distance Sensor and DC Motors, and use
an if conditionmultiple if conditions. You will need ifs to be nested, i.e., ifs within ifs.
analogWrite() with values 255,0 (dir1speed,dir2speed) and 64,0
,128respectively can be used for full-speed and quarter-speed clockwise rotation. 0,255 and 0,64 can be used for full-speed and quarter-speed anticlockwise rotation respectively. The speed is not perfectly linearly related to the value (duty cycle) in practice, and that is ok for this exercise
.
Solution
...
.