Checking a pin twice for LOW for 60 seconds before conformation

Hi,

I've a requirement to check for pin for LOW at least 60 seconds before confirmation..

I've done the below codes using flags , checking it once and 60 seconds later that it is still LOW ( powerPin > 100 )

Is there another way to use interrupts or timer to do a checks once and again after 60 seconds ??

Thanks

//global declarations
bool alreadyON = 1;
bool powerON = 1; 


    
      powerPin = analogRead(POWERPIN);

    if ( alreadyON ) {
         if ( powerPin > 100 ) {              // power pin is OFF
              alreadyON = 0;                   // Turn off this checking
              powerOFFtimer = millis();     // Record last time power was off              
          } 
    } else {
        // Check powerPin again and ensure it was still off after 60 secs
        if ( powerPin > 100 && ( millis() - powerOFFtimer > 60000 ) ) {

                powerON = 0;                  // Confirmed it is off after 60 secs

        } else if ( powerPin < 100 ) {
              powerON = 1;
              alreadyON = 1;
        }

Use a state machine.
Read the input and if it is high, make a time variable equal to the millis time.
Then check that the current time subtracted from the variable exceeds 60000 and when it does do what ever you want to do.

Would something like this work?

static unsigned long tStart = millis();
unsigned int tEnd = 60000;
if(powerPin >= 100)
  tStart = millis();
if(millis - tStart > tEnd)
  // been off for at least 60 sec.

I've a requirement to check for pin for LOW at least 60 seconds before confirmation..

Do you need the pin to stay LOW for 60 seconds or to be LOW at the end of 60 seconds

756E6C:
Would something like this work?

Given that you never read any input then no.

I took the question to mean stay LOW for 60 seconds.

I took the question to mean stay LOW for 60 seconds.

Me too

2 samples isn't enough to make sure signal is low for 60 seconds straight. I would say original code with flags is the right way to do it.

How about setting up a pin change interrupt. When the pin goes low the interrupt starts a 30 second timer which when done interrupts to signal that the pin has been low for 30 seconds.

If the pin goes high after going low the interrupt just turns off the timer.

When the pin goes low the interrupt starts a 30 second timer which when done interrupts to signal that the pin has been low for 30 seconds.

Messy.
Setting a timer to interrupt in 30 seconds is impossible, but you can code round that by counting how many times a shorter interrupt has been called. But you are using one interrupt and one timer, a bit resource heavy when the answer given in reply #1 can be done in a few lines of code.

Grumpy_Mike:
Messy.
Setting a timer to interrupt in 30 seconds is impossible,

Your are correct, the longest time would be about 4 secs. with a 16MHz sys clock and 1024 prescaler.
My bad.

Perhaps one day we will hear from the OP as to what he really wants.

If the OP wants to make sure the pin stays LOW for 60 seconds then he needs to read the pin as often as possible and save the value of millis() every time he finds the pin is HIGH. Then, when the difference between millis() and the saved value exceeds 60,000 he will know that it has been LOW all that time.

...R