/* Push Button as Momentary Switch
---------------------------------
Without delay function.
Easy to understand.
More efficient.
By Capt.Ali.H.K
4/12/2023
[email protected]
*/
#define BUTTON_PRESSED (!digitalRead(pushButton))
const uint8_t pushButton = 3;
void setup(){
pinMode(pushButton, INPUT_PULLUP);
Serial.begin(115200);
}
void loop(){
static bool currentPress;
static bool previousPress;
static bool buttonState;
static unsigned long debounceInterval;
if(millis() - debounceInterval > 15){
if(BUTTON_PRESSED){
currentPress = true;
debounceInterval = millis();
}
else
currentPress = false;
}
if(currentPress == LOW && previousPress){
buttonState = true;
Serial.println("Button Pressed");
previousPress = currentPress;
}
else if(currentPress && previousPress == LOW) {
buttonState = false;
previousPress = currentPress;
}
}