0

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();
}

1 Answer 1

2

None of the code you posted ever sets trigsensor to false, besides the initial value.

10
  • Thanks, you are right. But I'm not sure where it should be placed, and why??
    – nkuck
    Commented Feb 15, 2017 at 22:28
  • You are correctly doing little in the ISR: set a flag telling the main program to handle something. You should unset that flag when you are done doing handling the something you needed to handle. Guessing from your code: in the alarm() function. Commented Feb 15, 2017 at 22:55
  • It seems as if the interrupt on pin 2 is not triggering the function. Not sure what is wrong.
    – nkuck
    Commented Feb 16, 2017 at 15:19
  • I did make the change in the alarm function, but still not getting the interrupt to trigger. } trigsensor = false; readKeypad(); }
    – nkuck
    Commented Feb 16, 2017 at 15:24
  • What does the signal on the interrupt pin look like? You initialize it to trigger on a rising edge -- is that what is happening with the actual signal? Commented Feb 16, 2017 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.