Lesson 4 - If & else statements
From the previous lesson, we were able to get a basic understanding of Arduino and the IDE. We programmed the Arduino to blink an LED at intervals of one second. In this lesson, we will be progressing further and we will be using conditional statements on the Arduino.
What is a conditional statement?
A conditional statement is a basic programming structure which runs a set of instructions depending on a condition. When programming an Arduino, there are variations of conditional statements. These are used as basic control structures on the Arduino:
Note: Curly parenthesis are used to contain the code for each conditional statement. It is important to close these parenthesis off, to avoid syntax errors.
Program Requirements
The requirements we will be working against in this program are:
1) Blink an LED 10 times at half a second intervals
2) Each blink should be numerically displayed in the serial monitor
3) The LED on pin 13 is to be used
4) After 10 blinks, the LED should turn off completely, until the Arduino is reset
Coding
In the global scope, we will be declaring a variable name for pin 13, which will be used as our LED pin. The data type of this variable will remain as a constant integer. We will also be declaring another variable which will be used to count the blinks. The value of this variable will be changing as the LED blinks, and therefore it is declared as an integer with an initial count value of 0.
int Count = 0;
After the global variables have been declared within the scope, we will be setting up the pin mode for the LED pin as an output and initialising the serial monitor at a baud rate of 9600 bits per second. This is done in 'void setup' the same way as we had done in lesson 3.
Once the global variables have been declared and the program has been setup, we can move on to the main coding loop. The first step we will take in to writing the main program will be to define the conditions of the 'if' statement. From the program requirements, we are told that after 10 LED blinks the LED must turn off completely. The variable 'Count' is used within the condition because if the value of 'Count' is less than 10, we want the LED to blink. This is written as:
if (Count < 10) {
//code
}
If the counter is less than 10, the program will not proceed and loop within the 'if' statement. To blink the LED, we will simply digitally write the pin HIGH and LOW with a delay of 500 milliseconds. This will blink the LED once, so we must record this blink to 'Count'. Because this variable has been declared as an integer, we are able to modify its initial value throughout the code. To increase the value of 'Count', we use a '++' instruction. This increments the value only by 1 after each blink.
Count++;
Once the blink number has been stored, the program requirements ask us to numerically display this in the serial monitor. We simply print the integer as a whole in the serial monitor; without speech marks because we are displaying the global variable value.
Serial.println(Count);
The Arduino will now loop within the 'if' condition until the counter reaches 10. Once 10 blinks have been achieved, we are required to turn the LED off completely. Here's where we will use an 'else' statement. Within the 'else' statement we just digitally write the LED LOW and this will completely turn it off.
else{
digitalWrite(LED, LOW);
}
We have successfully completed the program meeting the specified requirements. It can now be compiled and uploaded to be tested. Below is an example of the serial monitor report. Conditional statements will allow us to program more complex codes at a higher level. We will be using them throughout the rest of the course.
This course has been created by InnoVech:
By Zaqyas Mahmood, Electronics Engineer
Copyright © 2020 InnoVech All Rights Reserved
Comments