This time, we’ll take a look at dynamic variables and
• Just what are dynamic variables?
After that, we’ll build a new attachment for Eddie,
then get to programming!
Just what are dynamic variables?
We mentioned dynamic variables last month, but didn’t go
into much depth beyond describing the different types. We know
that a variable can be binary, boolean, or string but what does it all
mean? Well, it turns out that using dynamic or changing variables
can be an incredibly powerful tool in our programming arsenal.
Consider the following: Let’s say we wanted Eddie to move
faster if he saw a bright light, or slower if he didn’t see any
light. There is a variable for motor power (speed) that is set
to 75 by default in the NXT software. This number means
that 75% of the maximum motor power will be given to
the designated motor, and the number can be anywhere
between 0 (no power) and 100 (full power). You can adjust
it manually, or you can get fancy and use dynamic variables
to adjust the motor every time the light value changes.
Why are they so darned important?
Think of the possibilities! Dynamic variables can be used in
just about everything — from calculating interest rates on bank
accounts based on how much money the bank has in reserves,
to determining the average amount of toxic chemicals in the
air after an explosion before sending humans into the rubble.
Dynamic variables are one of the fundamental building
blocks of advanced programming, and today we’ll be
getting ourselves very comfortable with them.
First, let’s build a new attachment for Eddie that will
aim his light sensors outward so he can see the light
around him, instead of under him.
Start with a single
4 x 6 angular
Add two black
friction pegs and
two blue hybrid
pegs as indicated.
Attach the light
sensor to the
reverse — this will
attach to the other
side of the robot.
This is the finished product.
Plug the sensor on Eddie’s left
into port 1 and the one on the
right into port 2.
Testing a Dynamic Variable
We’ve built the attachment. Now, let’s create a
program that uses a dynamic variable. This program will
make Eddie move forward faster if he sees a bright light,
slower if he sees little light, and stop if he sees no light.
Let’s take a moment to think about what we just did:
• We connected the “intensity” variable output of the
light sensor to the “power” variable input of the
SERVO 12.2010 47