Arduino - Control Statements

 

İf, Else If, Else’ Structure:

for example, If 30 minutes have passed – turn off lights or “If the moisture content is < 30% – turn the valve on”

Example:


int x

if (x < 40)

{

digitalWrite(led, HIGH);   //  do something

}
else if (x >= 100)

{

digitalWrite(led, LOW);

}

else {

digitalWrite(led, HIGH);

delay(500); // delay

digitalWrite(led, LOW);

}


If …else statement

An if statement can be followed by an optional else statement, which executes when the expression is false.

Example

// Global variable definition
int pump = 5 ;
int bulb = 9 ;

Void setup () {

}

Void loop () {
   // check the boolean condition
   if (pump > bulb) // if condition is true then execute the following statement
   {
      pump++;
   }else {
      bulb -= pump;
   }
}


    

if …else statement

When using an if statement, the code in its body runs only when the if statement evaluates to true. If it evaluates to false, program execution skips the code in the body of the if statement and goes to statement the body of the if statement.




Example:


int mois = 30;
int Temp = 20;

void setup() {
 Serial.begin(9600);
 
 if (mois > 30){
 Serial.println("Moisture content is more than 30%");
 }
 else{
 Serial.println("Moisture content is less than 30%");
 }
 
 if (Temp > 20){
 Serial.println("Temperature  is more than 20%.");
 }
 else{
 Serial.println("Temperature content is less than 20%");
 }
}

void loop() {
 
}






if-else-if statement

The if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.


Example

basic structure:

if (conditional expression 1)  {
Body of the if statement when conditional expression 1 is true
}
else if (conditional expression 2)  {
Body of the else-if statement when conditional expression 1 is false and conditional expression 2 is true
}
else {
Body of the else statement when conditional expression 1 and 2 are both false
}

Example



basic structure:

int Moisture ;
char Soil Moisture;

void setup() {
 Serial.begin(9600);

if (Moisture <= 10){  //soil moisture less or equal to 10
 Soil Moisture = 'Too low irrigate';
 }
 else if (Moisture <= 20){
 Soil Moisture = 'Low irrigate';
 }
 else if (Moisture <= 30){
 Soil Moisture = 'Moderate';
 }
 else if (Moisture <= 40){
 Soil Moisture = 'Average';
 }
 else if (Moisture <= 70){
 Soil Moisture = 'Too High';
 }

Serial.print("Your Soil Moisture is: ");
 Serial.println(Soil Moisture);
}

void loop() {

}







Switch Case Statement Execution

The switch
 statement allows you to choose from among a set of discrete values
 of a variable.  It's like a series of if statements.


basic structure:

switch (variable) { 
   case label:
   // statements
   break;
}

case label: { 
   // statements
   break;
}

default: { 
   // statements
   break;
}












Comments

Popular posts from this blog

Arduino function libraries~Math Library Function

Project Four: Pattern, Blink 4 leds using Arduino board

Project 6: Arduino print hello