Requiring Long Press to turn on LED

I would like to create code that only turns on an LED if a button is held for two seconds. I would have thought the code below would do that but it just delays the turn on time for a momentary press. Can anyone tell me why the second IF command is ignored and how to fix it?

Thank you very much.

const int BUTTON=2;
const int LED=3;
int BUTTONstate=0;
int safetystart=2500;
int timerswitch=4;
int stdrun=5000;
int longrun=5000;
int buttonread=11;
int timerstate=0;

void setup() {
// put your setup code here, to run once:
pinMode(BUTTON,INPUT);
pinMode (LED, OUTPUT);
pinMode(buttonread, INPUT);
//Serial.begin(9600);

}

void loop()
{
// put your main code here, to run repeatedly:

int BUTTONstate=digitalRead(BUTTON);

if (BUTTONstate==HIGH){
delay(safetystart);
}
if (BUTTONstate==HIGH){
digitalWrite(LED,HIGH);
delay(stdrun);

I will guess that your input pin connected to the button is floating.

Perhaps you would show the forum some respect by reading the guide and following it, for example posting a schematic and using code tags.

I guess, too :slight_smile:
all in all you need a timer() funktion and button-handler, how ever designed inside your sketch.

Hello Paul,

I had no disrespect intended. I'm new to this with nobody to turn to. I'm still learning the etiquette.

Thank you,

Mike

You need to read the button again. You're testing a variable after the delay that will not have changed.

@wildbill is correct, I should have spotted that.

Hello,

Yes! That is exactly the problem. I assumed the Arduino would check it again, as I have asked it to.

I'm sorry that this may be an elementary question, but how would I do that?

Would I set another variable and do the same test again or can I tell it to forget the last reading and test again?

Thank you so much for your help!

Mike

You have a common misunderstanding I think. This:

int BUTTONstate=digitalRead(BUTTON);

Means read the button now and store the result in a variable called ButtonState. It does not mean that any future reference to ButtonState will cause the read to be done again. You will need to explicitly call digitalRead again to find out if it is still pressed after the delay.

Also if you modify the script slightly you might only check if a button is pushed at one moment and two seconds later. It does not matter if the button is pushed in between the two seconds.

And also the floating of the pin as already said could delay the turn on of the LED without you intending it. So your schematic is essential.

I dont know if its the best way to do things but i would write something like that:

This function will check if enough time has passed

loop(){
unsigned long test_timer=millis(); // (re)sets your timer

// your button is pushed for the first time
if (digitalRead(BUTTON)==HIGH){
// while the button is still pushed you check if "saftystart" has passed
while (digitalRead(BUTTON)==HIGH){
// if "saftystart"  has passed, you turn on the LED
if (millis()-test_timer>safetystart){
digitalWrite(LED,HIGH);
break; // leave the while loop
}
}
}
}

(I did not run this code, so I wont guaranty it will run, this is mainly for the idea)

Consider that once the LED is turned on it will never turn off again with that code.

You could insert code tags by editing your previous post or posting the script again

Thank you everyone. I will draw the schematic and insert code tags tonight, after work and kids' sports.

Thank you!

Hello,

I am sorry for the delay in this. I have two very active kids and one of them had me at a football tournament all weekend over two hours from home. It was a great weekend for an outdoor tournament, though. I did what you all said. I added notes to the code and I drew out the schematics.

Thank you all so much,

Mike

// Pin 2 is input of start button(s)
const int BUTTON=2;

//Pin 3 is output to LED. It is the only output.
const int LED=3;

// This indicates that the state of the button is an interger but all I really need is HIGH and LOW
int BUTTONstate=0;

// This states that the LED should not light until the safetystart delay takes place.
int safetystart=2500;

//After the LED lights, the duration of it's ON time will be determined by input pin #4.
int timerswitch=4;

//This is the short LED on run time.
int stdrun=5000;

//This is the extension of time the LED should run in excess to the short run time.
int longrun=5000;

//This reads if the LED is on. This will be used to determin if the longrun should be active or not.
int buttonread=11;

//This will read to see if the long run should take place. It reads the position of the switch at pin 4 (timerswitch).
int timerstate=0;

void setup() {

pinMode(BUTTON,INPUT);
pinMode (LED, OUTPUT);
pinMode(timerswitch,INPUT);
pinMode(buttonread, INPUT);
}

void loop()
{
int BUTTONstate=digitalRead(BUTTON);
int timerstate=digitalRead(timerswitch);
int BUTTONagain=digitalRead(BUTTON);

//If the button(s) sending a High signal to input Pin 2 are pressed, wait 2.5 seconds to ensure LED does not turn on if button(s) are only momentarily pressed.
//This ensures the operator truly wants to turn on the LED.
//I say button(s) because I have wired two buttons in series. They are not on separate circuits, so they effectively work as one button, if they are both pressed simultaneously.
if (BUTTONstate==HIGH){
delay(safetystart);
}

//If, after 2.5 seconds, the button(s) are still indicating High, then turn on the LED for the stdrun (standard run time of 5 seconds).
//I understand that the button(s) may become unpressed and pressed again and it will still indicate a High reading. Ideally, I would rather the button(s) need to be held for the duration.
if (BUTTONstate==HIGH){
digitalWrite(LED,HIGH);
delay(stdrun);
}

//At the end of the stdrun of 5 seconds, the Arduino should check to see if the timerstate switch is set to HIGH. If it is set to HIGH, then it should run the longrun (long run time).
//For now, the longrun is also set to 5 seconds, effectively bringing the total LED on time to 10 seconds.

//If the timerstate button is not HIGH, then turn off the LED
if (timerstate==HIGH && BUTTONstate==HIGH){

delay(longrun);
}

//After all timers have completed, turn the LED off.
digitalWrite(LED,LOW);
}

Hello
please post your sketch formated and in code tags </>.

No disrespect, but have you tried searching the forum for LONG BUTTON PRESS .

Don't worry about disrespecting @mkoumou, he continues to disrespect the forum. Claims to mean no disrespect, but continues to ignore the forum guide. I'm out.

Hello,

Honestly, I am trying to learn and do what some of you are saying. You keep talking about respect but you have been rude and insulting. If you do not want to help, just ignore my post. The last post even said "Don't worry about disrespecting me."

Maybe consider why you search out to find my post, and then reply with anger.

Have a good night. I'll find other ways to figure it out.

Mike

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.