#define button_PIN 22
byte buttonState;
unsigned long debounceDelay = 50; // setting up for the debounce
unsigned long timePreviousButtonChange = millis(); // setting up for the debounce
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(button_PIN, INPUT);
}
void loop() {
unsigned long timeNow = millis(); // assinging the timeNow now to how many milliseconds its been since the loop has started
if (timeNow - timePreviousButtonChange > debounceDelay) { // checking to see if the delay has passed
byte newButtonState = digitalRead(button_PIN); // read the button state
// button state has changed
if (newButtonState != buttonState) {
buttonState = newButtonState; // update state variable
timePreviousButtonChange = timeNow; // updating the time of the last button press
if (buttonState == HIGH) {
Serial.println("button pressed");
} else {
Serial.println("button released");
}
}
// no delay as we want to see bouncing in the serial monitor
}
}