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