2

I want to use 4 Push Buttons and 4 leds in my project. What do I need to change in my sketch for multiple input push buttons and multiple outputs of leds? So my sketch is only for one push button and one led. So tell me how I can solve this problem.

Here is the code:

const int buttonPin =2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;   
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}

void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState) {
  buttonState = reading;

  // only toggle the LED if the new button state is HIGH
  if (buttonState == HIGH) {
    ledState = !ledState;
  }
  }
  }

  // set the LED:
 digitalWrite(ledPin, ledState);
 lastButtonState = reading;
 }
1
  • 1
    In many systems, you can simply ignore all inputs for a debounce delay after detecting any one. Others have more complicated requirements, for (rare) example filtering input noise before accepting an input, or allowing a different button to be pressed simultaneously or without delay. In that case you can use unique code for each, but could also use an array. Oct 7, 2016 at 17:51

3 Answers 3

1

Well you declared only one pushbutton and one led. Now do like push1, push2, push3, push4 and led1, led2, led3, led4, and so on. You can also do that with your buttonStates1... Do not forget to declare them as INPUTs or OUTPUTs. Copy paste will help you with this task.

1
  • This does not address the primary question of debouncing. Oct 7, 2016 at 17:49
1

What do I need to change in my sketch for multiple input push buttons and multiple outputs of leds?

easy:

1) study your code to understand how it debounces one input;

2) isolate those parameters / variables used to debounce that specific input;

3) group those parameters / variables into a struct and pass a pointer to that struct to your code;

4) rewrite your code so that it takes the pointer to that struct and debounces accordingly;

5) use that code on as many inputs as you wish.

that's the general approach you take to coding any task.

hope it helps.

1

I hope this will work for you.

const int buttonPin1 = 2; // Button 1
const int buttonPin2 = 3; // Button 2
const int buttonPin3 = 4; // Button 3
const int buttonPin4 = 5; // Button 4

const int ledPin1 = 6; // LED 1
const int ledPin2 = 7; // LED 2
const int ledPin3 = 8; // LED 3
const int ledPin4 = 9; // LED 4

int ledState1 = HIGH;
int ledState2 = HIGH;
int ledState3 = HIGH;
int ledState4 = HIGH;

int buttonState1, buttonState2, buttonState3, buttonState4;

int lastButtonState1 = LOW;
int lastButtonState2 = LOW;
int lastButtonState3 = LOW;
int lastButtonState4 = LOW;

long lastDebounceTime1 = 0;
long lastDebounceTime2 = 0;
long lastDebounceTime3 = 0;
long lastDebounceTime4 = 0;

long debounceDelay1 = 50;
long debounceDelay2 = 50;
long debounceDelay3 = 50;
long debounceDelay4 = 50;

void setup()
{
  pinMode(buttonPin1, INPUT);
  pinMode(ledPin1, OUTPUT);
  digitalWrite(ledPin1, ledState1);

  pinMode(buttonPin2, INPUT);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin2, ledState2);

  pinMode(buttonPin3, INPUT);
  pinMode(ledPin3, OUTPUT);
  digitalWrite(ledPin3, ledState3);

  pinMode(buttonPin4, INPUT);
  pinMode(ledPin4, OUTPUT);
  digitalWrite(ledPin4, ledState4);
}

void loop()
{
  led1Fun();
  led2Fun();
  led3Fun();
  led4Fun();
}

void led1Fun()
{
  int reading1 = digitalRead(buttonPin1);

  if(reading1 != lastButtonState1)
  {
    lastDebounceTime1 = millis();
  }

  if((millis() - lastDebounceTime1) > debounceDelay1)
  {
    if(reading1 != buttonState1)
    {
      buttonState1 = reading1;

      if(buttonState1 == HIGH)
      {
        ledState1 = !ledState1;
      }
    }
  }

  digitalWrite(ledPin1, ledState1);
  lastButtonState1 = reading1;
}

void led2Fun()
{
  int reading2 = digitalRead(buttonPin2);

  if(reading2 != lastButtonState2)
  {
    lastDebounceTime2 = millis();
  }

  if((millis() - lastDebounceTime2) > debounceDelay2)
  {
    if(reading2 != buttonState2)
    {
      buttonState2 = reading2;

      if(buttonState2 == HIGH)
      {
        ledState2 = !ledState2;
      }
    }
  }

  digitalWrite(ledPin2, ledState2);
  lastButtonState2 = reading2;
}

void led3Fun()
{
  int reading3 = digitalRead(buttonPin3);

  if(reading3 != lastButtonState3)
  {
    lastDebounceTime3 = millis();
  }

  if((millis() - lastDebounceTime3) > debounceDelay3)
  {
    if(reading3 != buttonState3)
    {
      buttonState3 = reading3;

      if(buttonState3 == HIGH)
      {
        ledState3 = !ledState3;
      }
    }
  }

  digitalWrite(ledPin3, ledState3);
  lastButtonState3 = reading3;
}

void led4Fun()
{
  int reading4 = digitalRead(buttonPin4);

  if(reading4 != lastButtonState4)
  {
    lastDebounceTime4 = millis();
  }

  if((millis() - lastDebounceTime4) > debounceDelay4)
  {
    if(reading4 != buttonState4)
    {
      buttonState4 = reading4;

      if(buttonState4 == HIGH)
      {
        ledState4 = !ledState4;
      }
    }
  }

  digitalWrite(ledPin4, ledState4);
  lastButtonState4 = reading4;
}
1
  • That code would greatly benefit from the use of arrays.
    – Delta_G
    Jul 6, 2017 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.