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 else code block instead: Serial.print(“a is not greater than b”).
- Enter the code into the Arduino editor, save it, and upload it to 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-upload 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.
}