Skip to content
Permalink
Browse files

#1 debounce for all buttons

  • Loading branch information...
MichalSkoula
MichalSkoula committed Sep 12, 2019
1 parent 94c7b26 commit d512ac3cfece537e1b4565388511b7f02c2dfd52
@@ -11,27 +11,20 @@ byte buttonPin(byte button)
case DOWN_BUTTON: return DOWN_BUTTON_PIN;
case CENTER_BUTTON: return CENTER_BUTTON_PIN;
}
// TODO vracet nějaký "bezpečný" pin?
return START_BUTTON_PIN;
}

bool buttonActive(byte button, byte state)
{
(void)button;
return state == LOW; // everything is now INPUT_PULLUP
return START_BUTTON_PIN;
}

unsigned long lastDebounceTime[7] = {0, 0, 0, 0, 0, 0, 0};
bool buttonPressed(byte button)
{
// holds last states of all buttons (currently 7 total)
static byte lastButtonState = 0;
// read the state of the switch into a local variable
bool pressed = !digitalRead(buttonPin(button));

byte state = digitalRead(buttonPin(button));
bool active = buttonActive(button, state);
if (active && state != bitRead(lastButtonState, button)) {
bitWrite(lastButtonState, button, active);
// it delay is over, we can return the real state
if ((millis() - lastDebounceTime[button]) > 200 && pressed) {
lastDebounceTime[button] = millis();
return true;
}
bitWrite(lastButtonState, button, active);
}
return false;
}
@@ -16,7 +16,6 @@ enum Pins {
BUZZER_PIN = 13
};

// TODO add ANY_BUTTON? START_OR_ACTION_BUTTON?
enum Buttons {
START_BUTTON = 0,
ACTION_BUTTON = 1,
@@ -28,12 +27,9 @@ enum Buttons {
CENTER_BUTTON = 6
};

// read and update button state
// return true when the button is pressed and then always false until
// the button is released and pressed again
// should not be called more than once per button per loop()
// for classical buttons loop() should have at least 3 ms of delay to filter out fake release
// TODO asi nefunguje pro joystick?


// read button state
bool buttonPressed(byte button);

#endif /* GAMELIB_INPUT_H */

0 comments on commit d512ac3

Please sign in to comment.
You can’t perform that action at this time.