Categories
TUTORIALS

Working With Multiple LEDs and Multiple Push Buttons

Buttons and LEDs are basic parts that can be easily programmed with Arduino, but this humble hardware can be critical for creating prototypes that interact with users in a certain way. Colors, flashes of light, and even vibrations can change the way that a user interacts with your prototype.

This tutorial aims to provide a basic shell in terms of both hardware and software that can be built upon in the future with more advanced responses. In this project, users record positive or negative test results with the RED and YELLOW button and call for help with the BLUE button. As such, the response to the blue button push is different than the responses to the data recording buttons.

Parts List

  • Button (Pull-Up Resistor) x3
  • LED x3
  • 330Ω Resistor x3
  • 10KΩ Resistor x3
  • Wire x11

Wiring

Breadboard:

  • Connect the 5v pin to the positive strip of the breadboard
  • Connect a GND pin to the negative strip of the breadboard

Buttons:

  • Connect one side of each button to a digital pin on the Arduino
  • On the same side, connect the button to the positive vertical on the breadboard with a 10KΩ resistor
  • Wire the opposite side of the button to the negative vertical on the breadboard

LEDs:

  • Connect the anode of the LED (long leg) to a digital PMW pin (marked with “~”) on the Arduino
  • Connect the cathode of the LED (short leg with flat edge) to ground using a 330Ω resistor
Here a battery is being used as the power source. The red and white twisted wires are providing power to the breadboard.
Here color coding was useful for keeping track of the parts.

Code

/* 
 * WORKING WITH MULTIPLE LEDS AND MULTIPLE PUSH BUTTONS
 * 
 * Sources: Vilros Ultimate Starter Kit Guide 
 *          
 * This code outlines a simple way to control push buttons and LEDs
 * using if/else statements. It aims to provide you with a framework
 * that can be built out to create more complex displays and initiate
 * additional functions with each button press.     
 */
 
// These are the pin numbers for the buttons
const int button1Pin = 10; 
const int button2Pin = 8; 
const int button3Pin = 4;

//These are the pin numbers for the LEDs
const int led1Pin = 11;
const int led2Pin = 9;
const int led3Pin = 5;

void setup() {
  
  // All of the pins for the BUTTONS need to be set as INPUTS
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  
  // ALL of the pins for the LEDs need to be set as OUTPUTS
  // It is also best practice to set the pins low after setting 
  // them as outputs
  pinMode(led1Pin, OUTPUT);
  digitalWrite(led1Pin, LOW);
  
  pinMode(led2Pin, OUTPUT);
  digitalWrite(led2Pin, LOW);
  
  pinMode(led3Pin, OUTPUT);
  digitalWrite(led3Pin, LOW);

}

void loop() {

  // These variables hold the button states
  int button1State = 0;                  
  int button2State = 0;
  int button3State = 0;

  // Now the loop will begin reading each button state, 
  // and then running through an if/else statement
  // to determine what to do with that button state reading.
  // Note that button states are "LOW" when you press them and
  // "HIGH" when they are not pressed! 
 
  button1State = digitalRead(button1Pin); 
  // Read the state of button 1...
                                        
  if (button1State == LOW){ 
  // ...if the state is LOW, then do the following:
    digitalWrite(led1Pin, HIGH);   // Turn the corresponding LED HIGH
    delay(400);                    
    // This delay means that the LED will stay illuminated for 400 millis
    digitalWrite(led1Pin, LOW);    
    // ...and then it will turn off.
  }
  
  // "else" dictates what happens when the if condition is not true.
  else {
    digitalWrite(led1Pin, LOW); // In this case, the LED remains dark.           
  }                                       
  
  button2State = digitalRead(button2Pin); 
  // Button 2 is controlled with the same type of statement    
                                   
    if (button2State == LOW){
    // initiate light response
    digitalWrite(led2Pin, HIGH);
    delay(400);
    digitalWrite(led2Pin, LOW);
    // Other actions like recording the button push as a data point or 
    // sending a SMS could be initiated in this part of the loop
  }

  else {
    digitalWrite(led2Pin, LOW);
  }

  //Button 3 is slightly different
  button3State = digitalRead(button3Pin); 
    if (button3State == LOW){             
    // The button 3 LED uses a "fading" effect like emergency lights
    for(int i = 0; i <= 255; i +=5){
    // This "for" loop is used to create a dimming effect      
      analogWrite(led3Pin, i);
      // "i" is the value written to the LED
      // The "for" loop incrementally increases "i" until in reaches 255          
      delay(30);                          
    }                                     
    for(int i = 0; i <= 255; i +=5){ // The "for" loop runs 3x 
      analogWrite(led3Pin, I);       
      delay(30);
    }
    for(int i = 0; i <= 255; i +=5){
      analogWrite(led3Pin, i);
      delay(30);
    // Adjust with any pattern or combination you like!
    }
   
  }

  else {
    digitalWrite(led3Pin, LOW);    
  }

}



Leave a Reply

Your email address will not be published. Required fields are marked *