In this part of the lab, you'll build a basicBot that will move forward and backward as well as rotate left and right.
Electronics and microcontroller
2 x continuous rotation servo motors
1 x Arduino Uno
1 x breadboard
jumper wires
Power supply and shell
1 x battery holder for 6 AA
6 x AA batteries (rechargeable is fine)
1 x snap attachment to DC power jack
1 x robot base (e.g., cardboard, plate or vinyl record with a hole in the center)
2 x wheels compatible with servo motor (e.g., thingiverse part)
2 x wheel treads (e.g., o-ring gaskets)
1 x stabilizer caster wheel
Tools
small philips head screwdriver
glue gun
First, attach your wheels to your servos:
Place wheel on servo motor.
->
Place cap on wheel.
Screw cap onto wheel.
Attach gasket to wheel for tread.
Set up your project, but do not attach the USB cable.
Next, start playing with your code.
Create a new sketch called basicBot.
Adapt the code from the Getting started with servo motors tutorial to
declare 2 variables of type Servo
leftServo
rightServo
declare 2 variables of type int for your stop values of each servo
leftServoStopValue
rightServoStopValue
declare 2 variables of type int for the pins to control your servos
leftServoPin (assign this the value 9)
rightServoPin (assign this the value 6)
declare and define four methods:
initServos should initialize both Servo objects
moveForward should move the bot forward; since the wheels will be mounted mirror-imaged to each other, this will rotate
your left servo counter clockwise
your right servo clockwise
moveBackward should move the bot backward
stopMoving should stop moving the bot
make sure to use the correct stop value for each motor
your setup should invoke initServos
your loop should
move the bot forward, then wait 2 seconds
move the bot backward, then wait 2 seconds
stop the bot, then wait 2 seconds
Test your code and don't forget to follow the steps for managing your power supply!
Now that you've got your code working, build the shell of your bot. You want it to something like this:
(top)
(bottom)
Disconnect your servo motors from the Arduino/breadboard.
Disconnect your Arduino from the USB cable.
Attach the wheels
Glue the left servo onto the bottom of your robot base; attach it so that the wires are closer to the front of the bot.
Making sure that it's parallel the the left, glue the right servo onto the bottom of your robot base; again, attach it so that the wires are closer to the front of the bot.
Glue the stabilizer caster wheel to the bottom of the bot (centered in the back)
Manage your wires
Thread the left servo's wires through the hole in your base and toward the left of the bot.
Thread the right servo's wires through the hole in your base and toward the right of the bot.
If you'd like, use a piece of tape to keep the wires where you want.
Reattach your Arduino/breadboard.
Double-check all connections (particularly that the servos have red wires to 5V and black wires to GND).
And test it out!
Place your 6 AA batteries in the holder.
Snap the connector onto the holder.
Double-check that the USB cable is not plugged in.
Plug in the battery pack to the Arduino -- it should move forward, back and stop!
Now, modify your sketch to:
include two more methods
rotateLeft should move one wheel while stopping the other to rotate counter clockwise
rotateRight should move one wheel while stopping the other to rotate clockwise
modify your loop to
move the bot forward, then wait 2 seconds
move the bot backward, then wait 2 seconds
stop the bot, then wait 2 seconds
rotate the bot left, then wait 2 seconds
rotate the bot right, then wait 2 seconds
stop the bot, then wait 2 seconds
Test it out -- your bot should move forward, back, stop, rotate left, right, stop!
Upload your basicBot sketch to moodle's Lab 2 - servo motors.