Introduction
Arduino: If-else Statement: Helpful 1 way to learn is the topic of this article in which I will develop the importance of The if-else statement is conditional in Arduino programming.
And explain that It is used to execute one of two blocks of code, depending on the value of a boolean expression.
The if(condition){statement}: Arduino: If-else Statement
Arduino If Statement is an example of a control flow statement in programming languages. It is used to make decisions and run different pieces of code based on the decision made.
The if() statement starts with “if.” It is followed by a condition, which can be any expression that evaluates whether true or false. If the expression evaluates to true, then the block of code that follows will be executed.
If it evaluates to false, then the block of code following will not be achieved.The condition evaluates as either true or false. If it evaluates to true, the code between the { } will execute. If it evaluates to false, the code between the { } will not run.
Conditional structure in programming
Conditional statements in c programming are used for decision-making.The structure of the conditional statement or the Arduino: If-else Statement is:if(condition){}else if ( condition){statement}else {statement}
Operators and if statement
Operators are the building blocks of any programming language. They are used to perform mathematical, logical, and relational operations. The if statement is a conditional statement used to control the flow of the program based on a specific condition.
The condition uses mathematical, logical, and relational operations to verify if the condition is met to execute the command or execute something else.
Serial monitor application1
Open the Arduino application, connect the Arduino to the computer and write the following code.void setup() {Serial.begin(9600);Serial.println(7==7);}void loop() {}
Comment on the code
*Upload the code from the Arduino Uno application.*Open the serial monitor*The serial monitor display 1*This means that if the result is true and 4 is equal to 4, the serial monitor display 1
Edit the code
write the following code.void setup() {Serial.begin(9600);Serial.println(7==9);}void loop() {}
Comment on the code
*Upload the code from the Arduino Uno application.*Open the serial monitor*The serial monitor display 0*This means that if the result is false and 7 is not equal to 9, the serial monitor display 0
Serial monitor application 2
Open the Arduino application, connect the Arduino to the computer and write the following code.int x = 27;int y = 7;void setup() {Serial.begin( 9600 );if(x>y){Serial.println(“true”); }}void loop() {}
Comment on the code
- Upload the code from the Arduino Uno application
- Open the serial monitor
- The serial monitor display true
- This means that if the condition is true (x>y), the serial monitor display true
Edit the code
int x = 27;int y = 7;void setup() {Serial.begin(9600);if(x<y){Serial.println (“true”); }else{Serial.println (“false”); }}void loop() {}
Comment on the code
- Upload the code from the Arduino Uno application
- Open the serial monitor
- The serial monitor display false
- This means that if the result is false and (x>y), the serial monitor displayfalse
Serial monitor application 3
Open the Arduino application, connect the Arduino to the computer and write the following code.int a = 5.5;void setup() {Serial.begin ( 9600 );if(a>0){Serial.println (“a is positive”); }else{Serial.println (“a is negative”); }}void loop() {}
Comment on the code
- Upload the code from the Arduino Uno application
- Open the serial monitor
- The serial monitor display: a is positive
- This means that if the result is true and (a>0), the serial monitor displaya is positive
Edit the code
Open the Arduino application, connect the Arduino to the computer and write the following code.int a = -5.5;void setup() {Serial.begin ( 9600 );if(a>0){Serial.println(“a is positive”); }else{Serial.println(“a is negative”); }}void loop() {}
Comment on the code
- Upload the code from the Arduino Uno application
- Open the serial monitor
- The serial monitor display: a is negative
- This means that if the result is false and (a<0), the serial monitor displaya is negative
Pushbutton Arduino project
We will use the pushbutton as an input for this project, and the LED as an output. If the user pushes the button, the result is true, and the LED is on.
The other case is that the result is false when the user does not push the button and the LED is off.
The code related to the project
Introduction
The components needed for this project are:
- Arduino Uno board
- Breadboard
- Pushbutton
- Two jumper wires
Circuit installation
Circuit installation
*Plug the red jumper wire into the pin 8 of the Arduino board*Connect the second side of the red jumper wire to the A1 leg of the pushbutton.*Plug the red-blue jumper wire into the ground pin of the Arduino board*Connect the second side of the blue jumper wire to the B1 leg of the pushbutton.
The code of the project
const int pushButton = 8;const int led=13;void setup() {pinMode ( pushButton, INPUT_PULLUP);pinMode( led, OUTPUT);}void loop() {if(digitalRead ( pushButton ) == LOW ){digitalWrite ( led, HIGH);}else{digitalWrite ( led,LOW);}}
LEDs Arduino Uno project
Circuit installation
- Arduino Uno board
- Breadboard
- Pushbutton
- Three blue jumper wires
- Three red jumper wires
The code of the project
// C++ codeint pin2 =2 ;int pin8 =8 ;int a =15 ;int b =6 ;void setup(){pinMode ( pin2, OUTPUT);pinMode ( pin8, OUTPUT);}void loop(){if(a>b){digitalWrite ( pin2, HIGH);digitalWrite ( pin8, LOW);}}
Comment on the project
The LED on the right lights because the first condition is met.
The first edition of the LEDs Arduino Uno project
int pin2 =2 ;int pin8 =8 ;int a =5 ;int b =21 ;void setup(){pinMode (pin2, OUTPUT);pinMode ( pin8, OUTPUT);delay(1000);}void loop(){if(a>b){digitalWrite ( pin2, HIGH);digitalWrite ( pin8, LOW);}else{digitalWrite ( pin2, LOW);digitalWrite ( pin8, HIGH); }}
Comment on the project
The LED on the left lights because The second condition is met.
The second edition of the LEDs Arduino Uno project
The code
int pin2 =2 ;int pin8 =8 ;int a =5 ;int b =21 ;void setup(){pinMode (pin2, OUTPUT);pinMode ( pin8, OUTPUT);delay(1000);}void loop(){if(a==b){digitalWrite ( pin2, HIGH);digitalWrite ( pin8, LOW);}else{digitalWrite ( pin2, LOW);digitalWrite ( pin8, HIGH); }}
Comment on the project
The LED on the left lights because The second condition is met.The second editionof the LEDs Arduino Uno projectint pin2 =2 ;int pin8 =8 ;int a =5 ;int b =21 ;void setup(){pinMode (pin2, OUTPUT);pinMode ( pin8, OUTPUT);delay(1000);}void loop(){if(a!=b){digitalWrite ( pin2, HIGH);digitalWrite ( pin8, LOW);}else{digitalWrite ( pin2, LOW);digitalWrite ( pin8, HIGH); }}
Comment on the project
The LED on the right lights because The first condition is met.
The third edition of the LEDs Arduino Uno project
// C++ codeint pin4 =4 ;int pin7 =7 ;int pin11 = 11;int pinState11= digitalRead( pin11 );int a = 15 ;int b = 5 ;void setup(){pinMode( pin4, OUTPUT );pinMode( pin7, OUTPUT );}void loop(){if(a!=b){digitalWrite( pin4, HIGH);digitalWrite( pin7, LOW);delay(1000);}else{digitalWrite( pin4, LOW);digitalWrite( pin7, HIGH); }if(pinState11 == HIGH){digitalWrite( pin7, HIGH);delay(2000);}}
Combine two conditions to execute if statement
This section will explore how to execute if statement based on two conditions. The first condition is whether the variable a is equal to 10, and the second condition is whether the variable b is equal to 20.
If we want to execute an if statement only when both of these conditions are satisfied, we can use the following code:
if (a == 10 && b == 20) {} else {}Code example// C++ codeint pin4 =4 ;int pin7 =7 ;int pin11 = 11;int pinState11= digitalRead( pin11 );int a = 15 ;int b = 5 ;void setup(){pinMode( pin4, OUTPUT );pinMode( pin7, OUTPUT );}void loop(){if(a!=b && a<b){digitalWrite( pin4, HIGH);digitalWrite( pin7, LOW);delay(1000);}else{digitalWrite( pin4, LOW);digitalWrite( pin7, HIGH); }if(pinState11 == HIGH){digitalWrite( pin7, HIGH);delay(2000);}}
Conclusion
The if() statement is a conditional statement. This means that it will only execute the statement if the condition is true.
An if statement can have an optional else clause that executes when the condition is false. The else clause must follow after endif at the end of the if() statement and have a corresponding else if() or else().
The syntax for an if() statement is as follows:if (condition) {statement;} else {statement; }
To conclude if(condition){statement} can be used in many different ways, including:
-To create loops, which are repeated statements executed until the condition becomes false.-To create conditional statements, which execute a certain statement only when the condition becomes true.
-To create decisions that execute one of two or more statements based on whether the condition becomes true or false.