While

In this experiment, we are going to learn about another logic condition, which will give us more programming possibilities.

!Important Information

Let’s introduce the while instruction, one of the most interesting programming structures that are available. You may not notice, but it is present in our lives. For instance: while you are very asleep, you should sleep. While you are still hungry, eat.

Like if, the declaration requires two components to be complete: a stop condition and a consequence. The while() logic works the following way:

While the stop condition is not satisfied, a consequence runs repeatedly. Only when the stop condition is reached that the program exits the while() loop and moves on to the rest of instructions on the program. It can be said that the way while() works is similar to the loop() function, with the difference that the loop() function does not have the stop condition, that is, "runs forever".

!Identifying the Components

Interface’s Right Button

!Code Components

Condition Execution: while( [stop condition] ) { [consequence] };

The Anatomy of while:

while(It is not weekend)
{
  Work from 06:00AM to 06:00PM
}

This means that while it is not weekend, we must work.

The stop condition can be any kind of declaration that can be evaluated as true or false. In order to build these conditional declarations, we generally use the comparison operators, as already explained on if experiment.

!Running on Arduino

The code below makes the yellow LED turn on when the RIGHT button is not pressed.

// Pin connected to pushbutton
int buttonPin = A0;
// Variable to check the button state
int buttonState;
// Matching the LEDs to the corresponding pins
int yellowLed = A1;

void setup()
  {
  // Initialise the LED pin as output
  pinMode(yellowLed,OUTPUT); 
  // Defines the button pin as input
  pinMode(buttonPin, INPUT);
}

void loop() {
  
  // Checks if the button state has changed
  buttonState = analogRead(buttonPin);
  // Cleans the noise      
  buttonState = 1023;
  // While no button is being pressed, reads again
  while (buttonState >= 897)
  {
    // Turns the Yellow LED on
    digitalWrite(yellowLed, HIGH);
    buttonState = analogRead(buttonPin);
  } 
  if (buttonState < 897)
    { 
        // if the RIGHT button is being pressed
        while (buttonState < 69)
          {
            // Turns the yellow LED off
            digitalWrite(yellowLed, LOW);
            buttonState = analogRead(buttonPin);        
          } 
      }  
}  

!Your Turn!

Try to modify the code so that the green LED turns on and the buzzer plays a sound.

Answer

//Pin connected to the pushbutton
int buttonPin = A0;
//Pin connected to the buzzer
int buzzer = 5;
//Variable to check the button state
int buttonState;
// Matching the LEDs to the corresponding pins
int yellowLed = A1;        
int greenLed = A2;

void setup()
  {
  // Initialises each LED pin as output
  pinMode(yellowLed,OUTPUT);
  pinMode(greenLed,OUTPUT);
  // Defines the buzzer pin as output 
  pinMode(buzzer,OUTPUT); 
  // Defines the button pin as input
  pinMode(buttonPin, INPUT);
}

void loop() {
  
  // Checks if the button state has changed
  buttonState = analogRead(buttonPin);
  // Cleans noises      
  buttonState = 1023;
  // While no button is pressed, reads again
  while (buttonState >= 897)
  {
    // Turns the yellow LED on
    digitalWrite(yellowLed, HIGH);
    digitalWrite(greenLed, LOW);
    buttonState = analogRead(buttonPin);
  } 
  if (buttonState < 897)
    { 
        // If the RIGHT button is being pressed
        while (buttonState < 69)
          {
            //Turns the yellow LED off
            digitalWrite(yellowLed, LOW);
            digitalWrite(greenLed, HIGH);
            // Makes the buzzer play the musical note G in Hz
            tone(buzzer,392);            
            delay(200);
            noTone(buzzer); 
            buttonState = analogRead(buttonPin);        
          } 
     
      }  
}