Go Down

### Topic: if pin 1 is high in 3seconds led will turn on else ignore .(if statement .help.) (Read 3577 times)previous topic - next topic

#### jaylisto

this is the situation, press the switch in more than 3 seconds before the led turn on unless it wont.

my idea didnt work.
Code: [Select]
`if ( button, HIGH) {delay(500);x + 1;if ( button, HIGH) {delay(500);x + 1;if (button, HIGH) {delay(500);x + 1;if ( x == 3) {digitalwrite( "leds turns on")x = 0;} } } }else {x=0;}`

just give me the idea.
jaylisto

#### PaulS

Code: [Select]
`if ( button, HIGH) {`
It is highly unlikely that the comma operator is doing what you think it is doing. You can't just invent a "command" and expect it to work.

There is a function to read the state of a digital pin. You should figure out what that digitalRead() function is, and how to use it.

#### PaulS

Oh, and adding 1 to x and discarding the result is an exercise in futility.

#### ash901226

it should be something along this line

Code: [Select]
`if (Switch==HIGH){ x++; if (x==3) {   digitalWrite(LED,HIGH);   x=0; }}`

#### jaylisto

it should be something along this line

Code: [Select]
`if (Switch==HIGH){ x++; if (x==3) {   digitalWrite(LED,HIGH);   x=0; }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..}`
jaylisto

#### PeterH

Code: [Select]
`if ( button, HIGH) {`

Should be :

Code: [Select]
`if(digitalRead(button) == HIGH) {`

Code: [Select]
`x + 1;`
Should be:
Code: [Select]
`x = x + 1;`

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.
I only provide help via the forum - please do not contact me for private consultancy.

#### jaylisto

Code: [Select]
`if ( button, HIGH) {`

Should be :

Code: [Select]
`if(digitalRead(button) == HIGH) {`

Code: [Select]
`x + 1;`
Should be:
Code: [Select]
`x = x + 1;`

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.
jaylisto

#### PaulS

Quote
there is no problem with the correct code.

Then post that code.

#### jaylisto

#8
##### Jan 14, 2013, 11:50 amLast Edit: Jan 14, 2013, 07:36 pm by AWOL Reason: 1

it should be something along this line

Code: [Select]
`if (Switch==HIGH){ x++; if (x==3) {   digitalWrite(LED,HIGH);   x=0; }}`

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.

Moderator edit: gibberish removed
jaylisto

#### jaylisto

#9
##### Jan 14, 2013, 12:02 pmLast Edit: Jan 14, 2013, 07:51 pm by AWOL Reason: 1

Quote
there is no problem with the correct code.

Then post that code.

Code: [Select]
`const int buttonPin = 2;     const int ledPin =  11;int x = 0; int buttonState = 0;void setup() {    pinMode(ledPin, OUTPUT);          pinMode(buttonPin, INPUT);     }void loop(){    buttonState = digitalRead(buttonPin);  if (buttonState == HIGH) {  x++;          delay(1000);// turn LED on:         if (x==3) {              digitalWrite(ledPin, HIGH);    x=0;        }  }      else {      digitalWrite(ledPin, LOW);  }}`
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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### ash901226

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.

#### HazardsMind

#12
##### Jan 14, 2013, 05:20 pmLast Edit: Jan 14, 2013, 05:30 pm by HazardsMind Reason: 1
Code: [Select]
`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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

#### Arrch

Of course, there are always people who will write code for others despite any attitude problems.

#### AWOL

#14
##### Jan 14, 2013, 07:40 pmLast Edit: Jan 14, 2013, 07:50 pm by AWOL Reason: 1
I've removed a lot of invective and insults from this thread.
OP, if this continues, this thread goes and your account risks going with it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up

Please enter a valid email to subscribe