Delay ON Timer with trigger

HI
I have been trying to incorporate a delay ON timer with a trigger. In short if a button is pressed, start the timer (t1) and at the end of the time period ("say 1min") check the status of the switch if it is still held pressed ONLY switch on the output.

(IMPORTANT!!!) If the Switch was released at ANY point during the t1 interval the timer start back up. Example, if the switch was RELEASED at 10seconds and pressed again at 20 seconds then the t1 should start from 20 seconds and start counting again for a minute.
Any help or directions would be greatly appreciated.

Let's try to simplify your requirements:

If, and only if, a button has been held continuously for one minute, activate an output.

Did that work?

yes that did work using a "delay()" but it does not know if the switch was released during the delay interval. Thanks

You have to "think backwards" for this. Of course you have to use millis().

In loop():

Maintain a time stamp variable. If the switch is inactive, set it to millis().
If millis() minus the time stamp is greater than 60 seconds, activate the output.

It really is that simple, I think. Show us the resulting code.

1 Like

please see the code below Stage THREE is where I want the delay to take place before the pin8 is HIGH and pin7 is LOW, SW1 is the one I need to keep monitoring for a period of (time_delay) with out any interrupts, if any start the count again. I don't have much programming experience so please excuse me.

int SW1,SW2,SW3 ;
const int LM1 = 2;
const int LM2 = 4;
const int LM3 = 12;
unsigned long time=0;
unsigned long time_delay =60000;//unsigned long for very long 

void setup()
{
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(2,INPUT); 
pinMode(4,INPUT);
pinMode(12,INPUT);
delay(200); 
}
void loop()
{

SW1=digitalRead(2);
SW2=digitalRead(4);
SW3=digitalRead(12);

if(SW1==LOW&&SW2==HIGH&&SW3==LOW){//One

digitalWrite(8,HIGH);
digitalWrite(7,HIGH);
analogWrite(9, 250);
}

if(SW1==LOW&&SW2==LOW&&SW3==HIGH){//TWO
delay(100);
digitalWrite(8,LOW);
digitalWrite(7,HIGH);
analogWrite(9, 55);
}
if(SW1==HIGH&&SW2==LOW&&SW3==LOW){//Five
delay(100);
digitalWrite(8,HIGH);
digitalWrite(7,LOW);
analogWrite(9, 55);
}
if(SW1==LOW&&SW2==LOW&&SW3==LOW){//Six
delay(100);
digitalWrite(8,LOW);
digitalWrite(7,HIGH);
analogWrite(9, 55);
}
if(SW1==HIGH&&SW2==HIGH&&SW3==LOW){//Stage Three

time=millis();
while((millis() - time)< time_delay){//THIS THE POINT WHERE I WANT THE TIMER TO WAITE AND CHECK
digitalWrite(8,HIGH);
digitalWrite(7,LOW);
analogWrite(9, 250);
}

}
if(SW1==HIGH&&SW2==LOW&&SW3==HIGH){//Four  
delay(100);
digitalWrite(8,HIGH);
digitalWrite(7,HIGH);
analogWrite(9, 250);
}
}

It's my fault, but I forgot to mention code tags. I will read your code but you should really go back and add them. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

Okay, so basically you have not implemented the simple strategy outlined above. Your "stage 3" is sort of like the second sentence of the plan I gave you. First of all, your check in "stage 3" should not be a while. It should be an if, so the loop can continue to run. Somewhere else, you have to implement the first sentence of the plan I gave you.

There is no way to "stop and wait". That is precisely what caused your program to go blind to button presses in the first place.

You must remove all your delay() calls, or your program will never work the way you are expecting it to. It is because you need to do several things at the same time.