Arduino multiple button debounce

preview_player
Показать описание
Detecting button presses on multiple buttons. Debounce and long/short press detection

Here is the code for the button detection

The variables at the top set the pins for the buttons, number of buttons and the arrays for the required info for each pin, which is then set in buttonSetup() in the Setup() function

Calling buttonHandler[buttonNumber] each time in loop will return an int for the current button state
0=not pressed or returns 0 after long press if button still held down
1=short press
2=long press
Рекомендации по теме
Комментарии
Автор

This is the code I've used. I did try to upload it to my website but it's not working so I'll paste it here for now. I'll get a link in the information when it's working

//Number of LEDs
const int numberLeds=3;
//Pins for each LED
int led[numberLeds] = {6, 7, 8};
//State of each LED
int ledState[numberLeds];



//Number of buttons
const int numberButtons=2;

//Set the pins used for each button
int button[numberButtons] = {2, 3};

//Start state of the LEDs off (HIGH=off when INPUT_PULLUP used)
//No need for pullup/down resistors
int

//Bools used for when buttons currently pressed (used for long press detection)
bool buttonActive[numberButtons];

//Debounce timer for each button
unsigned long


//Set the debounce delay
int debounceDelay=25;
//Set the time required for a long press
int longPressDelay=500;


void setup() {
  // put your setup code here, to run once:
  buttonSetup();
  ledSetup();

}

void loop() {
  // put your main code here, to run repeatedly: 
  
  //Loop through each button and get the state
  //0=not pressed
  //1=short press
  //2=long press
  //You could also loop through, set veriables for each button
  //then act on each buttons state seperately
  for (int x=0; x<numberButtons; x++)
  {
    int state=buttonHandler(x);

    if ( state== 1)
    {
      ledState[x]=!ledState[x];
      digitalWrite(led[x], ledState[x]);
    }

    if (state == 2)
    {
      ledState[2]=!ledState[2];
      digitalWrite(led[2], ledState[2]);
    }
  }
}

int buttonHandler(int number)
{
  //Handle presses for each button
  int reading = digitalRead(button[number]);

  //Check if button state has changed since last check
  if (reading != lastButtonState[number])
  {
    if (reading==HIGH && !buttonActive[number])
    {
      lastButtonState[number] = reading;
      
      //Return 0 (not pressed)
      return 0;
    }
    
    //if reading is high (open)
    if (reading==HIGH && buttonActive[number])
    {
      if (millis() - lastDebounceTime[number] > debounceDelay)
      {
        lastButtonState[number] = reading;

        buttonActive[number]=true;

        //Return 1 (short press)
        return 1;
      }

      lastButtonState[number] = reading;

      buttonActive[number]=false;
      
      //Return 0 (not pressed)
      return 0;
    }
    
    //if reading is low (closed)
    else if (reading==LOW)
    {
      if (!buttonActive[number])
      {
        //Start debounce timer
       
        
        lastButtonState[number] = reading;

        buttonActive[number]=true;

        //Return 0 (not pressed)
        return 0;
      }
      //Return 0 (not pressed)
      return 0;
    }
  }

  //Check if reading still high (open)
  if (reading==HIGH)
  {
    lastButtonState[number] = reading;
    buttonActive[number]=false;

    //Return 0 (not pressed)
    return 0;
  }

  
  if (reading==LOW)
  {
    //Check if button pressed for long enough to register as long press
    if (millis() - lastDebounceTime[number] > longPressDelay && buttonActive[number])
    {
      lastButtonState[number] = reading;

      buttonActive[number]=false;

      //Return 2 (long press)
      return 2;
    }
    else
      //Return 0 (not pressed)
      return 0;
  }
}
void ledSetup()
{
  for (int x=0; x<numberLeds; x++)
  {
    pinMode(led[x], OUTPUT);
    ledState[x]=LOW;
  }
}

void buttonSetup()
{
  for (int x=0; x<numberButtons; x++)
  {
    lastButtonState[x]=HIGH;
    buttonActive[x]=false;
    lastDebounceTime[x]=0;
   // buttonState[x]=HIGH;
    pinMode(button[x], INPUT_PULLUP);
    
  }
}

acggthdfh
Автор

I've been trolling for something like this for weeks. its surprisingly hard to do.

I've tried... Debounce library, bounce1 and Bounce2... which worked eventually. but none of those offered a long press option! (or maybe i missed it)

either way. thanks for the code !!!! its awesome

RebeLyfe
Автор

Turn on LED with button and keep it on using 2 button and 2 led??

limwuising
Автор

Please share the code for this project

guitarans
welcome to shbcf.ru