if pin 1 is high in 3seconds led will turn on else ignore .(if statement .help.)

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.

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

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.

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

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.

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.

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.

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.

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.

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!
  }
}

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.

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.

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?

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

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 ?

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 ?

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.

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

AWOL:
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.

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

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.

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.

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.

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.

I was referring to the without delay method. That code is looking for a "constant" HIGH for 3 seconds. As I said before, you need to measure your pin, and set the IF statement accordingly. Analog high is 255, so if the pin is NOT 255 for 3 seconds, it won't trigger. So you need to set a value range so that it does trigger after 3 seconds.

HazardsMind:
I was referring to the without delay method. That code is looking for a "constant" HIGH for 3 seconds. As I said before, you need to measure your pin, and set the IF statement accordingly. Analog high is 255, so if the pin is NOT 255 for 3 seconds, it won't trigger. So you need to set a value range so that it does trigger after 3 seconds.

you mean this one?

if (buttonState == true && lastReading == false)

to

if (buttonState > 1 && lastReading < 1 )