# Exercise 1: Servos
You were introduced to the field of engineering robotics and the Arduino programming basics you will see in the workshop. Here you will learn some hardware basics.
The main goal of the exercise is to get acquainted with the Arduino IDE, and confirm that the servos work as expected.
# Move a robot!
Meet our robot. It has 2 servo motors that control the two side wheels and a small caster wheel. In order to control the motors, we use a library Servo.h. To include the library into our program we use the
After we include the library we can create two
Servo objects of, which is done on line 3 and 4 in the Arduino sketch. 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 continuous 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.
Task 1: Move straight (virtual)
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.
The servos are installed opposite to each other. If the servo motors are both moving clockwise (or anticlockwise), then they will be turning in opposite directions relative to each other. Choose the right numbers between 0 and 180 to make the servos rotate so that the robot moves straight.
Task 2: Move straight (physical)
Goal: wire up the robot and test the servos using the working example.
- Take an inventory of all your hardware components. It should be wired up already. [see demo]
- Download the working example (click the 3 vertical dots to download). Open with the Arduino IDE and upload to your Uno board.
- If everything is working correctly, your wheels should move forward. Troubleshoot, if not.
Note: The Uno Board can be powered using USB, AA batteries, a 9V battery, or by using a rechargeable power pack alternative (4$ example from Dollarama). Use the USB connection to your computer to confirm the robot servos are working as expected. We will use power packs to keep things simple and consistent.
- Try to get your robot moving in a straight line.
- Optional: If you have quickly accomplished this task, try modifying your code by using
void loop. Challenge yourself to write a move straight function and call it in the loop. Then try to write other functions like turnLeft, turnRight, or something more ambitious like zigZag.