Skip to main content
Question Protected by VE7JRO
More relevant title, "debounce" tag
Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

Help Control multiple LEDs with sketch pleasemultiple buttons

Format code
Source Link
per1234
  • 4.3k
  • 2
  • 24
  • 44
}

if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
{digitalWrite(ledPin2, LOW);       // turn ledPin2 on
 ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
 
   }
if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
  { digitalWrite(ledPin2, LOW);       // turn ledPin2 on
    ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
  }
// set pins
const int buttonPin1 = 2;                 // Pin1's pushbutton
const int buttonPin2 = 4;                 // Pin2's pushbutton
const int buttonPin3 = 6;                 // Pin3's pushbutton
const int buttonPin4 = 8;                 // Pin4's pushbutton
const int ledPin1 = 3;                    // Pin1's LED
const int ledPin2 = 5;                    // Pin2's LED
const int ledPin3 = 7;                    // Pin3's LED
const int ledPin4 = 9;                    // Pin4's LED
 
// set variables
int ledPin1_status = 0;
int ledPin2_status = 0;
int ledPin3_status = 0;
int ledPin4_status = 0;
 
void setup() {  //initialize pins
 pinMode(buttonPin1, INPUT); // set PINn as INPUT
 pinMode(buttonPin2, INPUT);
 pinMode(buttonPin3, INPUT);
 pinMode(buttonPin4, INPUT);
 pinMode(ledPin1, OUTPUT);   // set LEDn as OUTPUT
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 pinMode(ledPin4, OUTPUT);
}

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
 
void loop() {
 if (digitalRead(buttonPin1) == HIGH) { // check if buttonPin1 was pressed (be sure to use a resistor?)
   if (ledPin1_status == 0) {           // check if ledPin1 was not already pressed
     digitalWrite(ledPin1, HIGH);       // turn ledPin1 on
     ledPin1_status = 1;                // 1 = on, set ledPin1_status to 1
     
   }
   else {
     digitalWrite(ledPin1, LOW);        // turn ledPin1 off
     ledPin1_status = 0;                // 0 = off, set Pin1_status to 0
     digitalWrite(ledPin2, LOW);        // turn ledPin2 off
     ledPin2_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin3, LOW);        // turn ledPin3 off
     ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin4, LOW);        // turn ledPin4 off
     ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
   }
 }

 if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
   {digitalWrite(ledPin2, HIGH);       // turn ledPin2 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
   
 }

if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    {digitalWrite(ledPin2, LOW);       // turn ledPin2 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
   
 }
 
 if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin3 was pressed (be sure to use a resistor?)
   {digitalWrite(ledPin3, HIGH);       // turn ledPin3 on
     ledPin3_status = 1;                // 1 = on, set ledPin3_status to 1
     
   }
   
 }

if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    {digitalWrite(ledPin3, LOW);       // turn ledPin3 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
  
 }

if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
   {digitalWrite(ledPin4, HIGH);       // turn ledPin4 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
 }

if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    {digitalWrite(ledPin4, LOW);       // turn ledPin4 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
 
 
   
 }
// set pins
const int buttonPin1 = 2;                 // Pin1's pushbutton
const int buttonPin2 = 4;                 // Pin2's pushbutton
const int buttonPin3 = 6;                 // Pin3's pushbutton
const int buttonPin4 = 8;                 // Pin4's pushbutton
const int ledPin1 = 3;                    // Pin1's LED
const int ledPin2 = 5;                    // Pin2's LED
const int ledPin3 = 7;                    // Pin3's LED
const int ledPin4 = 9;                    // Pin4's LED

// set variables
int ledPin1_status = 0;
int ledPin2_status = 0;
int ledPin3_status = 0;
int ledPin4_status = 0;

void setup() {  //initialize pins
  pinMode(buttonPin1, INPUT); // set PINn as INPUT
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(ledPin1, OUTPUT);   // set LEDn as OUTPUT
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
}

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void loop() {
  if (digitalRead(buttonPin1) == HIGH) { // check if buttonPin1 was pressed (be sure to use a resistor?)
    if (ledPin1_status == 0) {           // check if ledPin1 was not already pressed
      digitalWrite(ledPin1, HIGH);       // turn ledPin1 on
      ledPin1_status = 1;                // 1 = on, set ledPin1_status to 1
    }
    else {
      digitalWrite(ledPin1, LOW);        // turn ledPin1 off
      ledPin1_status = 0;                // 0 = off, set Pin1_status to 0
      digitalWrite(ledPin2, LOW);        // turn ledPin2 off
      ledPin2_status = 0;                // 0 = off, set Pin2_status to 0
      digitalWrite(ledPin3, LOW);        // turn ledPin3 off
      ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
      digitalWrite(ledPin4, LOW);        // turn ledPin4 off
      ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
    }
  }

  if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin2, HIGH);       // turn ledPin2 on
      ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
    }
  }

  if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin2, LOW);       // turn ledPin2 on
      ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
    }
  }

  if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin3 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin3, HIGH);       // turn ledPin3 on
      ledPin3_status = 1;                // 1 = on, set ledPin3_status to 1
    }
  }

  if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin3, LOW);       // turn ledPin3 on
      ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
    }
  }

  if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin4, HIGH);       // turn ledPin4 on
      ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
    }
  }

  if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin4, LOW);       // turn ledPin4 on
      ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
    }
  }
}

