I am trying to build a simple on delay timer. I would like to press and hold a pushbutton for 5 seconds and then a LED would come on.
int ledPin = 13;
int buttonPin = 2;
int ledState = LOW;
int state=0;
long previousMillis = 0;
long interval = 5000;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
unsigned long currentMillis = millis();
state=digitalRead(buttonPin);
if (state==HIGH)
{
if(currentMillis - previousMillis > interval )
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;}
else{
ledState= LOW;
digitalWrite(ledPin, ledState);
}
}