# HG changeset patch # User ark # Date 1485382336 -3600 # Node ID 284066a427c887d6b8f4f11ac29452e230b601cc # Parent 968a3e3eb8efbb47552631ab4a7cbb1eefe36b1d added flanks detection diff -r 968a3e3eb8ef -r 284066a427c8 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Wed Jan 25 23:12:16 2017 +0100 @@ -0,0 +1,1 @@ +.DS_Store diff -r 968a3e3eb8ef -r 284066a427c8 Button.cpp --- a/Button.cpp Sun Jan 22 23:39:37 2017 +0100 +++ b/Button.cpp Wed Jan 25 23:12:16 2017 +0100 @@ -20,7 +20,7 @@ this->pin = pin; this->isPullup = pu; pinMode(this->pin, this->isPullup?INPUT_PULLUP:INPUT); - this->isPressed = this->readState(); + this->isOn = this->readState(); this->lastChangeMs = 0; } @@ -32,17 +32,18 @@ void Button::update(unsigned long ms) { bool s = this->readState(); // process state change - if(this->isPressed != s) { + if(this->isOn != s) { // if last state change is older than threshold, accept new state // change is not a bounce. if(ms - this->lastChangeMs >= Button::BOUNCE_THRESHOLD) { - this->isPressed = s; + this->isPressed = this->isOn; + this->isReleased = !this->isOn; + this->isOn = s; this->lastChangeMs = ms; } - else { - Serial.print("ignoring bounce at "); - Serial.println(ms); - } + } else { + this->isPressed = false; + this->isReleased = false; } } diff -r 968a3e3eb8ef -r 284066a427c8 Button.h --- a/Button.h Sun Jan 22 23:39:37 2017 +0100 +++ b/Button.h Wed Jan 25 23:12:16 2017 +0100 @@ -1,6 +1,7 @@ #ifndef Button_h #define Button_h + /** utility class for debouncing a button based on pin state and time. @@ -12,10 +13,14 @@ class Button { public: // stabilised button state - bool isPressed; + bool isOn; // millis() time of last state change unsigned long lastChangeMs; - + // flag, true if state changed to pressed + bool isPressed; + // flag, true if state changed to off + bool isReleased; + /** create an uninitialised Button. setup() must be called after this to make it usable. diff -r 968a3e3eb8ef -r 284066a427c8 library.properties --- a/library.properties Sun Jan 22 23:39:37 2017 +0100 +++ b/library.properties Wed Jan 25 23:12:16 2017 +0100 @@ -3,6 +3,7 @@ author=Arkadiusz Juszczyk maintainer=Arkadiusz Juszczyk sentence=Debounced button for Arduino +paragraph=Debounced button for Arduino category=Signal Input/Output url=https://hg.wobblyhead.net/arduino/Button architectures=*