wobblyhead hg > arduino > Button
comparison Button.cpp @ 1:284066a427c8
added flanks detection
| author | ark |
|---|---|
| date | 2017-01-25 23:12 +0100 |
| parents | 968a3e3eb8ef |
| children | b76b999b88c7 |
comparison
equal
deleted
inserted
replaced
| 0:968a3e3eb8ef | 1:284066a427c8 |
|---|---|
| 18 | 18 |
| 19 void Button::setup(byte pin, bool pu) { | 19 void Button::setup(byte pin, bool pu) { |
| 20 this->pin = pin; | 20 this->pin = pin; |
| 21 this->isPullup = pu; | 21 this->isPullup = pu; |
| 22 pinMode(this->pin, this->isPullup?INPUT_PULLUP:INPUT); | 22 pinMode(this->pin, this->isPullup?INPUT_PULLUP:INPUT); |
| 23 this->isPressed = this->readState(); | 23 this->isOn = this->readState(); |
| 24 this->lastChangeMs = 0; | 24 this->lastChangeMs = 0; |
| 25 } | 25 } |
| 26 | 26 |
| 27 void Button::update() { | 27 void Button::update() { |
| 28 this->update(millis()); | 28 this->update(millis()); |
| 30 | 30 |
| 31 | 31 |
| 32 void Button::update(unsigned long ms) { | 32 void Button::update(unsigned long ms) { |
| 33 bool s = this->readState(); | 33 bool s = this->readState(); |
| 34 // process state change | 34 // process state change |
| 35 if(this->isPressed != s) { | 35 if(this->isOn != s) { |
| 36 // if last state change is older than threshold, accept new state | 36 // if last state change is older than threshold, accept new state |
| 37 // change is not a bounce. | 37 // change is not a bounce. |
| 38 if(ms - this->lastChangeMs >= Button::BOUNCE_THRESHOLD) { | 38 if(ms - this->lastChangeMs >= Button::BOUNCE_THRESHOLD) { |
| 39 this->isPressed = s; | 39 this->isPressed = this->isOn; |
| 40 this->isReleased = !this->isOn; | |
| 41 this->isOn = s; | |
| 40 this->lastChangeMs = ms; | 42 this->lastChangeMs = ms; |
| 41 } | 43 } |
| 42 else { | 44 } else { |
| 43 Serial.print("ignoring bounce at "); | 45 this->isPressed = false; |
| 44 Serial.println(ms); | 46 this->isReleased = false; |
| 45 } | |
| 46 } | 47 } |
| 47 } | 48 } |
| 48 | 49 |
| 49 | 50 |
| 50 bool Button::readState() { | 51 bool Button::readState() { |
