Can't figure how to debounce this

Hi, I read the debounce example in the documentation, it's pretty simple, but that's just a single button connected directly to the board, what I have is multiple switches with a 74HC165 ic, so I have to debounce all them reading always the same pin.
An example would really help me.

Thanks in advance.

http://forum.arduino.cc/index.php/topic,125754.0.html

The state of the switches is sampled with the latch pin, yes?
So you can debounce by taking several readings and when they stop changing for some amount of time call them done. Or, note the time when you first see a change, and then 50mS later take another and if they match call that good, if not wait another 25 or 2 or 3 more until they settle.

Thank you both.

This is my try

const int pLatch = 7;  
const int pClock = 6;  
const int pData  = 5;  

static const int num_buttons = 8;

int last_debounce_time[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int debounce_delay = 500;
int buttons_state[8];
int buttons_last_state[8] =  { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW };


void setup()
{
  Serial.begin(115200);
  pinMode (pLatch, OUTPUT);
  pinMode (pClock, OUTPUT);
  pinMode (pData, INPUT); 

  digitalWrite(pClock, LOW);  
  digitalWrite(pLatch, HIGH); 
}

void loop()
{
  digitalWrite(pLatch, LOW);  
  delayMicroseconds(5);
  digitalWrite(pLatch, HIGH);  
  
  for(int i = 0;i<num_buttons;i++)
  {
    int reading = digitalRead(pData); 
      
    // If the switch changed, due to noise or pressing:    
    if(reading != buttons_last_state[i])
    {
      last_debounce_time[i] = millis();
    }
      
    if ((millis() - last_debounce_time[i]) > debounce_delay) 
    {
      // 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 != buttons_state[i])
      {
        buttons_state[i] = reading;     
      }      
    }       
   
    buttons_last_state[i] = reading;
   
    digitalWrite(pClock, HIGH);  
    delayMicroseconds(5);
    digitalWrite(pClock, LOW);    

  }

  for(int i = 0;i<num_buttons;i++)
  {
    Serial.print(buttons_state[i]);
  }
  Serial.println("");
  delay(1);
}

It doesn't work...
I print the byte after each cycle, so at the start I get 00000000, so far so good, then I turn on the 2 buttons I have attached to the A and B pins and I get 00000011...still ok, but then I turn off both buttons and nothing changes (still getting 00000011), and after a few seconds I start getting noise in those two bits.

I can't figure what's wrong.

Sounds like you have a floating input how are the buttons wired?
Have you got pull down resistors fitted? If not you need them.
An input not connected to anything will not be a solid zero.

Ty, This is how I wired it.
Hope you can understand :blush:

int last_debounce_time[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

int? Time is ALWAYS stored in an unsigned long.

int buttons_state[8];
int buttons_last_state[8] =  { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW };

int? To hold HIGH or LOW?

You really need to spend some time learning what appropriate types to use.

  for(int i = 0;i<num_buttons;i++)
  {
    Serial.print(buttons_state[i]);
  }
  Serial.println("");
  delay(1);

By the time this gets done, your switches will be done bouncing. Why you think you need more code to deal with bouncing is a mystery that we (i.e. you) need to solve first.

This is how I wired it.

This is wrong.

VCC on the chip should be wired directly to 5V not through a 10K resistor.

The inputs on the buttons A to H should all have a pull up resistor on them, that is from the input pin to +5V, make them about 4K7 although this is not critical and can be anywhere between 1K and 10K

You need pullup resistors on the shift register input pins, 10K to +5V.

PaulS:
int? To hold HIGH or LOW?

That's how tutorial do it
http://arduino.cc/en/Tutorial/Debounce

Grumpy_Mike:
The inputs on the buttons A to H should all have a pull up resistor on them, that is from the input pin to +5V, make them about 4K7 although this is not critical and can be anywhere between 1K and 10K

Ok will try it that way then
BTW, what does the "7" mean in "4K7"?

CrossRoads:
10K to +5V.

Grumpy_Mike:
VCC on the chip should be wired directly to 5V not through a 10K resistor.

So, it's direct to 5V or through a 10K ressistor?

4k7 means 4.7K Ohms (some places may call this 4,7K Ohms), otherwise known as 4700 Ohms.

Vcc of the chip should be wired to 5 volts, NOT through any resistors.

The resistors are used for wiring switches or pushbuttons.

The 10K to +5V referrers to the pull up resistor if you read all the sentence, not the power supply.

You need pullup resistors on the shift register input pins, 10K to +5V.

Note that all the inputs on the shift register need pulling up not just those with switches on.