Need help debouncing

Hello everybody,

Like the subject line says, can somebody point me in the right direction with debouncing the following code.

const int buttonPin = 2;
int pinArray[] = {3, 5, 6, 9, 10, 11, 12};
int count = 0;
int timer = 30;
int mode = 0;
int buttonState = 0;

void setup()
{
  for (count = 0; count < 8; count++) 
  {
    pinMode(pinArray[count], OUTPUT);
    pinMode(buttonPin, INPUT);
  }
}

void loop() 
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) 
  {
    if (mode != 3)
    {
      mode = mode+1;
    } 
    else 
    {
      mode = 0;
    }
  } 
  else {}
  if (mode == 0)
  {
    for (count = 0; count < 8; count++) 
    {
      digitalWrite(pinArray[count], HIGH);
      delay(timer);
      digitalWrite(pinArray[count + 1], HIGH);
      delay(timer);
      digitalWrite(pinArray[count], LOW);
      delay(timer*2);
    }
    for (count = 6; count >= 0; count--) 
    {
      digitalWrite(pinArray[count], HIGH);
      delay(timer);
      digitalWrite(pinArray[count - 1], HIGH);
      delay(timer);
      digitalWrite(pinArray[count], LOW);
      delay(timer*2);
    }

  }
  else if (mode == 1)
  {
    for (count = 0; count < 8; count++) 
    {
      digitalWrite(pinArray[count], HIGH);
      delay(timer);
    }
    for (count = 0; count < 8; count++) 
    {
      digitalWrite(pinArray[count], LOW);
      delay(timer);
    }
    for (count = 0; count < 8; count++) 
    {
      digitalWrite(pinArray[7 - count], HIGH);
      delay(timer);
    }
    for (count = 0; count < 8; count++) 
    {
      digitalWrite(pinArray[7 - count], LOW);
      delay(timer);
    }

  }
  else if (mode == 2)
  {
    for (count = 0; count < 6; count++) 
    {
      digitalWrite(pinArray[4-count], HIGH);
      digitalWrite(pinArray[5+count], HIGH);
      delay(timer);
      digitalWrite(pinArray[6+count], HIGH);
      digitalWrite(pinArray[3-count], HIGH);
      //delay(timer);
      digitalWrite(pinArray[4-count], LOW);
      digitalWrite(pinArray[5+count], LOW);
      delay(timer*2);
    }

  }
  else if (mode==3)
  {
    for (count = 0; count < 5; count++)
    {
      digitalWrite(pinArray[count], HIGH);
      digitalWrite(pinArray[6-count], HIGH); 
      delay(timer);
      digitalWrite(pinArray[count + 1], HIGH);
      digitalWrite(pinArray[6-count - 1], HIGH);
      //delay(timer);
      digitalWrite(pinArray[count], LOW);
      digitalWrite(pinArray[6-count], LOW);
      delay(timer*2);
    }
    for (count = 0; count < 4; count++) 
    {
      digitalWrite(pinArray[4-count], HIGH);
      digitalWrite(pinArray[5+count], HIGH);
      delay(timer);
      digitalWrite(pinArray[6+count], HIGH);
      digitalWrite(pinArray[3-count], HIGH);
      //delay(timer);
      digitalWrite(pinArray[4-count], LOW);
      digitalWrite(pinArray[5+count], LOW);
      delay(timer*2);
    }
  }
}

Thanks ahead of time

Bounce2 library

With all the delays you have in there, I'd have thought you have it covered.

Don't wast time writing debounce code until you know you have a debounce problem.

In its simplest form all you need is a period between successive reads of a button. In many cases the normal progress of the code provides that. There is a very simple debounce system in the code in several things at a time.

...R

Is the button wired from Vcc to the input pin with a pull down resistor to ground?

Hello groundfungus, The button is wired from the 5V pin with a 10K resistor to ground.

The more standard way of wiring switches is shown in the attached. This has the advantages of using the internal pull up resistors and hardware debounce. You need to change your code to reflect the LOW for a pushed button. I have found that the hardware debounce alone will work with probably 90% of switches, but software debounce will work with all switches given the right delay between readings.

Hello again everybody,

I tried this debounce code, but my problem is how to place in the loop

