Add an LED and another button so that each button controls one LED
- Now in your system, there is one button that can control the onboard LED.
- Make the system so that there are 2 inputs which control 2 LEDs separately.
Pre-requisite:
- Successfully control the onboard LED using a button via Add a button.
Objectives:
- Achieve an understanding of digital inputs and outputs.
Descriptions:
- To add an LED to Arduino board, you need to know what kind of LED you are using.
( The kit usually come with the correct value for the kit LED, but if you bought your own LED, please reference to : http://www.evilmadscientist.com/2012/resistors-for-leds/ ) Now connect the LED to Arduino and make it work
Please reference to Arduino Blink Tutorial (if you are using a breadboard, please refer to the picture here : https://www.circuitbasics.com/arduino-basics-controlling-led/)
(Note that in the tutorial, it's sharing the pin 13 with the external LED, we don't want that. So make the external LED use another pin, say pin 12 )Now test if both LEDs are working by making them both blink. (Try figure out the code yourself, don't look at the code below yet)
Modified blink/* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */ // Pin 13 and 12 has an LED connected on most Arduino boards. // give it a name: int led0 = 13; int led1 = 12; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led0, OUTPUT); pinMode(led1, OUTPUT); } // the loop routine runs over and over again forever: void loop() { digitalWrite(led0, HIGH); // turn the LED0 on (HIGH is the voltage level) digitalWrite(led1, LOW); // turn the LED1 off delay(1000); // wait for a second digitalWrite(led0, LOW); // turn the LED0 off by making the voltage LOW digitalWrite(led1, HIGH); // turn the LED1 on delay(1000); // wait for a second }
- Now add another button to the system, two button in total. If you forgot how to connect them, please refer to Add a button tutorial.
- So now you have two buttons connected to Arduino, one connected to pin2, and one connected to pin3
Modify the programme so that each button control one LED (Try figure out the code yourself from the code in Add a button, don't look at the code below yet)
Controlling 2 LEDs with 2 buttons// constants won't change. They're used here to // set pin numbers: const int button0Pin = 2; // the number of the pushbutton pin const int button1Pin = 3; // the number of the pushbutton pin const int led0Pin = 13; // the number of the LED pin const int led1Pin = 12; // the number of the LED pin // variables will change: int button0State = 0; // variable for reading the pushbutton status int button1State = 0; void setup() { // initialize the LED pin as an output: pinMode(led0Pin, OUTPUT); pinMode(led1Pin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(button0Pin, INPUT); pinMode(button0Pin, INPUT); // turn LED off initially: digitalWrite(led0Pin, LOW); digitalWrite(led1Pin, LOW); } void loop(){ // read the state of the pushbutton value: button0State = digitalRead(button0Pin); button1State = digitalRead(button1Pin); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (button0State == HIGH) { // turn LED on: digitalWrite(led0Pin, HIGH); }else{ // turn LED off: digitalWrite(led0Pin, LOW); } if (button1State == HIGH) { // turn LED on: digitalWrite(led1Pin, HIGH); }else{ // turn LED off: digitalWrite(led1Pin, LOW); } }
Change the programme so that if you press one button, the corresponding LED turns on for 2 seconds and then turn off
Delayed off// constants won't change. They're used here to // set pin numbers: const int button0Pin = 2; // the number of the pushbutton pin const int button1Pin = 3; // the number of the pushbutton pin const int led0Pin = 13; // the number of the LED pin const int led1Pin = 12; // the number of the LED pin // variables will change: int button0State = 0; // variable for reading the pushbutton status int button1State = 0; void setup() { // initialize the LED pin as an output: pinMode(led0Pin, OUTPUT); pinMode(led1Pin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(button0Pin, INPUT); pinMode(button0Pin, INPUT); // turn LED off initially: digitalWrite(led0Pin, LOW); digitalWrite(led1Pin, LOW); } void loop(){ // read the state of the pushbutton value: button0State = digitalRead(button0Pin); button1State = digitalRead(button1Pin); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (button0State == HIGH) { // turn LED on: digitalWrite(led0Pin, HIGH); // wait for 2 seconds; delay(2000); // turn LED off: digitalWrite(led0Pin, LOW); } if (button1State == HIGH) { // turn LED on: digitalWrite(led1Pin, HIGH); // wait for 2 seconds; delay(2000); // turn LED off: digitalWrite(led1Pin, LOW); } }