Datasheet

Chapter 1 Your Shield-Bot’s Brain
30Robotics with the BOE Shield-Bot
Activity 5: Make Decisions
Your BOE Shield-Bot will need to make a lot of navigation decisions based on sensor inputs.
Here is a simple sketch that demonstrates decision-making. It compares the value of
a to b,
and sends a message to tell you whether or not
a is greater than b, with an if…else
statement.
If the condition
(a > b) is true, it executes the if statement’s code
block:
Serial.print("a is greater than b"). If a is not greater than b, it executes
the
else code block instead: Serial.print("a is not greater than b").
Create the SimpleDecisions sketch, save it, and run it on the Arduino.
Open the Serial Monitor and test to make sure you got the right message.
Try swapping the values for
a and b.
Re-load the sketch and verify that it printed the other message.
// Robotics with the BOE Shield - SimpleDecisions
void setup()
{
Serial.begin(9600);
int a = 89;
int b = 42;
if(a > b)
{
Serial.print("a is greater than b");
}
else
{
Serial.print("a is not greater than b");
}
}
void loop()
{
// Empty, no repeating code.
}
More Decisions with if... else if
Maybe you only need a message when a is greater than b. If that’s the case, you could cut
out the
else statement and its code block. So, all your setup function would have is the
one
if statement, like this: