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:
zum-3led-4pulsThe 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:

While-loop-diagram Flowchart- dowhile

The do-while syntax can be seen here:

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:

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:

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:

This code is equivalent to:

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:

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?