Datasheet
Chapter 6 • Light-Sensitive Navigation with Phototransistors
202 • Robotics with the BOE Shield-Bot
ndShade = tRight / (tLeft+tRight) - 0.5; // Calculate & subtract 0.5
Next, this for loop places the cursor in the right place for printing an asterisk. Take a close
look at the
for loop’s condition. It takes ndShade and multiples it by 40. It also has to add
20 to the value because if
ndShade is –0.5, we want that to print with zero leading spaces.
So (–0.5 × 40) + 20 = 0. Now, if
ndShade is 0, we want it to print 20 spaces over: (0 × 40) +
20 = 20. If it’s +0.5 we want it to print 40 spaces over: (0.5 × 40) + 20 = 40. Of course, if it’s
something in between, like 0.25, we have (0.25 × 40) + 20 = 30. So, it’ll print half way
between center and far right.
for(int i = 0; i<(ndShade * 40) + 20; i++) // Place asterisk in 0 to 40
{
Serial.print(' '); // Pad (ndShade * 40) + 20 spaces
}
After printing the spaces, a single asterisk prints on the line. Recall that println prints and
also adds a newline so that the next time through the loop, the asterisk will display on the
next line.
Serial.println('*'); // Print asterisk and newline
delay(100); // 0.1 second delay
}
Activity 4: Test a Light-Roaming Routine
One approach toward making the Boe-Bot roam toward light sources is to make it turn away
from shade. You can use the
ndShade variable to make the BOE Shield-Bot turn a little or a
lot when the contrast between the light detected on each side is a little or a lot.
Shady Navigation Decisions
Here is an if statement that works well for turning away from shade on the right side of the
BOE Shield-Bot. It starts by declaring two
int variables, speedLeft and speedRight. They
are not declared within the
if…else block because other blocks in the loop function will
need to check their values too. Next,
if(ndShade > 0.0) has a code block that will be
executed if shade is detected on the robot’s right side, slowing down the left wheel to make
the BOE Shield-Bot turn away from the dark. To do this,
ndShade * 1000.0 is subtracted
from 200. Before assigning the result to
speedLeft, int(200.0–(ndShade×1000.0)
converts the answer from a floating point value back to an integer. We’re doing this to make
the value compatible with our custom
maneuver function , which needs an int value.