Hi,
I am using a pir (3 pin PIR) in my project and somehow the PIR is not responding. It doesn't detect motion. I tried the PIR with a simple code that turns an LED when motion is detected and it worked fine on the same pin i am using (i.e. Pin 9) however when I upload the code below the pir doesn't work. I hope someone could help me figure out the problem. I think the issue is purely software issue.
Code:
#include <Keypad.h>
const byte ROWS = 2;
const byte COLS = 2;
char keys[ROWS][COLS] =
{
{
'1','3' }
,
{
'7','9' }
,
};
byte rowPins [ROWS] = {
5,6};
byte colPins [COLS] = {
7,8};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int pirPin = 9;
int dataPin1 = 13; //Name Pin. Define which pins will be used for the Shift Register control
int dataPin2 = 10; //Off Pin
int dataPin3 = 11; //CVN Pin
int dataPin4 = 12; //Vacation Pin
int dataPin5 = 2; //Here Pin
int latchPin = 3;
int clockPin = 4;
int val = 0;
int seq[14] = {
1,2,4,8,16,32,64,128,64,32,16,8,4,2}; //The byte sequence
void setup()
{
Serial.begin (9600);
pinMode(dataPin1, OUTPUT);
pinMode(dataPin2, OUTPUT);
pinMode(dataPin3, OUTPUT);
pinMode(dataPin4, OUTPUT);
pinMode(dataPin5, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(pirPin, INPUT);
}
void handleKeypress (const char key)
{
Serial.println (key);
}
void loop()
{
for (int n = 0; n < 14; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin1, clockPin, MSBFIRST, seq[n]); //Send the data
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(20);
}
byte key = kpd.getKey();
if (key)
handleKeypress (key);
val = digitalRead(pirPin);
switch (key) {
case '1':
// Serial.println("entering loop");
while(HIGH == val)
{
for (int n = 0; n < 14; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin1, clockPin, MSBFIRST, seq[n]); //Send the data
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(20);
}
for (int n = 0; n < 14; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin2, clockPin, MSBFIRST, seq[n]); //Send the data
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(50);
}
}
//Serial.println("exiting loop");
break;
Serial.println("after break");
case '3':
while(HIGH == val)
{
for (int n = 0; n < 14; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin1, clockPin, MSBFIRST, seq[n]); //Send the data
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(20);
}
for (int n = 0; n < 14; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin3, clockPin, MSBFIRST, seq[n]); //Send the data
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(50);
}
}
break;
case '7':
while(HIGH == val)
{
for (int n = 0; n < 14; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin1, clockPin, MSBFIRST, seq[n]); //Send the data
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(20);
}
for (int n = 0; n < 14; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin4, clockPin, MSBFIRST, seq[n]); //Send the data
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(50);
}
}
break;
case '9':
while(HIGH == val)
{
for (int n = 0; n < 14; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin1, clockPin, MSBFIRST, seq[n]); //Send the data
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(20);
}
for (int n = 0; n < 14; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin5, clockPin, MSBFIRST, seq[n]); //Send the data
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(50);
}
}
break;
}
}
Here is the code that I am using for the PIR you can find it scattered in the code above. I just copied it here below so you see what I am doing:
Code:
int pirPin = 9;
int val = 0;
pinMode(pirPin, INPUT);
val = digitalRead(pirPin);
while(HIGH == val)
Thank you very much
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.