In this part of the lab, you'll adapt your BasicBot and code from the Advanced HC-SR04 (distance sensor) example to build a FollowBot that will follow an object at a target distance (default is 32 cm).
Electronics and microcontroller
2 x continuous rotation servo motor
1 x HC-SR04 distance sensor
3 x LEDs (pick the same color)
1 x 200 ohm resistor
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 plug
1 x robot base (e.g., plate or vinyl record)
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
Set up your project, but do not attach the USB cable.
Adapt the sketch from the Advanced HC-SR04 (distance sensor) example by adding in code from your BasicBot 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
moveBackward should move the bot backward
stopMoving should stop moving the bot
modify your setup to invoke initServos
Additionally:
declare and define a new method moveBot to
move the bot forward if the distance sensed is over the target; or
move the bot backward if the distance sensed is under the target; or
stop the bot if the distance sensed satisfies the target
modify your loop to invoke moveBot after invoking updateOutput
Being careful to manage your power supply, test out your code. Your bot should follow an object (try a piece of cardboard) at 32cm. If it's too far, the bot should move forward; too close, and the bot should move back.
Upload your followBot sketch to moodle's Lab 2 - servo motors.