Hi, I've spent days on this and I have to admit it's over my head now lol and I need some help!
I have two sensors, a PIR motion sensor and a light sensor to turn on a LED strip. I wanted to fade the LEDs on when its dark and there is movement detected.
I was trying to use millis to record the light sensor value so it only changed every second or so and wanted to turn off the leds if no motion is detected for x number of minutes.
Here's what i have so far..
/* This sketch is to Fade on an LED strip with PIR and Light sensor
as inputs
*///set some pins
int LightSensor = A0; // Pin for the Light Sensor
int PirPin = 4; //The PIR sensor intput
int LedStripPin = 9; // The PWM output to control MOSFET// set some variables etc
int brightness = 0; // how bright the LED strip pin is
int fadeAmount = 5; // how many points to fade the LEDs by
int LightSensorValue = 0; // The stored value read from the Light sensor
unsigned long perviousMillis = 0; //will store the timer
long offTime = 6000; //timer for how long the Leds will be kept on
long LightSensorRead = 2000; //timer for interval between Light sensor reads
int LedsOff = 0; // off interger//set a timer interger to ajdust the fade
//int Timer = 40; //Timer value to fade effect the LEDs onvoid setup()
{// set output pins as pins are input by default
pinMode(9, OUTPUT);//start debug serial coms
//Serial.begin(9600);}
void loop()
{
//Serial debug
//Serial.println(LightSensorValue);// Set the timer!
unsigned long currentMillis = millis();// read the value from the Light sensor and asign to LightPinValue update every 5 seconds
if (currentMillis - perviousMillis >= LightSensorRead)
{
LightSensorValue = analogRead(LightSensor);}
// Turn off leds??
if ((analogRead(LedStripPin) == 250) && (currentMillis - perviousMillis >= offTime))
{
analogWrite(LedStripPin, LedsOff);
}//IF statement to activate LEDS if conditions are met (Is the PIR active and is it dark?)
if (digitalRead(PirPin) == HIGH && (LightSensorValue) >= 900) {// set the brightness of LED pin
analogWrite(LedStripPin, brightness);// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;// wait for a few milliseconds to see the dimming effect...
//delay(Timer);}
// limit range of sensor values to between 10 and 255
brightness = constrain(brightness, 0, 250);}
thanks for any advice onis
LED_FADE_PROJECT.ino (2.03 KB)