Help with button counter and debounce

Hey there, Im trying to programm a button counter. One button counts up the other one down. But my current problem is that, even if i press as short as possible, the button counts up till around 50 same for down. I've found out that there is a debounce method to solve this but i cant quite get it working with a counter and im also net very experienced. Could somone help me out?

This is my code without bouncer right now:


int x = 0;
int xalt;
int button1 = 2;
int button2 = 3;
int state1;
int state2;

void setup() {

  pinMode(button1, INPUT);
  pinMode(button2, INPUT);

void loop() {
  state1 = digitalRead(button1);
  state2 = digitalRead(button2);

  if (xalt != x)
    xalt = x;
  if (state1 == HIGH) {
  if (state2 == HIGH) {



Try this, you will need to modify it to do exactly what you want but it should be enough to get you started.

/* Simple button debounce for 4 buttons. Increments a count and sends the updated count to the serial monitor once per button press */
/* Tested on an Uno */
/* Connect simple push to make buttons between 0V and pin 2, 0V and pin 3, 0V and pin 4 and between 0V and pin 5 */

#define noOfButtons 4     //Exactly what it says; must be the same as the number of elements in buttonPins
#define bounceDelay 20    //Minimum delay before regarding a button as being pressed and debounced
#define minButtonPress 3  //Number of times the button has to be detected as pressed before the press is considered to be valid

const int buttonPins[] = {2, 3, 4, 5};      // Input pins to use, connect buttons between these pins and 0V
uint32_t previousMillis[noOfButtons];       // Timers to time out bounce duration for each button
uint8_t pressCount[noOfButtons];            // Counts the number of times the button is detected as pressed, when this count reaches minButtonPress button is regared as debounced 
uint8_t testCount[noOfButtons];             //Test count, incremented once per button press

void setup() {
  uint8_t i;
  uint32_t baudrate = 115200;
  Serial.print("Serial port connected: ");
  for (i = 0; i < noOfButtons; ++i) {
    pinMode(buttonPins[i], INPUT_PULLUP);
    Serial.print("Testcount ");
    Serial.print(" = ");

void loop() {
  delay(10);     //Your other code goes here instead of this delay. DO NOT leave this delay here, it's ONLY for demonstration.

void debounce() {
  uint8_t i;
  uint32_t currentMillis = millis();
  for (i = 0; i < noOfButtons; ++i) {
    if (digitalRead(buttonPins[i])) {             //Input is high, button not pressed or in the middle of bouncing and happens to be high
        previousMillis[i] = currentMillis;        //Set previousMillis to millis to reset timeout
        pressCount[i] = 0;                        //Set the number of times the button has been detected as pressed to 0
      } else {
      if (currentMillis - previousMillis[i] > bounceDelay) {
        previousMillis[i] = currentMillis;        //Set previousMillis to millis to reset timeout
        if (pressCount[i] == minButtonPress) {
          doStuff(i);                             //Button has been debounced. Call function to do whatever you want done.

// Function to do whatever you want done once the button has been debounced.
// In this example it increments a counter and send the count to the serial monitor.
// Put your own functions here to do whatever you like.
void doStuff(uint8_t buttonNumber) {
  Serial.print("Button ");
  Serial.print(" testcount = ");
  Serial.println (testCount[buttonNumber]);

Answer is in the state change detect tutorial.

Thanks, that actually help me solve my problem :slight_smile: