Conditional Statement with Arduino

If Statements

If a statement is a basic conditional statement that can be used for checking the similarity of two values. If statement makes it possible to perform an action based on the result of the comparison. When the result is true, the tasks listed within the statement body are executed and a false result skips the task listed in the statement body. A general if statement can be written as follows:

if (conditions) {

//task to be executed if condition return true

}

If statement are used in multiple ways. You cannot program a complex program without using an if statement. the statements can be used to compare sensor threshold values and perform an action based on the sensor value.

If-Else

            If else statement has an if statement but with another option when the first one fails. The else statement does not check for conditions. Therefore, it becomes the default operation when all the conditions fail. The syntax is as follows:

if (conditions) {

//task to be executed if conditions return true

}

else {

//default task

}

Nested if statement

Sometimes one wants to check multiple conditions before performing a specific action. Nested if statements help with the process of performing multiple condition checks by using multiple if statements. The if statements are written in another if statement to form a nest of if statements. The execution process moves from the outer if statement to the innermost if statement. The if statements within another if statement can also have an else clause. The syntax is as follows:

if (condition) {

//task to be executed if conditions return true (optional)

if (condition) {

//task to be executed if conditions return true (optional)

if (condition) {

//task to be executed if conditions return true (optional)

//This is the innermost if statement in this nest

}

}

else {

//default task for the second level if statement

}

}

If…Else Statements

Sometimes you may want to check a second or a second or a third condition when the first one fails. In this case, you cannot use an else statement as it does not check for any condition. However, you can use a combination of else and if statements to check for a second condition. The syntax of else…if statement is as follows:

if (conditions) {

//task to be executed if conditions return true

}

else if (more conditions) {

//task to be executed if conditions return true

}

Switch Case

            Switch case statements operate similarly as if statement because they are conditional based. The switch case allows you to choose between multiple options based on the value of a variable. The switch case is majorly used when there are multiple options to choose from. The syntax is as follows:

switch (comparison variable) {

case targetValue:

            expected task

            break;

case targetValue2:

            expected task

            break;

}

A break statement is used to break from the switch operation when one condition has been fulfilled to avoid checking for other conditions. Breaking from the statement saves time and memory resources.

Let’s Try Something

In this activity, we will read values from a light-dependent resistor (LDR) and then control light based on the values. We will use the if statement and switch case to show how they can be used to achieve a similar operation. We will read a value from the LDR and use the map function to map it out to values that can be used to determine if it’s day, night, or in between. The values obtained will be used to control light and print messages on the computer screen using the serial print function.

Hardware

  • Arduino board
  • Light-dependent resistor
  • 10k ohm resistors
  • Jumper wires
  • Breadboard
  • Led

Circuit

The LDR is connected to the Arduino pin A0 using a voltage divider circuit using a resistor to complete the other part of the circuit as shown below. The analog read function is used to get values from the LDR based on the light intensity. The analog read function returns a value between 0 to 550 in a well-lit outdoor environment.

Let’s Code

int led = 3;

void setup () {

// initialize serial communication:

Serial.begin(9600);

pinMode (A0, INPUT);

pinMode (led, OUTPUT); //set the led pin to output

}

 

void loop () {

// read the sensor:

int sensorReading = analogRead(A0);

// map the sensor range to a range of four options (0,1,2,3):

int range = map (sensorReading, sensorMin, sensorMax, 0, 3);

 

// do something different depending on the range value:

switch (range) {

case 0:    // your hand is on the sensor

Serial.println("dark");

digitalWrite (led, LOW); //switch off the light

break;

case 1:    // your hand is close to the sensor

Serial.println("dim");

break;

case 2:    // your hand is a few inches from the sensor

Serial.println("medium");

break;

case 3:    // your hand is nowhere near the sensor

Serial.println("bright");

digitalWrite (led, HIGH); //Switch on the light

break;

}

delay (10) ;// delay in between reads for stability

}

Same thing with if statement

int led = 3;

void setup () {

// initialize serial communication:

Serial.begin(9600);

pinMode (A0, INPUT);

pinMode (led, OUTPUT); //set the led pin to output

}

 

void loop () {

// read the sensor:

int sensorReading = analogRead(A0);

// map the sensor range to a range of four options (0,1,2,3):

int range = map (sensorReading, sensorMin, sensorMax, 0, 3);

 

// do something different depending on the range value:

If (range==0) {

Serial.println("dark");

digitalWrite(led, HIGH); //switch on the light

}

else if (range==1) {

Serial.println("dim");

}

else if (range==2) {

Serial.println("medium");

}

else if (range==3) {

Serial.println("bright");

digitalWrite (led, LOW); //Switch off the light

}

delay (10); // delay in between reads for stability

}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *