Datasheet
Chapter 1 • Your Shield-Bot’s Brain
26 • Robotics with the BOE Shield-Bot
See that 'm' really is 109!
There are two ways to prove that the ASCII code for 'm' really is 109. First, instead of
declaring
char c = 'm', you could use byte c = 'm'. Then, the println function will
print the
byte variable’s decimal value instead of the character it represents. Or, you could
leave the
char c declaration alone and instead use Serial.println(c, DEC) to display
the decimal value
c stores.
Try both approaches. So, do you think the letters l, m, n, o, and p would be
represented by the ASCII codes 108, 109, 110, 110, 111, and 112?
Modify your sketch to find out the decimal ASCII codes for l, m, n, o, p.
If you can, go to http://learn.parallax.com/ascii and try other ASCII characters.
Global vs.Local Variables
So far, we’ve declared variables inside a function block (inside the function’s curly braces),
which means they are local variables. Only the function declaring a local variable can see or
modify it. Also, a local variable only exists while the function that declares it is using it. After
that, it gets returned to unallocated memory so that another function (like
loop) could use
that memory for a different local variable.
If your sketch has to give more than one function access to a variable’s value, you can
use global variables. To make a variable global, just declare it outside of any function,
preferably before the
setup function. Then, all functions in the sketch will be able to modify
or retrieve its value. The next example sketch declares global variables and assigns values to
them from within a function.
Example Sketch – StoreRetrieveGlobal
This example sketch declares a, c, and root2 as global variables (instead of local). Now that
they are global, both the
setup and loop functions can access them.
Modify your sketch to match the one below, and save it as StoreRetrieveGlobal.
Run it on the Arduino, and then open the Serial Monitor and verify that the
correct values are displayed repeatedly by the
loop function.
// Robotics with the BOE Shield - StoreRetrieveGlobal
int a;
char c;
float root2;
void setup()
{
Serial.begin(9600);