HazardsMind:
const int buttonPin = 2;
const int ledPin = 11;
int buttonState = LOW;
int lastReading = LOW;
long onTime = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && lastReading == LOW) {
onTime = millis();
}
//held
if (buttonState == HIGH && lastReading == HIGH) {
if ((millis() - onTime) > 3000 ) { //(current time - first pressed time) must be greater than 3000 (3 seconds)
digitalWrite(ledPin, HIGH);
lastReading = LOW; //reset state
}
else {
digitalWrite(ledPin, LOW);
}
}
lastReading = buttonState; //write button to lastreading for compare
}
I can not try this myself right now, but it should work.
your code works!!! i already tried it.
but my code also works as i add x = 0 in the else statement. anyway thank you.