Hi all,
I've been trying to make a smart lamp that switches between a warm light, a purple light and an off state according to light levels and people in vicinity. However, the PIR sensor that I'm currently using gives off data in a weird way. Instead of giving a constant "1" when it detects something in its range, it drops to "0" for about a second before returning to "1". This makes it switch to the wrong light for a little and I want to fix this. I've tried regulating it by setting up an intterupt with a timer, so that only when the signal is "0" for more than 2 seconds, the other color lamp turns on.
Here is the code I'm using:
/macro definitions of PIR motion sensor pin and LED pin/
#define PIR_MOTION_SENSOR 2//Use pin 2 to receive the signal from the module
#include <ChainableLED.h>
int pin1=4,pin2=5,number_of_LED=1;
ChainableLED leds(4,5,1);
int pin3=2;
int TimeUpdateInterval = 2000;
int LastUpdateTime;
void setup()
{
Serial.begin(9600);
pinMode(PIR_MOTION_SENSOR, INPUT);
attachInterrupt(digitalPinToInterrupt(PIR_MOTION_SENSOR), PIR, FALLING);
leds.init();
}
void loop()
{
int light=analogRead(pin3);
int PIRstatus=digitalRead(PIR_MOTION_SENSOR);
Serial.print(PIRstatus);
Serial.print(" ");
Serial.println(LastUpdateTime);
if (light > 400 && PIRstatus == 1)
{
leds.setColorRGB(0,0,0,0);
}
if(light < 400 && PIRstatus == 1)
{
leds.setColorRGB(0,255,128,0);
}
if (millis() - LastUpdateTime > TimeUpdateInterval && light < 400 && PIRstatus == 0)
{
leds.setColorRGB(0,102,0,204);
}
}
void PIR() {
LastUpdateTime = millis();
}