const int buttonPin = 2;
int pinArray[] = {3, 5, 6, 9, 10, 11, 12};
int count = 0;
int timer = 30;
int mode = 0;
int buttonState = 0;
boolean lastButton;
boolean currentButton = false;

void setup()
{
  for (count = 0; count < 8; count++) 
  {
    pinMode(pinArray[count], OUTPUT);
    pinMode(buttonPin, INPUT);
  }
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(buttonPin);
  }
  return current;  
}

void loop() 
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) 
  {
    if (mode != 3)
    {
      mode = mode+1;
    } 
    else 
    {
      mode = 0;
    }
  } 
  else {}
  if (mode == 0)
  {
    for (count = 0; count < 8; count++) 
    {
      digitalWrite(pinArray[count], HIGH);
      delay(timer);
      digitalWrite(pinArray[count + 1], HIGH);
      delay(timer);
      digitalWrite(pinArray[count], LOW);
      delay(timer*2);
    }
    for (count = 6; count >= 0; count--) 
    {
      digitalWrite(pinArray[count], HIGH);
      delay(timer);
      digitalWrite(pinArray[count - 1], HIGH);
      delay(timer);
      digitalWrite(pinArray[count], LOW);
      delay(timer*2);
    }

  }
  else if (mode == 1)
  {
    for (count = 0; count < 8; count++) 
    {
      digitalWrite(pinArray[count], HIGH);
      delay(timer);
    }
    for (count = 0; count < 8; count++) 
    {
      digitalWrite(pinArray[count], LOW);
      delay(timer);
    }
    for (count = 0; count < 8; count++) 
    {
      digitalWrite(pinArray[7 - count], HIGH);
      delay(timer);
    }
    for (count = 0; count < 8; count++) 
    {
      digitalWrite(pinArray[7 - count], LOW);
      delay(timer);
    }

  }
  else if (mode == 2)
  {
    for (count = 0; count < 6; count++) 
    {
      digitalWrite(pinArray[4-count], HIGH);
      digitalWrite(pinArray[5+count], HIGH);
      delay(timer);
      digitalWrite(pinArray[6+count], HIGH);
      digitalWrite(pinArray[3-count], HIGH);
      delay(timer);
      digitalWrite(pinArray[4-count], LOW);
      digitalWrite(pinArray[5+count], LOW);
      delay(timer*2);
    }

  }
  else if (mode==3)
  {
    for (count = 0; count < 5; count++)
    {
      digitalWrite(pinArray[count], HIGH);
      digitalWrite(pinArray[6-count], HIGH); 
      delay(timer);
      digitalWrite(pinArray[count + 1], HIGH);
      digitalWrite(pinArray[6-count - 1], HIGH);
      delay(timer);
      digitalWrite(pinArray[count], LOW);
      digitalWrite(pinArray[6-count], LOW);
      delay(timer*2);
    }
    for (count = 0; count < 4; count++) 
    {
      digitalWrite(pinArray[4-count], HIGH);
      digitalWrite(pinArray[5+count], HIGH);
      delay(timer);
      digitalWrite(pinArray[6+count], HIGH);
      digitalWrite(pinArray[3-count], HIGH);
      delay(timer);
      digitalWrite(pinArray[4-count], LOW);
      digitalWrite(pinArray[5+count], LOW);
      delay(timer*2);
    }
  }
}

Also, groundfungus, I tried your the wiring that you suggested (without the cap) and it didn’t work. It just cycled through all the button modes without even pushing the buttons

Did you change your code to look for LOW for a pushed (active) switch?

groundfungus,

first of all, thanks a bunch for your help. I did change to LOW and I changed the pinMode to INPUT_PULLUP. This worked a little better, but it is still not quite there. If I hold the button it almost works correctly. That probably sounds bad. I have tried the software debounce but I just don't know where in the loop to call the function. My programming skills are mediocre at best.

Ok, I have gotten a tad bit closer

const int buttonPin = 2;
int pinArray[] = {3, 5, 6, 9, 10, 11, 12};
int count = 0;
int timer = 30;
int mode = 0;
int buttonState = 0;
boolean lastButton;
boolean currentButton = false;

void setup()
{
  for (count = 0; count < 8; count++)
  {
    pinMode(pinArray[count], OUTPUT);
    pinMode(buttonPin, INPUT_PULLUP);
  }
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(buttonPin);
  }
  return current;
}

