so here's an updated code with revisions.
#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[] = "078xxxxxxx4"; // Replace xxxxxxxx with the recipient's mobile number
int ledPin = 11;
int doorContactPin = 12; // pin door contact is attached to
//door delay
int time = 0;
int time_threshold = 3000; //time in ms => 3 sec.
int val = 0;
//pin state.
int current_val = 0; // val wil be used to store the state of the input pin.
int old_val = 0; // this variable stores the previous value of "val".
int state = 0; // 0 = doorContactPin open while 1 = doorContactPin closed.
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);
pinMode(ledPin,OUTPUT);
digitalWrite(doorContactPin,HIGH);
}
void loop()
{
//door delay code.
digitalWrite(ledPin,HIGH);
val = digitalRead(doorContactPin);
while(val == LOW){
val = digitalRead(doorContactPin);
}
time = 0;
while((val == HIGH)&&(time < time_threshold)){
time = time + 100;
val = digitalRead(doorContactPin);
delay(100);
digitalWrite(ledPin,HIGH);
}
if (time >= time_threshold){
digitalWrite(ledPin,LOW);
}
//door state
current_val = digitalRead(doorContactPin); // read input value and store it.
// check if there was a transition.
if ((current_val == HIGH) && (old_val == LOW)){
state = 1 - state;
delay(10);
}
old_val = current_val; // val is now old, let's store it
if (state == 1) {
digitalWrite(doorContactPin, HIGH); // doorContactPin closed.
}
else {
digitalWrite(doorContactPin, LOW); // doorContactPin open.
}
//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
}
still feels like I'm not doing something right.. I get the sms everytime the door is open but I dont think the code for the door state is actually doing anything.
As was mentioned before. I still get sms for the duration of the door been open.
What other code do i need?