Learn to code

ARDUINO & CODES

Previous Post
Create a For Loop statement in Arduino
Next Post
Understanding Arrays in Arduino

Understanding If / Else statement in Arduino

in Arduino Basics

Controlling a program requires that the programmer specifies one or more conditions to be evaluated or tested by the program. In the previous tutorial, we controlled one led by using input from the Serial Monitor and using for loops. We will abandon the for loop 

So, for this tutorial, we want the user to input his desired color for an LED. We will use two LED’s, a red one and a blue one. In order to this, we need to learn how to use a new coding command called the IF statement.  The IF statement has a clause or group of command between a set of curly brackets. If the condition is met, the code will be executed. The below code gives a general example of how to use the IF statement.

What you will need to know?

Before continuing with this guide, you may want to check out any topics from the list below that sound unfamiliar.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 5
LED LED × 2
220 ohm resistor 220 ohm resistor × 2

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

Generic Code IF / ELSE

The If / ELSE statement allows you to control the flow of your code by allowing multiple tests. An else clause will be executed if the condition in the IF statement is False. 

Each IF test will proceed to the next until a true test is encountered. When a true test is found, the code between the curly brackets will be executed. IF no test is true, the ELSE part is executed.

Remember that an unlimited number of ELSE / IF statement can be used.

The Arduino would only execute the code if the condition was true. Otherwise, it would jump to the first line of code after the closing curly bracket.

Below is the circuit diagram that we will be using and the code. After the coding part, we will explain how the code works.

if ( some condition) {
 
//put all your commands between the curly brackets
 
}
else {
// put all your commands here if the condition in the IF statement is False
}

Controlling 2 LED’s by user input

// Sketch: Blinking two LEDs by user
int redPin = 10; //set red LED pin to 10
int bluePin = 11; //set blue LED pin to 11
String colorChoice; //Will hold users input of color choice
void setup() {
  // put your setup code here, to run once:
  pinMode(bluePin, OUTPUT); //Set redPin to be an output
  pinMode(redPin, OUTPUT); //Set greenPin to be an output
  Serial.begin(9600); //Turn on Serial port
}
void loop() {
  Serial.println("What color do you want to blink up, red or blue?"); //Prompt user for color
  while (Serial.available() == 0) { } //Wait for input
  colorChoice = Serial.readString();
  if (colorChoice == "red") {
    digitalWrite(redPin, HIGH);
    delay(1000);
    digitalWrite(redPin, LOW);
    delay(1000);
  }
  if (colorChoice == "blue") {
    digitalWrite(bluePin, HIGH);
    delay(1000);
    digitalWrite(bluePin, LOW);
    delay(1000);
  }
   if (colorChoice!="red" && colorChoice != "blue") {
    Serial.println("That is not a valid color choice, please try again");
    Serial.println("");
     }
}

Code Explanation

Notice, the code above is using various new statements. What we want to do is write a program that will ask the user what color of the LED should be turned on. The choices are Red or Blue. If the choice is made the LED will turn off while the other LED will turn off.  To do this we have to start from the begin.

Firstly, we need to define the global variables for the 2 LEDs.

Secondly, we need to start the communication between the Arduino Board and the Serial Monitor. Furthermore, we need to set the LED pins to output. After the setup() part we are ready for the loop() section.

In the loop section, we want the print to the user the question which LED he wants to turn on. Since we are using words and not numbers as input we will make use of Serial.readString() statement to read the choice that the user had made. In the tutorial …. we learned the two basic commands for communicating between the User and the Arduino Board.

OK, now we learned how to use the IF  / ELSE statements we are going further with expanding our knowledge by learning arrays.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu