Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Tip
titleAdd 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:

  1. Successfully control the onboard LED using a button via Add a button.

Objectives:

  1. Achieve an understanding of digital inputs and outputs.

Descriptions:

  1. 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/ )
  2. 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 )

  3. 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)

    Code Block
    languagecpp
    titleModified blink
    linenumberstrue
    /*
      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
    }


  4. 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. 
  5. So now you have two buttons connected to Arduino, one connected to pin2, and one connected to pin3
  6. 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)

    Code Block
    languagecpp
    titleControlling 2 LEDs with 2 buttons
    linenumberstrue
    // 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); 
      }
    }


  7. Change the programme so that if you press one button, the corresponding LED turns on for 2 seconds and then turn off

    Code Block
    languagecpp
    titleDelayed off
    linenumberstrue
    // 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); 
      }
    }