Type Here to Get Search Results !

How To Use Control Statement IF Else Function In Arduino Programming

The if-else statement is the simplest of the control structures and is among the most commonly used method for decision making. The basic syntax is as follow:



  1. In our example, we are checking if myCounter is equal to 100.
  2. If this is true than the lines inside will be executed and if the condition is not met, than we have the option to use another block of code under the else.
  3. in addition, the condition itself can be a combination of several sub conditions. so as you see, this is simple and straight forward control statement.


A very common use for "if" statement is to read the state of a digital or analog pin and perform an action based on the status. so, in this simple example that reads the value of a pushbutton switch connected to specific pin number that we have in the "switchPin" variable and then assigning that value to the variable ledSwitch and then tests the condition.

If LED Switch is equal to the constant HIGH, meaning someone is pushing the pushbutton then it perform a block of code:

  1. Change the state of the digital pin 13 to HIGH using the digitalWrite() function which would have the effect of turning ON the “L” LED on the Arduino interface board, and then wait for 1 second and write again to digital pin 13 the value LOW to turn OFF back again the "L" LED.
  2. One important remark I would like to add on if-else statement is to be careful from some common mistake.
  3. Where we use a single equal sign in test statement, which means “make equal to,” instead of a double equal sign (==), which says “test if it is equal to", so please note between them, you sould use the ==


Please watch this practical demonstration of this chapter below to understand it better.


Post a Comment

0 Comments