Datasheet

Chapter 6 Light-Sensitive Navigation with Phototransistors
204Robotics with the BOE Shield-Bot
The larger ndShade is, the more it subtracts from 200. That’s not a problem in this example,
but if
ndShade were 0.45, it would try to store 250 in the speedLeft variable. Since the
speeds we’ll want to pass to the
maneuver function need to be in the -200 to 200 range, we’ll
use the Arduino’s
constrain function to prevent speedLeft from going out of
bounds:
speedLeft = constrain(speedLeft, –200, 200).
Here is an
else statement that works well for turning away from shade on the left. It slows
down the right wheel and keeps the left wheel going full speed forward. Notice that it adds
(ndShade*1000)
to 200. Reason being, this is the else statement for if(ndShade > 0.0),
so it will get used when
ndShade is equal to or smaller than zero. So, if ndShade is
0.125,
speedRight = int(200.0 + (ndShade * 1000.0)) would evaluate to 200 + (
1.25 × 1000) = 200 125 = 75. The
constrain function is used again, to limit speedRight.
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
}
Test Navigation Decisions with Serial Monitor
Before actually testing out these navigation decisions, it’s best to take a look at the variable
values with the Serial Monitor. So, instead of a call to the
maneuver function, first, let’s use
some
Serial.print calls to see if we got it right.
Serial.print(speedLeft, DEC); // Display speedLeft
Serial.print(" "); // Spaces
Serial.print(ndShade, DEC); // Display ndShade
Serial.print(" "); // More spaces
Serial.println(speedRight, DEC); // Display speedRight
delay(2000); // 1 second delay
}
The print and println calls should result in a display like the one below. It shows the value
of
speedLeft in the left column, speedRight in the right column, and ndShade between
them. Watch it carefully. The side with brighter light will always display 200 for full-speed-
forward, and the other will be slowing down with values less than 200—the darker the
shade, the smaller the number.