Robotics with the Boe-Bot text v2.2

Page 214 · Robotics with the Boe-Bot
Constants can even be used to calculate other constants. Here is an example of two
constants, named LeftThreshold and RightThreshold that are calculated using the
four constants just discussed. The LeftThreshold and RightThreshold constants are
used in the program to figure out whether or not the flashlight beam has been detected.
' Average Scale factor
LeftThreshold CON LeftBright + LeftAmbient / 2 * 5 / 8
RightThreshold CON RightBright + RightAmbient / 2 * 5 / 8
The math performed on these constants is an average, and then a scale. The average
calculation for LeftThreshold is LeftBright + LeftAmbient / 2. That result is
multiplied by 5 and divided by 8. This means that LeftThreshold is a constant whose
value is the
5
/
8
of the average of LeftBright and LeftAmbient.
Math expressions in PBASIC are executed from left to right. First, LeftBright is
added to LeftAmbient. This value is divided by 2. The result is then multiplied by 5 and
divided by 8.
Let’s try this: LeftBright + LeftAmbient = 20 + 108 = 128.
128 / 2 = 64.
64 * 5 = 320
320 / 8 = 40
You can use parentheses to force a calculation that is further to the right in a line of
PBASIC code to be completed first. For example, you can rewrite this line of PBASIC
code:
pulseRight = 2 - distanceRight * 35 + 750
like this:
pulseRight = 35 * (2 – distanceRight) + 750
In this expression, 35 is multiplied by the result of (2 – distanceRight), then the
product is added to 750.
Example Program: FlashlightControlledBoeBot.bs2
Enter FlashlightControlledBoeBot.bs2 into the BASIC Stamp Editor.
Substitute your timeLeft measurement with no flashlight beam (from Table 6-
1) in place of the value 108 in the LeftAmbient CON directive.