jaylisto:
jaylisto:
UKHeliBob:
Does that code do what you want ?What happens if the user presses the button for 2.5 seconds then releases it ?
What will the value of x be then ?What value will x have after the user now presses the button for 1 second ?
ow. wow, thank you very much. i didnt recognize it. i tried it, it looks solved but i realized that you are correct.
i think i need to add x = 0 at the else statement. thank you for the post.thank you for your help, now it works like charm. i added x=0 in the else statement, so that when you releases the switch in 2.5 second it will reset the value of x to 0.
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 11;
int x = 0; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
x++;
delay(1000);
if (x==3) {
digitalWrite(ledPin, HIGH); // turn LED on:
x=0;
}
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
x=0; //to set the x as zero if its under 3 second, brilliant!
}
}