Go Down

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

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

I didn't get insulted by him yet, so I guess it worked.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

#### jaylisto

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

#### jaylisto

#17
##### Jan 15, 2013, 03:31 pmLast Edit: Jan 15, 2013, 03:34 pm by jaylisto 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.

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

#### HazardsMind

Multi quote? Not that im aware of. Usually you just write "@To all who helped", or something like that.

Once you write the new post, the people who did help, will read it.

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

#### Arrch

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

#### jaylisto

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.

Code: [Select]
`const int buttonPin = 2;     // the number of the pushbutton pinconst int ledPin =  11;int x = 0; // the number of the LED pin// variables will change:int buttonState = 0;         // variable for reading the pushbutton statusvoid 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!   }}`
jaylisto

#### jaylisto

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.

your code works!!! i already tried it.
but my code also works as i add x = 0 in the else statement. anyway thank you.
jaylisto

#### jaylisto

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.

wow, your code is amazing, since i realize in my arduino other programs are affected due to delay,
how did you do this thing? you did not use delay.

can you explain how did this thing works?
jaylisto

#### AWOL

Have a look at, and more importantly play with, the blink without delay example in the IDE, without delay.
"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.

#### UKHeliBob

You know the time that the button was pressed (onTime) copied from millis() when it happened.
Later there is a check whether the current time, millis(), minus onTime is greater than 3000.  If it is, then 3000 milliseconds (3 seconds) have passed and something needs to be done.  If not then don't hang around waiting, get on with something else and check the elapsed time again next time round the loop()

Does that help ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### jaylisto

You know the time that the button was pressed (onTime) copied from millis() when it happened.
Later there is a check whether the current time, millis(), minus onTime is greater than 3000.  If it is, then 3000 milliseconds (3 seconds) have passed and something needs to be done.  If not then don't hang around waiting, get on with something else and check the elapsed time again next time round the loop()

Does that help ?

the millis records the time between the button is pressed and released? right?

ok, if the value of the millis is passed to "ontime"

why did he minus the millis to the ontime?

it supposed to be ontime>3000

but the code works well. i wonder why.
jaylisto

#### Quick5pnt0

mills() is the time in milliseconds since the arduino powered up

#### jaylisto

#27
##### Jan 21, 2013, 03:45 amLast Edit: Jan 21, 2013, 03:47 am by jaylisto Reason: 1

Have a look at, and more importantly play with, the blink without delay example in the IDE, without delay.

ok from your code its hard to transfer what i have done,

i dont know if its better to make another topic or just post it here, i have just added a sensor.

if the analog pir sensor has an output and it trigers in 3 seconds just like the switch before. the led 13 must turned on.

this is the working code with delay.

Code: [Select]
`// Uses a PIR sensor to detect movement, buzzes a buzzer int ledPin = 13;                // choose the pin for the LED int x = 0;int inputPin = A0;               // choose the input pin (for PIR sensor)             // we start, assuming no motion detected int val = 0;                    // variable for reading the pin status void setup() {   pinMode(ledPin, OUTPUT);      // declare LED as output } void loop(){   val = analogRead(inputPin);  // read input value   if (val < 1) {            // check if the pir analog has trigerd (i just put one because the analog pir sensor produces                                     morethan  1 ohms when it detects something),.  x++; delay(1000);if (x==3){      digitalWrite(ledPin, HIGH);  // turn LED ON      x=0;  }  else {     digitalWrite(ledPin, LOW); // turn LED OFF    x=0;    delay(300);       } }     `

this code worked, the light turns on when the sensor sens a person in 3 seconds.

without delay style

, implemented to your code.

Code: [Select]
`const int pirbuttonPin = A0;     const int ledPin =  13;int buttonState = false;int  lastReading = false;long onTime = 0;void setup() {   pinMode(ledPin, OUTPUT);      }void loop(){   buttonState = analogRead(pirbuttonPin);    if (buttonState == true && lastReading == false) {        onTime = millis();      }//held  if (buttonState == true && lastReading == true) {    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}`

it did not worked, what is wrong with my code? hope some one helps me.
jaylisto

#### HazardsMind

The reason it's not working is because analog gets values 0-255 and digital is 0 or 1. So as of right now your code is looking for a value of 255 to trigger. Your pin might not be putting out 255 but something like 168 or something. So you need to set a range to trigger the if statement to be true.

First you need to see what the value is, then set the IF statement to trigger at that value. It's the same in theory, but with a different setup. Instead of digital readings, it's analog.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

#### jaylisto

The reason it's not working is because analog gets values 0-255 and digital is 0 or 1. So as of right now your code is looking for a value of 255 to trigger. Your pin might not be putting out 255 but something like 168 or something. So you need to set a range to trigger the if statement to be true.

First you need to see what the value is, then set the IF statement to trigger at that value. It's the same in theory, but with a different setup. Instead of digital readings, it's analog.

the upper code is working well, with the if statement greater than 1 . i have messured the analog sensor in digital tester as 0 = (no detection) and more than one ohms if detected.. so i did the line <1 ..... working as good. but when i use without delay method. wont work at all.
jaylisto

Go Up

Please enter a valid email to subscribe

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy