If "this", do "that" (if)

As we continue in our experiments, let's now learn about logical conditions, opening a lot of possibilities when programming the Arduino.

!Important Informations

This experiment introduces the if instruction, one of the fundamental programming structures, not only important for computers, but also for the majority of decisions we make in our lives. If it is cloudy outside, then take an umbrella – or if you are hungry, then make a sandwich. Just like us, the computers use if to make choices.

An if requires two components: a condition and a consequence. The condition is a value or mathematical operation, which is evaluated as true or false. If the condition is evaluated as true, the consequence happens. The consequence can be a block of code of any size, either a single line or hundreds of lines.

If the condition is false, then the consequence is ignored and the program starts to run the code that is right after the if statement.

!Identifying the Components

Buzzer Interface

!Code Components

On Arduino, the anatomy of an if statement in general looks like the following:

if ( [condition] ) { [consequence] };

On the example below, the condition is used to identify if the input on pin 2 is activated – for example, in case a button connected to pin 2 is pressed.

if (digitalRead(2) == LOW) {
    // Do something because the button is pressed
}

The condition can be any declaration that can be evaluated as true or false. We can build conditional expressions, and for that we usually use comparison operators. There are various kinds of comparison operators, among which:

Comparison Operators Code Representation
Less than <
Greater than >
Less or equal to <=
Greater or equal to =>
Equal to ==
Different from !=

!Running on Arduino

Run the code below on your Arduino in order to make the buzzer play the musical notes using if.

// Pin connected to buzzer
int buzzer = 5;

int frequency = 0;

void setup() 
{
    // Defines the buzzer pin as output 
    pinMode(buzzer, OUTPUT);    
}

void loop() 
{
    frequency = frequency + 1;

    if (frequency > 392) { // If it has reached the value of the last note
        frequency = 0;     // Makes the variable equal to zero
    }
    
    if (frequency == 261) {
        // The buzzer sounds at a frequency relative to the musical note C in Hz
        tone(buzzer, frequency);    
        // Waits for some time to turn off
        delay(200);
        // Turns the buzzer off
        noTone(buzzer);
    }
    
    if (frequency == 293) {
        // The buzzer sounds at a frequency relative to the musical note D in Hz   
        tone(buzzer, frequency);             
        delay(200);    
        noTone(buzzer);
    }
    
    if (frequency == 329) {
        // The buzzer sounds at a frequency relative to the musical note E in Hz
        tone(buzzer, frequency);      
        delay(200);
        noTone(buzzer);
    }
    
    if (frequency == 349) {
        // The buzzer sounds at a frequency relative to the musical note F in Hz
        tone(buzzer, frequency);    
        delay(200);    
        noTone(buzzer);
    }
    
    if (frequency == 392) {
        // The buzzer sounds at a frequency relative to the musical note G in Hz
        tone(buzzer, frequency);            
        delay(200);
        noTone(buzzer);
    }
}

!Your Turn!

Try to modify the code above using the yellow LED to sinalize that the variable has value 0 and the green LED to indicate that the variable has reached value 392.

Answer

// Pin connected to buzzer
int buzzer = 5;

// Matches the LEDs to the corresponding pins
int yellowLed = A1;
int greenLed = A2;

int frequency = 0;

void setup() 
{
    // Defines the buzzer pin as output
    pinMode(buzzer, OUTPUT);    
}

void loop() 
{
    if (frequency == 0) {
        // Turns the LED on
        digitalWrite(yellowLed, HIGH);                 

        // Waits a while    
        delay(200);                           

        // Turns the LED off 
        digitalWrite(yellowLed, LOW);
    }

    frequency = frequency + 1;

    if (frequency > 392) { // If it has reached the value of the last note
        frequency = 0;     // Makes the variable equal to zero
    }
    
    if (frequency == 261) {
        // The buzzer sounds at a frequency relative to the musical note C in Hz
        tone(buzzer, frequency);    
        // Waits for some time to turn off
        delay(200);
        // Turns the buzzer off
        noTone(buzzer);
    }
    
    if (frequency == 293) {
        // The buzzer sounds at a frequency relative to the musical note D in Hz   
        tone(buzzer, frequency);             
        delay(200);    
        noTone(buzzer);
    }
    
    if (frequency == 329) {
        // The buzzer sounds at a frequency relative to the musical note E in Hz
        tone(buzzer, frequency);      
        delay(200);
        noTone(buzzer);
    }
    
    if (frequency == 349) {
        // The buzzer sounds at a frequency relative to the musical note F in Hz
        tone(buzzer, frequency);    
        delay(200);    
        noTone(buzzer);
    }
    
    if (frequency == 392) {
        digitalWrite(greenLed, HIGH);
      
        // The buzzer sounds at a frequency relative to the musical note G in Hz
        tone(buzzer, frequency);            
        delay(200);
        noTone(buzzer);
        
        digitalWrite(greenLed, LOW);
    }
}