Hi i am having problems getting a PIR sensor to work along side the DmxMaster library (http://www.tinkerkit.com/dmxmaster-library/#dmxmaster). When running the sketch with the lightsOn() and lightsOff() functions commented out the sensor calibrates the detects motion and then prints the end of motion, (in the serial monitor). However when i run the sketch with these functions active the sensor instantly detects movement and never detects when the movement has ended, almost as if it isn't running the
if(digitalRead(pirPin) == LOW){
}
Does anyone have any ideas as to why this is happening?
Thanks
Josh
#include <DmxMaster.h>
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3; //the digital pin connected to the PIR sensor's output
/////////////////////////////
//SETUP
void setup(){
pinMode(13, OUTPUT);
Serial.begin(9600);
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
if(digitalRead(pirPin) == HIGH){
lightsOn();
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
lightsOff();
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
void lightsOn(){
DmxMaster.write(1,255);
DmxMaster.write(4,255);
}
void lightsOff(){
DmxMaster.write(1,0);
DmxMaster.write(4,0);
}