constexpr int buttonPin = 3; // the number of the pushbutton pin
// variables will change:
byte buttonState; // variable for reading the pushbutton status
byte lastButtonState = 1;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbutton value:
buttonState = buttonStatus();
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
if (buttonState == HIGH) {
Serial.println("OFF");
} else {
Serial.println("ON");
}
}
}
boolean buttonStatus() {
static unsigned long lastChange = 0;
static byte state = HIGH;
static byte lastState = HIGH;
byte actState = digitalRead(buttonPin);
if (actState != lastState) {
lastChange = millis();
lastState = actState;
}
if (state != lastState && millis() - lastChange > 30) {
state = lastState;
}
return state;
}