Pushbutton Debouncing Filter Help

I'm new to Arduino, and I'm trying to implement a debouncing code function that will be used in a pushbutton circuit I have that is using a 7 segment display to count every pushbutton press from 0 to 9.

The problem I'm having is when I run the code, my display is stuck on 0, and won't recognize any presses. Any help would be appreciated!!

I'm using the Uno R3 Arduino

const int a=2;
const int b=3;
const int c=4;
const int d=5;
const int e=6;
const int f=7;
const int g=8;
const int buttonPin = 9; 
int validButtonPress = 0;
int count = 0;
int buttonPress = 0;
int buttonState = 0;  
int lastButtonState = 0;
long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() 
{
  //Designating input/output pins
pinMode(buttonPin, INPUT); 
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
}
void loop()
{
  if (readButton(buttonPin) == 1)
      
  buttonState = digitalRead(buttonPin);
  //Checking for a button press
  if  (validButtonPress == 1)
  {
   if (buttonState != lastButtonState)
     //counting function
   {
    if (buttonState == 1)
    {
     count++;
      
    }

    else 
    {
    }
    }
  }
      
  
lastButtonState = buttonState;

  //Switch case for incrementing LED counter
switch(count)
  {
    case 0:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, HIGH);
    break;

        case 1:
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
    break;

        case 2:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, HIGH);
    digitalWrite(g, LOW);
    break;
        case 3:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, LOW);
    break;
  
        case 4:
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    break;

        case 5:
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, HIGH);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    break;
  
        case 6:
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    break;

       case 7:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
    break;

        case 8:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    break;
  
        case 9:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, HIGH);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    break;
  
 default: count=0; break; 
  } 

}
//Attempted debouncing code
int readButton(int buttonPin)
{
  int validButtonPress = 0;
  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState)
    lastDebounceTime = millis();
  if ((millis() - lastDebounceTime) > debounceDelay)
    if (reading != buttonState)
  {
    buttonState = reading;
    if (buttonState == HIGH)
      validButtonPress = 1;
  }
  lastButtonState = reading;
  return validButtonPress;
}

Here's one way to handle debouncing. After a button is pressed, you set up a counter which is decremented each time you successfully detect that the button is released. If you detect a press, you start over counting.

/*
This is Arduino polling code for pin D4 as button A, low when pressed.
It reacts immediately to a button press, but then requires the button
to be released continuously for 100mS before another press is recognized.
*/


//in setup:

int newCount = 100;
int button_A_count = 0;
unsigned long newMilli_A;
unsigned long oldMilli_A;
int buttonApin = 4;


// in loop:

if (button_A_count) {
  newMilli_A = millis();
  if (oldMilli_A_ != newMilli_A) {
    oldMilli_A = newMilli_A;
    button_A_count--;
    if (digitalRead(buttonApin) == LOW) button_A_count = newCount;
  }
}

else if (digitalRead(buttonApin) == LOW) {
  button_A_count = newCount;
  oldMilli_A = millis();
  .
  .
  .
  whatever you want to do in response to a button_A press
  .
  .
  .
}

// same setup and loop code for button B, button C, etc.

for such a simple case, why not just add a 10 ms delay?

Have a read of https://forum.arduino.cc/index.php?topic=719995.0

You can use my DebouncedSwitch library. See the tutorial for examples and the code.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.