Ok, I give up. I have 5 buttons
I want to debounce them in an odd way.
The buttons are on pins: buttonpins*; (0 - 4)*
The button states used in the rest of the code are: buttonstates*; (0 - 4)*
If no buttons are pressed, I don't want any buttons to give a 'pressed' reading until 200-300 (call it btnDelay) millis after the first button was pushed. The buttons are converted into a binary input from the user, but because a person can't push two or three buttons with their hand at exactly the same time, I get invalid binary readings.
Hope this makes sense!
Thanks
hmm...
r u doing it like this:
void loop() {
uint32_t when[5];
bool reported[5];
bool pressed[5] = {false,false,false,false,false};
for (;;) {
for (uint8_t bn=0; bn<5; bn++) {
const uint32_t now = millis();
const bool bs = digitalRead(bn);
if (pressed[bn] && !bs) {
if (reported[bn]) {
Serial.print(bn); Serial.println(" released");
reported[bn] = false;
}
pressed[bn] = false;
} else if (!pressed[bn] && bs) {
when[bn] = now;
reported[bn] = false;
pressed[bn] = true;
} else if (!reported[bn] && bs && now-when[bn]>500) {
Serial.print(bn); Serial.println(" pressed");
reported[bn] = true;
}
}
}
}
?
...I don't think this does it? It watches the time on EACH button, but I only want to delay BtnDelay from the time the first button is pressed.
void scanButtons() {
boolean anyReading = false;
for (int i = 0; i <NUMBUTTONS; i++){
int reading = digitalRead(buttonPins[i]);
if (reading == 0) anyReading = true; //at least 1 button was pressed }
if (anyReading == false) { //no button pressed for (int i = 0; i <NUMBUTTONS; i++){
int reading = digitalRead(buttonPins[i]);
buttonStates[i] = reading; //set all buttons to off
prevButtonTime = millis(); //update the time. once a button is pressed this time will
give the time of first button press }
}
if (millis() - prevButtonTime < buttonDelay) return; //no need to get a reading until buttonDelay amount of time after the 1st press
//if we've made it this far, go ahead and take an official reading of the buttons for (int i = 0; i < NUMBUTTONS; i++)
{
int reading = digitalRead(buttonPins[i]);
buttonStates[i] = reading; };
}
Got it!
cool