Datasheet
Light-Sensitive Navigation with Phototransistors • Chapter 6
Robotics with the BOE Shield-Bot • 207
Activity 5: Shield-Bot Navigating by Light
At this point, the LightSeekingDisplay sketch needs four things to take it from displaying
what it’s going to do to actually doing it:
Remove the
Serial.print calls.
Add servo code.
Add the
maneuver function.
Add a call to the
loop function to pass speedLeft and speedRight to
the
maneuver function.
The result is the LightSeekingShieldBot sketch.
Create, save, and run the sketch LightSeekingShieldBot.
Connect the battery pack, put the BOE Shield-Bot on the floor, and set the power
switch to 2.
Let the BOE Shield-Bot roam and try casting shadows over its left and right light
sensors. It should turn away from the shadow.
/*
* Robotics with the BOE Shield - LightSeekingShieldBot
* Roams toward light and away from shade.
*/
#include <Servo.h> // Include servo library
Servo servoLeft; // Declare left and right servos
Servo servoRight;
void setup() // Built-in initialization block
{
tone(4, 3000, 1000); // Play tone for 1 second
delay(1000); // Delay to finish tone
servoLeft.attach(13); // Attach left signal to pin 13
servoRight.attach(12); // Attach right signal to pin 12
}
void loop() // Main loop auto-repeats
{
float tLeft = float(rcTime(8)); // Get left light & make float
float tRight = float(rcTime(6)); // Get right light & make float
float ndShade; // Normalized differential shade
ndShade = tRight / (tLeft+tRight) - 0.5; // Calculate & subtract 0.5
int speedLeft, speedRight; // Declare speed variables
if (ndShade > 0.0) // Shade on right?
{ // Slow down left wheel