// constants won't change. They're used here to set pin numbers:
const int btn = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
namespace button {
unsigned long interval = 200;
unsigned long buttonPressedTime = 0;
bool buttonState = false;
bool state(int buttonPin){
unsigned long currentMillis = millis();
int reading = digitalRead(buttonPin);
if (reading == LOW && !buttonState) {
// Button was just pressed
buttonState = true;
buttonPressedTime = currentMillis;
}
if (reading == HIGH && buttonState) {
// Button was just released
buttonState = false;
interval = 200; // Reset the interval to 200ms
}
return buttonState;
}
}
void setup() {
pinMode(btn, INPUT_PULLUP); // Set the button pin as an input with a pull-up resistor
Serial.begin(115200);
}
void loop(){
Serial.print("Button State: ");
Serial.println(button::state(btn));
if(button::state(btn)) ledState = HIGH;
else ledState = LOW;
digitalWrite(ledPin, ledState);
}