void loop()
{

  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW)
  {
    if (mode != 3)
    {
      mode = mode + 1;
    }
    else
    {
      mode = 0;
    }
  }
  else {}
  lastButton = currentButton;                   //This is where I call the debounce function
  currentButton = debounce(lastButton);
  if (lastButton == false && currentButton == true)
  {
    if (mode == 0)
    {
      for (count = 0; count < 8; count++)
      {
        digitalWrite(pinArray[count], HIGH);
        delay(timer);
        digitalWrite(pinArray[count + 1], HIGH);
        delay(timer);
        digitalWrite(pinArray[count], LOW);
        delay(timer * 2);
      }
      for (count = 6; count >= 0; count--)
      {
        digitalWrite(pinArray[count], HIGH);
        delay(timer);
        digitalWrite(pinArray[count - 1], HIGH);
        delay(timer);
        digitalWrite(pinArray[count], LOW);
        delay(timer * 2);
      }

    }
    else if (mode == 1)
    {
      for (count = 0; count < 8; count++)
      {
        digitalWrite(pinArray[count], HIGH);
        delay(timer);
      }
      for (count = 0; count < 8; count++)
      {
        digitalWrite(pinArray[count], LOW);
        delay(timer);
      }
      for (count = 0; count < 8; count++)
      {
        digitalWrite(pinArray[7 - count], HIGH);
        delay(timer);
      }
      for (count = 0; count < 8; count++)
      {
        digitalWrite(pinArray[7 - count], LOW);
        delay(timer);
      }

    }
    else if (mode == 2)
    {
      for (count = 0; count < 6; count++)
      {
        digitalWrite(pinArray[4 - count], HIGH);
        digitalWrite(pinArray[5 + count], HIGH);
        delay(timer);
        digitalWrite(pinArray[6 + count], HIGH);
        digitalWrite(pinArray[3 - count], HIGH);
        delay(timer);
        digitalWrite(pinArray[4 - count], LOW);
        digitalWrite(pinArray[5 + count], LOW);
        delay(timer * 2);
      }

    }
    else if (mode == 3)
    {
      for (count = 0; count < 5; count++)
      {
        digitalWrite(pinArray[count], HIGH);
        digitalWrite(pinArray[6 - count], HIGH);
        delay(timer);
        digitalWrite(pinArray[count + 1], HIGH);
        digitalWrite(pinArray[6 - count - 1], HIGH);
        delay(timer);
        digitalWrite(pinArray[count], LOW);
        digitalWrite(pinArray[6 - count], LOW);
        delay(timer * 2);
      }
      for (count = 0; count < 4; count++)
      {
        digitalWrite(pinArray[4 - count], HIGH);
        digitalWrite(pinArray[5 + count], HIGH);
        delay(timer);
        digitalWrite(pinArray[6 + count], HIGH);
        digitalWrite(pinArray[3 - count], HIGH);
        delay(timer);
        digitalWrite(pinArray[4 - count], LOW);
        digitalWrite(pinArray[5 + count], LOW);
        delay(timer * 2);
      }
    }
  }
}

now each mode runs one time and then when I press the button it switches modes, but with each switch it runs only once.

OK now its time for an admission. I have never used software debounce. Every switch that I have used has worked perfectly with just the cap across it and a 10k or so pullup (or the replacement did). I'll probably catch hell for saying that, but i have been doing so for decades.

groundfungus: OK now its time for an admission. I have never used software debounce.

I won't claim NEVER to have used it - but almost never. And when I didn't use it I did not have capacitors or any other hardware debounce either.

A much over rated problem - usually due to poor software design.

...R

fsalas10: Ok, I have gotten a tad bit closer

Can you explain what your code is supposed to do? What is supposed to happen when the button is pressed?

Am I correct to think there is just one button?

...R

Yes, just one button. I want it to switch between modes with each button press. There are four modes, and each is a different pattern that the leds will light up in.

Something like this perhaps

void loop() {
  readButton();
  // other stuff
}

void readButton() {
   if (millis() - prevBtnMillis >= 100) {
      curBtn = digitalRead(btnPin);
      if (prevBtn == HIGH && curBtn == LOW) { // assumes active LOW
          mode ++;
          if (mode > maxMode) {
             mode = 0;
          }
       prevBtn = curBtn;
       prevBtnMillis = millis();
     }
  }
}

...R