Objectives: Learn how a button works Be able to wire and program a button to make LEDs flash By the end of this session: You will know how to write a program and connect a button to change the ways that LEDs flash, as well as how to use ‘IF’ statements. © S. Pithouse for use by Warwick Technology Volunteers Page: 1 Arduino Uno Breadboard Jumper Wires LEDs Resistors When the button is pressed, a connection is made between the two legs of the device, this can be detected by the Arduino and can be used to provide an input and to affect the way that a program runs. Buttons © S. Pithouse for use by Warwick Technology Volunteers Page: 2 1) Insert the button and resistor into the breadboard as in the diagram. Make sure that there is a black stripe on the resistor! 2) Connect a wire between the leg of the button and the GND pin on the Arduino 3) Connect a wire between the other leg of the button and the ‘9’ pin and, then connect a wire between the other leg of the resistor and the 5V pin, as in the diagram © S. Pithouse for use by Warwick Technology Volunteers Page: 3 1) Plug the USB cable into the Arduino and Computer, then open the Arduino Program. 2) Visit the website—http://bit.ly/1woHjXc—and copy and paste the ‘Button template program’ into the Arduino window. 3) The program sets ‘button’ (pin 9) as an INPUT in the setup function. It then enters the loop and saves the value read from the button into a variable called buttonState. This is 1 if the button is not pressed, or 0 if the button is pressed. The final part checks to see if the buttonState value is 0 (pressed), then it turns the built-in LED on, else it turns the built in LED off. © S. Pithouse for use by Warwick Technology Volunteers Page: 4 4) Upload the program by clicking on the upload button. 5) Your LED should turn on when you press the button and then turn off as soon as you let go. Now, add the following lines of code, to make the LED flash when you hold the button down. 6) Upload the program by clicking on the upload button. 7) The LED should flash when you press the button. © S. Pithouse for use by Warwick Technology Volunteers Page: 5 8) Try connecting another button to the breadboard as shown in the diagram, then add the following lines of code: You will see that the green wire has been added to connect the two buttons together, but since the first button’s leg was connected to GND, both LED’s legs become connected to GND. The blue wire was added to connect the button to pin 8 on the Arduino. © S. Pithouse for use by Warwick Technology Volunteers Page: 6 9) We need to tell the program that we have added another button. Firstly, we need to give it a name and make a variable for its value to be stored We need to tell it that ’newButton’ is an input We then need to save the button state Finally, make the LED flash, only when both the buttons are pressed. 10) Upload the program by clicking on the upload button. © S. Pithouse for use by Warwick Technology Volunteers Page: 7 11) You should see the LEDs flashing only when you press both buttons. This is because the if statement means, if the first button is pressed (==0) AND the second one is also pressed (==0), then flash the lights. Extension task If you finish all of these steps, try making the LED flash when either button is pressed, or make it flash all the time, but when the buttons are pressed, they flash faster. © S. Pithouse for use by Warwick Technology Volunteers Page: 8