0

I have tried changing pinModes and I/O declarations but still getting erratic or unexpected pin readings (HIGH), even though, I think, that I have set them LOW during setup. I read elsewhere that this is not uncommon, but I have not found a similar situation or a suitable explanation/solution. I can only believe that it is a basic misunderstanding (still learning). Thanks for any support. By the way, I had deleted the use of the keypad for trial purposes. My code is below:

//////////////////////////// Initialize & Includes //////////////////////////
#include "SIM900.h"
#include "sms.h"
#include "Keypad.h"
#include <GSM.h>
SMSGSM sms;
boolean pir_SensorPin = 2;
int led_OutPin = 11; // green
int alarm_OutPin = 12; // red
int alarm_count = 0;
boolean switchPin = 4; // alarm on test
boolean keypadPIN;
int z = 0;
int i = 0;
int waitTime = 5000;

//////////////////////////// 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 (switchPin, INPUT);       // Set pinMode.
  pinMode (led_OutPin, OUTPUT);     // Set pinMode.
  pinMode (alarm_OutPin, OUTPUT);   // Set pinMode.
  pinMode (pir_SensorPin, INPUT);   // Set pinMode.
  digitalWrite (led_OutPin, LOW);   // Set output pins to LOW for start.
  digitalWrite (alarm_OutPin, LOW); // Set output pins to LOW for start.
  digitalWrite (switchPin, LOW);
  digitalWrite (pir_SensorPin, LOW);
  Serial.println ("Studio Alarm starting up."); // Serial message that GSM shield is starting up
  if (gsm.begin(4800)) {    // Set GSM shield to recommended 4800 baud rate.
    Serial.println ("Status = Network READY");
    digitalWrite (led_OutPin, HIGH);
  }
  //  attachInterrupt (digitalPinToInterrupt (2), trigger, CHANGE); // PIR as interrupt
  Serial.println ("System Ready");

}

//////////////////////////// State Setup //////////////////////////
enum STATE {
  armed, // system acknowledges trigger
  unarmed, // system unarmed via keypad entry setting keypadPIN = LOW
  justActivated, // PIR-triggered
  alarm // confirm system unarmed then goto unarmed state else start alarm
};

STATE systemState = unarmed;
long timeGettingArmed;

/////////////////////////// Trigger ///////////////////////////////
//void trigger() {
//  systemState = alarm;

//////////////////////////// State Machine //////////////////////////
void loop() {
  check(); Serial.print ("switchPin = "); Serial.println (switchPin);
  Serial.print ("pir_SensorPin ="); Serial.println (pir_SensorPin);
  switch (systemState) {
    case armed:
      Serial.println ("armed");
      if (digitalRead (pir_SensorPin == HIGH)) {
        systemState = alarm;
      } else if (digitalRead (keypadPIN == LOW)) {
        systemState = unarmed;
      }
      break;
    case unarmed:
      if (digitalRead (pir_SensorPin == HIGH)) {
        Serial.println ("unarmed");
        timeGettingArmed = millis(); // Or just do a delay here.
        systemState = justActivated;
      }
      break;
    case justActivated:
      Serial.println ("juatActivated");
      if (timeGettingArmed + waitTime < millis()) {
        systemState = armed;
      }
      break;
    case alarm:
      Serial.println ("alarm");
      if (digitalRead (keypadPIN == LOW)) {
        systemState = unarmed;
      } else {
        beep();
      }
      break;
  }
}

//////////////////////////// Alarm //////////////////////////
void beep() {
  Serial.println("at beep");
  // (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 (1000); // On/Off/On/Off/Off
    digitalWrite (alarm_OutPin, LOW); delay (500);
    digitalWrite (led_OutPin, HIGH); // delay (1000);
    Serial.println (alarm_count);
  }
  systemState = armed;
}

void check() {
  if (digitalRead (switchPin == HIGH)) { // manual test in lieu of keypad attached
    (keypadPIN = LOW); // unarmed
  }
  else {
    (keypadPIN = HIGH); // armed
  }
  Serial.print ("switchPin = "); Serial.println (switchPin);
  Serial.print ("keypadPIN = "); Serial.println (keypadPIN);
}

Serial output shows:
pir_SensorPin =1
switchPin = 1
keypadPIN = 1
switchPin = 1
3

Your code to read from the pins is incorrect. You write

if (digitalRead (pir_SensorPin == HIGH)) {

However, this compares pir_SensorPin == HIGH, (which is 2 == HIGH), producing a true or false that it then passes as the argument to digitalRead, which will try to interpret that as a pin number. Fix your parentheses:

if (digitalRead(pir_SensorPin) == HIGH)  {

(This problem occurs throughout your code.)

You also are writing an input pin in your setup, which is not necessarily wrong (it is one way of enabling or disabling the pullup resistor), but you should understand why you're doing it:

pinMode (pir_SensorPin, INPUT);   // Set pinMode.
...
digitalWrite (pir_SensorPin, LOW);
  • 3
    The effect of writing to an input pin enables or disables (disables in this case) the internal pullup resistor. In earlier versions of the Arduino API there was no INPUT_PULLUP mode, so writing to the input pin was common. I suppose there are still plenty of examples and tutorials that still tell you to do that. – Majenko Apr 23 '17 at 11:50
  • Thanks to you both. I am in the process of implementing its changes and my parentheses issues. – nkuck Apr 23 '17 at 12:07
  • Majenko, thanks for the useful explanation. I really do need to finish reading that whole data sheet and then the library source.... – Curt J. Sampson Apr 23 '17 at 15:23
  • Still not able to get a functioning operation, as I expect. I have since grounded my two input pins, with no success. I am getting the following outputs:switchPin = 1 PIRtriggerCheck = 0 switchPin = 1 1 The PIRtriggerCheck doesn't seem to get triggered. Code follows: – nkuck Apr 29 '17 at 7:00
  • Write the simplest possible program that sets up two pins as input pins and then, in a loop, reads them and prints out their values. Tie one to 5V and the other to ground, and ensure you're getting the right readouts. If it doesn't work, edit the question to include the simpler code that's not working. If it does work, gradually add bits to the program to bring it closer to the final version, testing after each small change. – Curt J. Sampson Apr 29 '17 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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