IoT - Modular - Buttons.h

From The TinkerNet Wiki
Revision as of 00:01, 2 August 2021 by Grant (talk | contribs)
Jump to navigation Jump to search

<syntaxhighlight lang="cpp" line="1"> class PushButton {

public:

PushButton(uint8_t pin) // Constructor (executes when a PushButton object is created)

: pin(pin) { // remember the push button pin

pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor

};

bool isPressed() // read the button state check if the button has been pressed, debounce the button as well

{

bool pressed = false;

bool state = digitalRead(pin); // read the button's state

int8_t stateChange = state - previousState; // calculate the state change since last time

if (stateChange == rising) { // If the button is pressed (went from high to low)

if (millis() - previousBounceTime > debounceTime) { // check if the time since the last bounce is higher than the threshold

pressed = true; // the button is pressed

}

}

if (stateChange == falling) { // if the button is released or bounces

previousBounceTime = millis(); // remember when this happened

}

previousState = state; // remember the current state

return pressed; // return true if the button was pressed and didn't bounce

};

private:

uint8_t pin;

bool previousState = HIGH;

unsigned long previousBounceTime = 0;

const static unsigned long debounceTime = 1000;

const static int8_t rising = HIGH - LOW;

const static int8_t falling = LOW - HIGH;

};

// <syntaxhighlight>