- In our example, we are checking if myCounter is equal to 100.
- 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.
- 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:
- 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.
- One important remark I would like to add on if-else statement is to be careful from some common mistake.
- 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.
Dear visitor. Click on "Follow" button from right side above and sign in with your Gmail account. Write unlimited comments on every post. 100% Free