Fade LED project

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 on

void 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)

What is your question? Or do you expect someone to write your code for you? :slight_smile:

Well, my question was how to use the millis timers? As what I've done thus far doesn't work. So yes any coding help would be great

Couple of problems I can see right off the bat:

You don't ever set perviousMilliseconds apart from when you initialise it to zero so your timing logic won't work.

Statements like:

analogRead(LedStripPin) == 250

are unlikely to ever return exactly 250 as analog inputs tend to bounce around a bit. Check for a range of values around the value you are looking for.

It looks like you are timing a couple of different things - when to read the sensor, when to turn the lights off. You will need to keep track of when you last did each of them in separate variables.

I think you will need to keep track of another timer for the dimming of your LEDs, otherwise they will just come on and go off too quickly for you to notice. It looks like you had a delay() in there but that is commented out, which is good (i.e. don't use delay).

ok, thanks for having a look, i'll have another crack at it and probably be back lol

In the end, I got rid of the light sensor because it was complicating the situation!
ended up with this code which seems to work

/* This sketch is to Fade on an LED strip with PIR and Light sensor
as inputs
*/

//set some pins
int PirPin = 4; //The PIR sensor intput
int LedPin = 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
long previousMillis = 0; // store the last time it was turned on
int ledOff = 0; // PWM LED off value

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 900000; // interval to turn off the LED's 15mins (milliseconds)

void 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 timer
unsigned long currentMillis = millis();

//IF statement to activate LEDS if conditions are met (Is the PIR active)
if (digitalRead(PirPin) == HIGH)
{

// set the brightness of LED pin
analogWrite(LedPin, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
}

if (currentMillis - previousMillis > interval) {

// save the last time you turned on the the LED's
previousMillis = currentMillis;

if(analogRead(LedPin) >= 200) {

analogWrite(LedPin, ledOff) ;
}

}

// limit range of sensor values to between 10 and 255
brightness = constrain(brightness, 0, 250);

}