Datasheet

Chapter 6 Light-Sensitive Navigation with Phototransistors
214Robotics with the BOE Shield-Bot
#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
// Add this if condition to stop under the bright lamp.
if((tRight + tLeft) < 1300.0) // If A3 voltage greater than 2
{
servoLeft.detach(); // Stop servo signals
servoRight.detach();
}
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
speedLeft = int(200.0 - (ndShade * 1000.0));
speedLeft = constrain(speedLeft, -200, 200);
speedRight = 200; // Full speed right wheel
}
else // Shade on Left?
{ // Slow down right wheel
speedRight = int(200.0 + (ndShade * 1000.0));
speedRight = constrain(speedRight, -200, 200);
speedLeft = 200; // Full speed left wheel
}
maneuver(speedLeft, speedRight, 20); // Set wheel speeds
}
long rcTime(int pin) // rcTime measures decay at pin
{
pinMode(pin, OUTPUT); // Charge capacitor
digitalWrite(pin, HIGH); // ..by setting pin ouput-high
delay(5); // ..for 5 ms
pinMode(pin, INPUT); // Set pin to input
digitalWrite(pin, LOW); // ..with no pullup