So I am modifying an Instructable I found to work as a door/motion detector alarm for my apartment.
I want to get the code working properly just printing to the normal serial port before i go out and spend money on the GPRS shield and a SIM Card.
The issue is that the Arduino keeps flip flopping between the else if and else statements even when the motionPin reads low.
It keeps printing
DOOR IS CLOSED
MESSAGE SENT
DOOR IS CLOSED
MESSAGE SENT
it should only print
DOOR IS CLOSED
then do nothing until the motion sensor is tripped, then print
DOOR IS OPEN
MESSAGE SENT
then
DOOR IS CLOSED again.
I am using a PIR sensor from Radioshack so i guess i may need to include the LockLow function into this code, but I am not sure. Any help would be awesome.
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
SoftwareSerial mySerial(10, 11); //rx, tx
boolean doorOpen = false;
boolean messageSent = false;
int motionPin = 3; //pin detecting motion
int ledPin = 13; //red LED on pin 13
int calibrationTime = 5;
void setup() {
pinMode(motionPin, INPUT);
SIM900.begin(19200);
Serial.begin(19200);
Serial.println("BEGINNING SERIAL COMMUNICATION");
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");SIM900power();
//delay(20000); // give time to log on to network.
digitalWrite(motionPin, LOW);
messageSent = false;
}
void SIM900power() { // software equivalent of pressing the GSM shield "power" button
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(5000);
}
void sendSMS() {
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(100);
SIM900.println("AT + CMGS = "+16039983462""); // recipient's mobile number, in international format
delay(100);
SIM900.println("APARTMENT SECURITY ALERT -- THE FRONT DOOR HAS BEEN OPENED"); // message to send
delay(100);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println();
delay(5000); // give module time to send SMS
}
void loop() {
if (digitalRead(motionPin) == HIGH) { //if motionPin is HIGH the door has moved, and is therefore opened
Serial.println("DOOR IS OPENED");
doorOpen = true;
delay(1000);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
}
else if (digitalRead(motionPin) == LOW) { // door is closed so do reset messageSent and doorOpen states
doorOpen = false; //door is not open
messageSent = false; //reset the message status
Serial.println("DOOR IS CLOSED");
}
else (digitalRead(motionPin == HIGH) && (messageSent == false)); { //if the door is open, and the messageSent state is false, send a message and set the state to true, so we dont send one again.
//delay(1000); this will give you a one second delay to turn the unit off -- for me and my gf when entering the house when the alarm is active. not needed in the testing phase.
sendSMS();
Serial.println("MESSAGE SENT");
//messageSent = true;
messageSent = true;
}
}
