Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button library improvements #23

Merged
merged 10 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions Libraries/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
Expand Down
23 changes: 2 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions examples/Button/Button.ino
Original file line number Diff line number Diff line change
@@ -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");
}