thank you very much with this sir. ill try this style.
but i think if you press the switch 3 times it will light up. (not holding the switch) . though ill just try it if it works..
}
Your code layout makes it very difficult what your control structure is.
Please adopt the habit of putting each { and } on separate lines, with matching pair indented by the same amount, and with the code between them indented one extra level. It makes it much, much easier for us (and you) to understand the structure of the code and also to spot where the structure is not what you intended.
Your code layout makes it very difficult what your control structure is.
Please adopt the habit of putting each { and } on separate lines, with matching pair indented by the same amount, and with the code between them indented one extra level. It makes it much, much easier for us (and you) to understand the structure of the code and also to spot where the structure is not what you intended.
pls give me the answer coz my program didnt work. i repeat, there is no problem with the correct code. i only did that for representation (i know its wrong)
this is a very simple problem, though no one can answer even me.
the solution for my answer, i think...
check if the switch is high
delay (100)
check if the switch still high
delay (100)
check if the switch still high
delay (100)
check if the switch still high
turn on the led
but how can i transfer this to a code or something?.
if no one can help me then
ill keep trying to solve this problem.
woohooooooo. yes! it works! for all the answer out their. this is the only one made me smile ,
very good sir, thank you very much!. you only use looping but its the same with mine. what exactly is the one second as a delay? is it delay(1000)?
. you really help me sir. thank you very much.
Im sorry jaylisto, The program that i gave to you does not do what you need it to do. I thought you need to press the switch 3 time b4 the led will light up. so its does not do acording to what you want.
btw i want to say its not good that you anger all those post a reply for your question. those people have help me learn alot.
@Arrch
I wanted to see if he would insult me too afterwards, If he did, I would've removed my post and leave him with nothing. I can tolerate rude people to an extent, as I do everyday as a tech support technician. But if I try to help them and they are still ungreatful, then they get blocked.
I didn't get insulted by him yet, so I guess it worked.
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.
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.
brilliant! wow, i never know this kind of style. in school i never use this kind of style. you compared it in the last value of the variable.
very good sir. i will try this one. i will bow my head for you and say thanks.
and! what do you mean by the code millis();?
by the way since you are a genius,i have an out of topic question for you.
is it possible to multi quote? so that i can reply to all who posted in my topic easily?. thanks genius man.
HazardsMind: @Arrch
I wanted to see if he would insult me too afterwards, If he did, I would've removed my post and leave him with nothing. I can tolerate rude people to an extent, as I do everyday as a tech support technician. But if I try to help them and they are still ungreatful, then they get blocked.
You're better than I am at it. I have the philosophy that if you insult anyone trying to help, not just me, I'm not helping until they are apologized too, but that wouldn't work too well for tech support I would imagine.
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!
}
}