wobblyhead hg > arduino > Button
view Button.cpp @ 2:b76b999b88c7 default tip
fix flank detection
| author | ark |
|---|---|
| date | 2017-01-31 00:07 +0100 |
| parents | 284066a427c8 |
| children |
line wrap: on
line source
#if defined(ARDUINO) && ARDUINO >= 100 #include "Arduino.h" #else #include "WProgram.h" #endif #include "Button.h" /** see Button.h for description. */ Button::Button() {} void Button::setup(byte pin) { this->setup(pin, true); } void Button::setup(byte pin, bool pu) { this->pin = pin; this->isPullup = pu; pinMode(this->pin, this->isPullup?INPUT_PULLUP:INPUT); this->isOn = this->readState(); this->lastChangeMs = 0; } void Button::update() { this->update(millis()); } void Button::update(unsigned long ms) { bool s = this->readState(); // process state change this->isPressed = false; this->isReleased = false; 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->isOn = s; this->isPressed = this->isOn; this->isReleased = !this->isOn; this->lastChangeMs = ms; } } } bool Button::readState() { bool s = digitalRead(this->pin); if(this->isPullup) s = !s; return s; }
