I have changed some code around but also left some old code in as i was'nt sure how to edit it to the correct form.
#include <SoftwareSerial.h>
SoftwareSerial module(2,3); // We need to create a serial port on D2/D3 to talk to the GSM module
char mobilenumber[] = "07xxxxxxxx4"; // Replace xxxxxxxx with the recipient's mobile number
int doorContactPin = 4; // pin door contact is attached to
// NEW CODE
unsigned long doorOpenTime = 0;
unsigned long doorClosTime = 0;
unsigned long time_threshold = 3000; //time in ms => 3 sec.
int currDoorState;
int prevDoorState = LOW; // Assumes that LOW means closed and that the door is closed when the Arduino starts
//door delay
int time = 0;
int val = 0;
void setup()
{
module.begin(9600); //Initialize serial ports for communication.
delay(35000); // give the GSM module time to initialise, locate network etc.
pinMode(doorContactPin,INPUT);
digitalWrite(doorContactPin,LOW);
//door delay code.
digitalWrite(doorContactPin,LOW);
val = digitalRead(doorContactPin);
while(val == HIGH){
val = digitalRead(doorContactPin);
}
time = 0;
while((val == LOW)&&(time < time_threshold)){
time = time + 100;
val = digitalRead(doorContactPin);
delay(100);
digitalWrite(doorContactPin,LOW);
}
if (time >= time_threshold){
digitalWrite(doorContactPin,HIGH);
}
}
void sendSMS()
{
//send sms message
module.println("AT+CMGF=1"); // set SMS mode to text
module.print("AT+CMGS="); // now send message...
module.write(34); // ASCII equivalent of "
module.print(mobilenumber);
module.write(34); // ASCII equivalent of "
module.println();
delay(500); // give the module some thinking time
module.println("Door Open Alert"); // our message to send
module.write(26); // ASCII equivalent of Ctrl-Z
delay(5000); // the SMS module needs time to return to OK status
}
void loop()
{
//new code
currDoorState = digitalRead(doorContactPin);
if(currDoorState != prevDoorState)
{
// Door was closed and is now open or door was open and is now closed
if(currDoorState == 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();
doorOpenTime = 0;
doorClosTime = 0;
}
}
else
{
// Door is still open. How long has it been open?
if(millis() - doorOpenTime >= time_threshold)
{
sendSMS();
doorOpenTime = 0;
doorClosTime = 0;
}
}
}
}
I put your code in but have tried various ways since then to get the required delay after the door has opened. I have returned to the code above as what i have tried just does'nt work. The "millis()" function i assume is looking at the time_threashold, am I correct? I think my "delay code" is still messing things up but could not find answer from your previous advice on this. My brain is turning to liquid on this.