CONDITIONAL STATEMENTS
Before we dive into S-R-A Loop lets take a first look to IF-statemen. IF-statement allows us to execute some code when the condition is true. Such navigation of execution of the code is essential in programming and as such one of the fundamental structures of the field. Lets test test the bumper push-button-switch if it is working properly…
Tasks:
-
Construct the bumper of the robot with push-button-switch as is shown in this video instructions.
-
And connect the push-button-switch (PBSW) terminals with module RobDuino according to [@tbl:SW-RobDuino]:
| PBSW con. | RobDuino connectors |
|---|---|
| No. 1 | A0 |
| No. 2 | GND |
| No. 3 | +5V |
Table: Connection of push-button-switch to the Robduino module. {#tbl:SW-RobDuino}
- Test the push-button-switch in the bumper with next [@lst:260_Conditional_Statements]:
const int BUMPER_PIN = A0;
const int TEST_BUMPER_LED_PIN = 3;
void setup()
{
pinMode(BUMPER_PIN, INPUT);
pinMode(TEST_BUMPER_LED_PIN, OUTPUT);
}
void loop()
{
bool bumperIsPressed = digitalRead(BUMPER_PIN);
if ( bumperIsPressed ) digitalWrite(TEST_BUMPER_LED_PIN, HIGH);
}
: Conditional Statements. {#lst:260_Conditional_Statements}
- Then... complete the program to turn OFF the LED when the bumper is not touching anything.
- Next... Change IF statements into single one IF-THEN-ELSE statement.
Questions:
- Check if the LED on the output terminal D3 is ON when the bumper is pressed.
- Measure the voltage potencial at the terminal A0 when the bumper is pressed.
- Explain when the curly braces
{}are necessary in the if-statement.
Summary:
IF Statement
If statement can be written in several forms. The easiest one is:
if (value_one) statement1;In this case the variable named
value_onecan hold some numerical number. Ifvalue_oneistrueor greater than0the program will executestatement1. But this simple example is not used so often due its simplicity. We rather use it in this form:if ( value_one == value_two ){ statement1; statement2; }In this case
value_onecan be any number and thestatement1andstatement2will be executed if thevalue_onewill be equal tovalue_two. These command can be expanded into IF-ELSE form:if ( value_one == value_two ){ statement1; statement2; }else{ statement3; }Condition operators
Also other logical condition operators can be used:
- Less than:
a < b- Less than or equal to:
a <= b- Greater than:
a > b- Greater than or equal to:
a >= b- Equal to
a == b- Not Equal to:
a != bAnd some more conditional statements are available in C++:
if- to specify a block of code to be executed, if a specified condition is trueelse- to specify a block of code to be executed, if the same condition is falseelse- if to specify a new condition to test, if the first condition is falseswitch- to specify many alternative blocks of code to be executedIssues:
<++>
<++>