Meet our robot. It has 2 servo motors that controls the two side wheels and a small caster wheel. The servo motors are connected to the Arduino boards. The left servo is connected to pin 9 and the right servo is connected to pin 10. In order to control the motors, we use a library Servo.h
. To include the library into our program we use the #include <Servo.h>
. Note that the line that starts with #
does not end in a semicolon.
After we include the library we can create two objects of Servo
, which is done on line 3 and 4. For every servo variable, we give it a name to be able to use it afterwards in our code. Inside the setup function, we will attach the pin number to the library servos. The left one is at pin 9 and the right servo at pin 10. This is done on line 7 and 8.
To control the servo, we can write to it an angle from 0 to 180. At 90 the continuos servo does not move. And as you increase the value towards the 180 or decrease it towards 0, the servo will change the speed in the specific direction clockwise or anticlockwise, depending if the angle is above or under 90.
The following program writes an angle of 170 to the left servo and then waits for 3 seconds. After that it writes 90 to both servos for them to stop. Modify the code to collect all 3 coins.
Can you get all the coins?