if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
{digitalWrite(ledPin2, LOW);       // turn ledPin2 on
 ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
 
   }
// set pins
const int buttonPin1 = 2;                 // Pin1's pushbutton
const int buttonPin2 = 4;                 // Pin2's pushbutton
const int buttonPin3 = 6;                 // Pin3's pushbutton
const int buttonPin4 = 8;                 // Pin4's pushbutton
const int ledPin1 = 3;                    // Pin1's LED
const int ledPin2 = 5;                    // Pin2's LED
const int ledPin3 = 7;                    // Pin3's LED
const int ledPin4 = 9;                    // Pin4's LED
 
// set variables
int ledPin1_status = 0;
int ledPin2_status = 0;
int ledPin3_status = 0;
int ledPin4_status = 0;
 
void setup() {  //initialize pins
 pinMode(buttonPin1, INPUT); // set PINn as INPUT
 pinMode(buttonPin2, INPUT);
 pinMode(buttonPin3, INPUT);
 pinMode(buttonPin4, INPUT);
 pinMode(ledPin1, OUTPUT);   // set LEDn as OUTPUT
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 pinMode(ledPin4, OUTPUT);
}

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
 
void loop() {
 if (digitalRead(buttonPin1) == HIGH) { // check if buttonPin1 was pressed (be sure to use a resistor?)
   if (ledPin1_status == 0) {           // check if ledPin1 was not already pressed
     digitalWrite(ledPin1, HIGH);       // turn ledPin1 on
     ledPin1_status = 1;                // 1 = on, set ledPin1_status to 1
     
   }
   else {
     digitalWrite(ledPin1, LOW);        // turn ledPin1 off
     ledPin1_status = 0;                // 0 = off, set Pin1_status to 0
     digitalWrite(ledPin2, LOW);        // turn ledPin2 off
     ledPin2_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin3, LOW);        // turn ledPin3 off
     ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin4, LOW);        // turn ledPin4 off
     ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
   }
 }

 if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
   {digitalWrite(ledPin2, HIGH);       // turn ledPin2 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
   
 }

if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    {digitalWrite(ledPin2, LOW);       // turn ledPin2 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
   
 }
 
 if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin3 was pressed (be sure to use a resistor?)
   {digitalWrite(ledPin3, HIGH);       // turn ledPin3 on
     ledPin3_status = 1;                // 1 = on, set ledPin3_status to 1
     
   }
   
 }

if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    {digitalWrite(ledPin3, LOW);       // turn ledPin3 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
  
 }

if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
   {digitalWrite(ledPin4, HIGH);       // turn ledPin4 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
 }

if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    {digitalWrite(ledPin4, LOW);       // turn ledPin4 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
 
 
   
 }
if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
  { digitalWrite(ledPin2, LOW);       // turn ledPin2 on
    ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
  }
// set pins
const int buttonPin1 = 2;                 // Pin1's pushbutton
const int buttonPin2 = 4;                 // Pin2's pushbutton
const int buttonPin3 = 6;                 // Pin3's pushbutton
const int buttonPin4 = 8;                 // Pin4's pushbutton
const int ledPin1 = 3;                    // Pin1's LED
const int ledPin2 = 5;                    // Pin2's LED
const int ledPin3 = 7;                    // Pin3's LED
const int ledPin4 = 9;                    // Pin4's LED

// set variables
int ledPin1_status = 0;
int ledPin2_status = 0;
int ledPin3_status = 0;
int ledPin4_status = 0;

