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.
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.
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.
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.
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.
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
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.