diff --git a/Libraries/Button.h b/Libraries/Button.h index 6a6afda..0f15ef1 100644 --- a/Libraries/Button.h +++ b/Libraries/Button.h @@ -5,36 +5,43 @@ #define Button_h class Button { -public: - bool state; - bool fired; - bool lastFired; - unsigned long debounceTimer; - int debounce; +private: int pin; - void (*callback)(int state); - - Button() {} + bool lastPinState; + int debounce; + unsigned long pinChangeMillis; + bool buttonState; + void (*callback)(int); - void setup(int p, void (*CB)(int)) { +public: + // constructor (pin, callback function, [optional] debounce in millis) + Button(int p, void (*CB)(int), int _debounce = 20) { callback = CB; pin = p; pinMode(p, INPUT_PULLUP); - debounceTimer = 0; - debounce = 20; - lastFired = state = fired = true; + debounce = _debounce; + lastPinState = buttonState = (digitalRead(pin)); + } + + // returns the button state + bool getState() { + return buttonState; } + //run in a loop when button press should be noticed. void update() { - if (digitalRead(pin) != state) { - state = !state; - fired = !state; - debounceTimer = millis() + debounce; + bool pinState = digitalRead(pin); + + //if the state of the pin has changed. + if (pinState != lastPinState) { + lastPinState = pinState; + pinChangeMillis = millis(); } - if (debounceTimer < millis() && state != fired && lastFired != state) { - lastFired = fired = state; - callback(!state); + //if the pin state was stable for the debounce value in millis. + if (((millis() - pinChangeMillis) > debounce) && buttonState != pinState) { + buttonState = pinState; + callback(!buttonState); } } }; diff --git a/README.md b/README.md index 4e11d83..3b9733e 100644 --- a/README.md +++ b/README.md @@ -165,29 +165,10 @@ To setup a new `Button`, you'll need to pass in two parameters: 1. `int` pin, location of the input pin 2. `void (*callback)(int)`, a callback function that accepts the returned `int` value of the sensor +3. (optional) `int` debounce value in milliseconds. #### Button Example -``` -#include "Libraries/Button.h" - -Button button1; -int button1Pin = 2; - -void setup() { - - button1.setup(button1Pin, [](int state) { - if (state == 1) { - // This means, our button was pressed - // We should do something, like turn on a light - } - }); - -} - -void loop() { - button1.update(); -} -``` +See Examples Folder ### Timer.h This library is used to handle common timer-related functions. Currently, it does timing by counting from `millis()` although, in the future, `micros()` may be implemented. diff --git a/examples/Button/Button.ino b/examples/Button/Button.ino new file mode 100644 index 0000000..c0c286a --- /dev/null +++ b/examples/Button/Button.ino @@ -0,0 +1,25 @@ +// Arduino does not support relative paths :( +// To get this sketch to compile, you must update this include +// statement with your own absolute path to the library +#include "C:\Users\jmeyer\Documents\Code\arduino-base\Libraries\Button.h" + +//declare pin assignments at the top as constant integers +const int buttonPin = 4; + +Button myButton(buttonPin, &buttonFunction, 30); // pin, function reference, (optional)debounce in milliseconds. + +void setup() { + Serial.begin(9600); + pinMode(LED_BUILTIN, OUTPUT); +} + +void loop() { + myButton.update(); +} + +void buttonFunction(int state) { + //replace with code to execute based on button state. + digitalWrite(LED_BUILTIN, state); + if (state == 1) + Serial.println("Button pressed"); +}