 # To infinity… meanwhile don´t press me

In one of the previous lessons, we learnt how to program the for loop, which was basically used for repeating a series of actions a certain number of times. However, there will be times when rather than repeating an action a certain number of times, we want it to be repeated until something happens. For example, if I want a program to switch an LED on and off 10 times, the most obvious thing would be to do it as with the for loop. But what about if I wanted it to switch on and off until the button is pressed? Although there are obscure ways of doing this using the for loop (let´s see if you can think of one), there are easier ways. An intuitive way to put it would be like this: While the button is not pressed, switch the LED on and off. The diagram would look like this: Let´s take a look at the Arduino code for the while loop. Let´s assume that there is a whole number variable, and that in another part of our program it is updated, the while loop would be like this:

This code sets digital pin 2 at 5 volts for 1 second, and at 0 volts for another second (if a LED is connected it will blink) while it is less than 10. You can see the reference page for the while loop here.

# Let´s learn about the push-button

We have seen earlier how to write on a digital pin, setting it at 5 volts or 0 volts, and as an example we use an LED. Now we will learn how to read a value of a digital pin, which will thus be 5 volts or 0 volts. As an example we will use a push-button. Buttons are clearly digital peripherals, as they can take two values, pressed or not pressed. Well,  it basically sets one digital pin at 5 volts and the other at 0. Depending on the design of the button, it will be 5 volts when pressed or 0 volts when not pressed. Let´s have a look at how the button works: You can see in the diagram that the button has three pins. One connected at 5V and another to earth (GND) and the third to the signal pin (In). In this diagram, if you have studied electronics, you will check that there are 5 volts in the signal pin when the button is pressed, on closing the circuit. But now take a look at this diagram: Here the signal pin will have 0 volts when the button is pressed. So how do we program it in Arduino? Let´s assume that the next component connection is like this: First let´s declare pin 5, which is connected to the button, as input, and pin 3, where the LED is connected, as output:

Once this is done, we will create a program to make the LED blink while the button is pressed and the opposite if not pressed. How do we know if the button is pressed or not? Our button marks 5V when pressed, so the function that reads the digital pins: digitalRead() should return 1. (for more information go to the function reference page. So the code would be:

This program can be modified using a variable. Let´s see how:

The most important thing to bear in mind is that the value of the push-button variable should be updated at each iteration. Otherwise, the value would never change within the loop, independently of whether the button is pressed or not, so the program would never leave the loop. I´ll set you a challenge. Have a go at making a program that works as a chronometer. Use the LCD and start counting (and displaying) each second that passes until the button is pressed. Then when it´s pressed, make the LCD display the last value and keep it on screen. We´ll see if you can manage it!