unsigned long doorOpenTime = 0;
unsigned long doorClosTime = 0;
int currDoorState;
int prevDoorState = LOW; // Assumes that LOW means closed and that the door is closed when the Arduino starts
void loop()
{
currDoorState = digitalRead(doorContactPin);
if(currDoorState != prevDoorState)
{
// Door was closed and is now open or door was open and is now closed
if(currState == LOW)
{
// Door is now closed
doorClosTime = millis();
}
else
{
// Door is now open
doorOpenTime = millis();
}
}
prevDoorState = currDoorState;
// Now see if the door was opened
if(doorOpenTime > 0)
{
// It was. Was it closed?
if(doorClosTime > 0)
{
// Yes. How far apart?
if(doorClosTime - doorOpenTime >= time_threshold)
{
SendSMS("Door closed after more than 3 seconds");
doorOpenTime = 0;
doorClosTime = 0;
}
}
else
{
// Door is still open. How long has it been open?
if(millis() - doorOpenTime >= time_threshold)
{
SendSMS("Door still open after more than 3 seconds");
// doorOpenTime = 0;
// doorClosTime = 0;
}
}
}
}
You can see that there are two places where an SMS is sent, for two different reasons. Either or both may be important to you. You may care, or not, that the door was finally closed. If so, leave them both in, and leave the door times commented out in the second block. If not, remove the first block containing the SendSMS() call, and uncomment the door time resets in the second block.
[/quote]
silly question but where does the first block of code start for SendSMS() is it.
{
SendSMS("Door closed after more than 3 seconds");
doorOpenTime = 0;
doorClosTime = 0;
}