In the previous lesson, we looked at the while loop and the push-button, where we could program actions while nothing was happening. If you have built the circuit from the previous lesson, you don´t need to change anything. These are the connection diagrams:
The push-button and LED modules are connected to digital pins 5 and 3, respectively. It can also be done with LED built into the board, which is connected to pin 13.
The do-while loop
The do-while loop is similar as it waits for the event, but there are certain differences: the while loop is executed while the condition is being fulfilled, whereas the do-while is executed at least once after the condition has been fulfilled. You can see two diagrams below, the while loop on the left and the do-while on the right:
![]() |
![]() |
The do-while syntax can be seen here:
1 2 3 |
do{ // your code goes here }while(condition); |
First we look at the part in brackets where we write the code to be executed, then the condition. Once the code has been written within the do { } brackets, it will check if the condition has been met or not. If so, it will repeat this process until the condition is no longer fulfilled, whereas the while loop checks before executing the code. This is the only difference between the while and do-while loops. Following the previous example of code, let´s take a look at how the Arduino code for the do-while loop would be. If we assume that there is a whole number variable and that in another part of our program it is updated, the do-while loop would be like this:
1 2 3 4 5 6 |
do{ digitalWrite(2,HIGH); delay(1000); digitalWrite(2,LOW); delay(1000); }while(a<10); |
You can see the reference page for the do-while loop here.
The push-button
To find out how to use the button and the digital inputs go to the previous lesson, the-while-loop-and-the-push-button.
The code
If you have connected the elements as described at the beginning, meaning the button module to pin 5 and the LED module to pin 3, we are ready to start programming. First we will declare some variables for storing the number of the pin we have connected to the hardware. This practice is really useful because if we change the connection diagram, we will only have to change the pin number used. To do this we will use this code:
1 |
int pinButton = 5, led = 3; |
So instead of using the pin number, we can use its name, making it easier to program, especially if we use a keyword as the name. Once we have declared the variables of the pins, we need to indicate how each pin will function, like this:
1 2 3 4 |
void setup(){ pinMode(pinButton, INPUT); pinMode(led, OUTPUT); } |
This code is equivalent to:
1 2 3 4 5 |
void setup() { pinMode(5,INPUT); //push button pin pinMode(3,OUTPUT); // LED pin } |
Once that is done, we will create a program to make the LED blink with different patterns for when the button is pressed and not pressed. The code would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
int pinButton = 5, led = 3; //Declaration of the variables void setup() { Serial.begin(9600); // We start the serial communication at 9600 bauds pinMode(pinButton, INPUT); // We set the pin connected to the button as input pinMode(led, OUTPUT); // We set the pin connected to the LED as output } void loop() { boolean firstTime = false; // We start the Boolean firstTime variable with the value false do { // do-while loop start if (firstTime == false) { // If the firstTime variable is false, then we enter firstTime = true; } else { // If the firstTime variable is true, then we enter firstTime = false; } // Serial.println("Inside of do-while"); // Use a message to indicate that the do-while loop is being executed if (firstTime) { // if reads the value stored in the firstTime variable. If true or 1, then we enter blinky(1, 1000); // Called the blinky function with the time parameters = 1 and delay_led = 1000 } else { // if reads the value stored in the variable. If false or 0, then we enter blinky(20, 50); // Called the blinky function with the time parameters = 20 and delay_led = 50 } } while (digitalRead(pinButton)); // Reading the condition for repeating the do-while loop. If the button is pressed, it will return // to repeat the code within the do-while loop. // Serial.println("Outside of do-while"); // Use a message to indicate that the do-while loop has been executed. } /* Declaration of the void function (returns no result) with the name blinky. It has two parameters: times (whole number variable) that determines the number of times the blinking will be produced. delay_led (whole number variable) that determines the time between the two states of the LED: Determines the frequency of the blinking. NB: The function switches the LED on and off, so the times variable times represents the number of on/off cycles (times = n it will switch the LED on and off n times) */ void blinky(int times, int delay_led) { for (int i = 0; i < times; i++) { // For loop and declaration of variable i. The loop will be executed from i to times - 1 Serial.print("iteration "); // Prints information on the number of times for is executed with the next line. Serial.println(i); // Prints the number of times the for loop is executed digitalWrite(led, HIGH); // Enables the pin connected to the LED delay(delay_led); // Waiting time indicated on calling the function with the delay_led parameter digitalWrite(led, LOW); // Disables the pin connected to the LED delay(delay_led); // Waiting time indicated on calling the function with the delay_led parameter } } |
In this code, we can see how firstTime is used as a condition, without any other type of operation or comparison. The if control structure waits for a true or false response or 1 or 0, which is exactly what we are doing since firstTime is a boolean variable. For a reminder on variables, you can go to this lesson or the Arduino reference page. We also use the same function to create a totally different pattern on the LED, making the code shorter and easier for everyone to understand and modify. If you want to see the messages that the program was sending in order to know whether it´s within the do-while loop or not, delete the comments in lines 21 and 31 of the code.
Now try this exercise. Create a copy of the project, search for and manually modify all fields that contain the LED variable using the number of the pin connected to the LED (which is pin 13 in the video). Then connect the LED to another pin in the original project and modify the value of the LED variable at the beginning of the code. Now can you see the power of assigning pins to variables?