As I should expect(?), the PIR on pin#2 should trigger the trigsensor state at any time the board is running. However, I may get one trigger then nothing else. I have a GSM shield on top of a UNO. I know that I still have some tweaking to do, but I'm trying to solve one issue at a time. Thanks for any insight. The other functions seem to perform correctly. When I enter an incorrect code, the alarms operates properly. When the interrupt is triggered, the alarm response should be the same.
///////////////////////// initialize & includes //////////////////////////
#include "SIM900.h"
#include "sms.h"
#include "Keypad.h"
#include <GSM.h>
SMSGSM sms;
boolean started = true;
int PIR_SensorPin = 2;
int Alarm_OutPin = 11; //green
int Light_OutPin = 12; //red
int alarm_count;
boolean volatile trigsensor = false;
const byte interruptPin = 2;
int z = 0;
int i = 0;
//////////////////////////// setup keyPad //////////////////////////
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] =
{
{
'1', '2', '3', 'A'
}
,
{
'4', '5', '6', 'B'
}
,
{
'7', '8', '9', 'C'
}
,
{
'*', '0', '#', 'D'
}
};
byte rowPins[ROWS] = {
6, 7, A2, A3 // row pin# on keypad to Arduino pin#, ie Row Pin #1 goes to Arduino Pin #6, etc.
}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {
3, A0, A1, A4
}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const char PIN[5] = {
'9', '6', '7', '9', '#'
}; // PIN number
char key_input[5] = {
0, 0, 0, 0, 0
}; // used for comparison
//////////////////////////////// setup ///////////////////////////////////
void setup() {
Serial.begin (9600); // Establish Serial connection.
pinMode (Alarm_OutPin, OUTPUT); // Set pinMode.
pinMode (Light_OutPin, OUTPUT); // Set pinMode.
pinMode (PIR_SensorPin, INPUT); // Set pinMode.
digitalWrite (Alarm_OutPin, LOW); // Set output pins to LOW for start.
digitalWrite (Light_OutPin, LOW); // Set output pins to LOW for start.
digitalWrite (PIR_SensorPin, LOW);
Serial.println ("GSM Shield testing."); // Serial message that GSM shield is starting up
if (gsm.begin(4800)) { // Set GSM shield to recommended 4800 baud rate.
Serial.println ("Status = READY");
digitalWrite (Alarm_OutPin, HIGH);
// PIR as interrupt
attachInterrupt (digitalPinToInterrupt (2), trigger, RISING);
//attachInterrupt ((0), trigger, RISING);
}
// started = false; //////////////////////////////////////////////////////////////
Serial.println ("System Ready, at the keypad loop!");
}
//////////////////////////////// void loop ///////////////////////////////////
void loop() {
readKeypad();
}
////////////////////////////// void trigger ///////////////////////////////////
void trigger() {
trigsensor = true;
started = true;
}
//////////////////////////////// void alarm ///////////////////////////////////
void alarm() {
Serial.println (started); Serial.println (trigsensor);
// digitalWrite (trigsensor, LOW);
Serial.println("at Alarm");
// while ((timedelay - time) > 0); Serial.println (time); time++;
if ((started == true) && (trigsensor == true)) {
// (sms.SendSMS("**********", "***Motion Detected in Studio!***"));
Serial.println ("MOTION detected: SMS Sent");
for (alarm_count = 0; alarm_count < 5; alarm_count++) { // Cycle outputs if triggered
digitalWrite (Alarm_OutPin, HIGH); delay (2000); // On/Off/On/Off/Off
digitalWrite (Alarm_OutPin, LOW);
digitalWrite (Light_OutPin, HIGH); delay (2000);
digitalWrite (Light_OutPin, LOW); // End by turning off light
}
digitalWrite (Alarm_OutPin, HIGH);
}
else { }
readKeypad();
}
//////////////////////////////// read Keypad ///////////////////////////////////
void readKeypad() {
char key = keypad.getKey();
if (key != NO_KEY) { // if a keypad input
key_input[z] = key;
Serial.print("Key_input["); Serial.print(z); Serial.print("] = "); Serial.println(key);
z++;
if (z >= sizeof(key_input)) {
z = 0;
}
switch (key) {
case '*': // resets key_inputs to "0"
z = 0;
break;
case '#': // pressed to "enter"
z = 0;
for ( i = 0; i < 4 ; i++ ) {
Serial.print(key_input[i]);
}
Serial.println ("-------");
delay(100); // for extra de-bounce
checkPIN();
break;
}
}
}
//////////////////////////////// check PIN ///////////////////////////////////
void checkPIN() {
Serial.println ("@ checkPIN ");
int correct = 0;
int i;
for ( i = 0; i < (sizeof(PIN) - 1) ; i++ ) {
if (key_input[i] == PIN[i]) {
correct++;
}
}
if (correct >= sizeof(PIN) - 1) {
Serial.print ("# correct = "); Serial.println (correct); Serial.println (sizeof(PIN) - 1);
correctPIN();
}
else {
incorrectPIN();
}
}
//////////////////////////////// correct PIN ///////////////////////////////////
void correctPIN() { // do this if correct PIN entered
Serial.println ("@ correct PIN ");
started = false;
digitalWrite (Alarm_OutPin, HIGH);
readKeypad();
}
///////////////////////////// incorrect PIN ///////////////////////////////////
void incorrectPIN() { // do this if incorrect PIN entered
Serial.println ("@ incorrect PIN ");
started = true;
// add time delay before alarm, via keypad??
alarm();
}