void setup() {  //initialize pins
  pinMode(buttonPin1, INPUT); // set PINn as INPUT
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(ledPin1, OUTPUT);   // set LEDn as OUTPUT
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
}

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void loop() {
  if (digitalRead(buttonPin1) == HIGH) { // check if buttonPin1 was pressed (be sure to use a resistor?)
    if (ledPin1_status == 0) {           // check if ledPin1 was not already pressed
      digitalWrite(ledPin1, HIGH);       // turn ledPin1 on
      ledPin1_status = 1;                // 1 = on, set ledPin1_status to 1
    }
    else {
      digitalWrite(ledPin1, LOW);        // turn ledPin1 off
      ledPin1_status = 0;                // 0 = off, set Pin1_status to 0
      digitalWrite(ledPin2, LOW);        // turn ledPin2 off
      ledPin2_status = 0;                // 0 = off, set Pin2_status to 0
      digitalWrite(ledPin3, LOW);        // turn ledPin3 off
      ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
      digitalWrite(ledPin4, LOW);        // turn ledPin4 off
      ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
    }
  }

  if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin2, HIGH);       // turn ledPin2 on
      ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
    }
  }

  if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin2, LOW);       // turn ledPin2 on
      ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
    }
  }

  if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin3 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin3, HIGH);       // turn ledPin3 on
      ledPin3_status = 1;                // 1 = on, set ledPin3_status to 1
    }
  }

  if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin3, LOW);       // turn ledPin3 on
      ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
    }
  }

  if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin4, HIGH);       // turn ledPin4 on
      ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
    }
  }

  if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    { digitalWrite(ledPin4, LOW);       // turn ledPin4 on
      ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
    }
  }
Source Link

Help with sketch please

Problem 1: LED1 needs to be on only when button1 is held down. Right now it takes several presses to turn on and off and stays on. Problem 1: LEDs 2,3,4 do not turn off with buttons 2,3,4. They are all turned off by button1, which is what I want, but I also need them to turn of with their respective button presses.

I need buttons 2,3,4 to light up LEDs 2,3,4 and stay lit until I press the respective buttons again OR button1 which turns off all lights, and the LED for button1 must only stay on when button1 is held down.

I am half way there with just a few minor problems. The first is LED1 stays on rather than when I hold the button1 down only. And the other problem is, it takes many presses to turn the LEDs on or off. I fixed this on 2,3,4 by removing the following code for each pin:

}

if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
{digitalWrite(ledPin2, LOW);       // turn ledPin2 on
 ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
 
   }

After I remove that code the LEDs light straight away and stay on until I press button 1 which is what I want. But I would also like LEDs 2,3,4 to turn off with their respective button press. With that code removed LEDs 2,3,4 no longer work independently of button1.

Full Code:

// set pins
const int buttonPin1 = 2;                 // Pin1's pushbutton
const int buttonPin2 = 4;                 // Pin2's pushbutton
const int buttonPin3 = 6;                 // Pin3's pushbutton
const int buttonPin4 = 8;                 // Pin4's pushbutton
const int ledPin1 = 3;                    // Pin1's LED
const int ledPin2 = 5;                    // Pin2's LED
const int ledPin3 = 7;                    // Pin3's LED
const int ledPin4 = 9;                    // Pin4's LED
 
// set variables
int ledPin1_status = 0;
int ledPin2_status = 0;
int ledPin3_status = 0;
int ledPin4_status = 0;
 
void setup() {  //initialize pins
 pinMode(buttonPin1, INPUT); // set PINn as INPUT
 pinMode(buttonPin2, INPUT);
 pinMode(buttonPin3, INPUT);
 pinMode(buttonPin4, INPUT);
 pinMode(ledPin1, OUTPUT);   // set LEDn as OUTPUT
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 pinMode(ledPin4, OUTPUT);
}

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
 
void loop() {
 if (digitalRead(buttonPin1) == HIGH) { // check if buttonPin1 was pressed (be sure to use a resistor?)
   if (ledPin1_status == 0) {           // check if ledPin1 was not already pressed
     digitalWrite(ledPin1, HIGH);       // turn ledPin1 on
     ledPin1_status = 1;                // 1 = on, set ledPin1_status to 1
     
   }
   else {
     digitalWrite(ledPin1, LOW);        // turn ledPin1 off
     ledPin1_status = 0;                // 0 = off, set Pin1_status to 0
     digitalWrite(ledPin2, LOW);        // turn ledPin2 off
     ledPin2_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin3, LOW);        // turn ledPin3 off
     ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin4, LOW);        // turn ledPin4 off
     ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
   }
 }

 if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
   {digitalWrite(ledPin2, HIGH);       // turn ledPin2 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
   
 }

if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    {digitalWrite(ledPin2, LOW);       // turn ledPin2 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
   
 }
 
 if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin3 was pressed (be sure to use a resistor?)
   {digitalWrite(ledPin3, HIGH);       // turn ledPin3 on
     ledPin3_status = 1;                // 1 = on, set ledPin3_status to 1
     
   }
   
 }

if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    {digitalWrite(ledPin3, LOW);       // turn ledPin3 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
  
 }

if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
   {digitalWrite(ledPin4, HIGH);       // turn ledPin4 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
 }

if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
    {digitalWrite(ledPin4, LOW);       // turn ledPin4 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     
   }
 
 
   
 }