/* This sketch is to Fade on an LED strip with PIR and Light sensor
as inputs
*/
//set some pins
int LightPin = A0; // Pin for the Light Sensor
int PIR_Pin = 4; //The PIR sensor intput
int LedPin = 5; // The PWM output to control MOSFET
// set some variables
int brightness = 0; // how bright the LED pin is
int fadeAmount = 5; // how many points to fade the LED by
int LightPinValue = 0; // The stored value from the Light sensor
//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(5, OUTPUT);
//start debug serial coms
Serial.begin(9600);
}
void loop()
{
// limit range of sensor values to between 10 and 255
constrain(brightness, 0, 255);
// read the value from the Light sensor and asign to LightPinValue
LightPinValue = analogRead(LightPin);
//IF statement to activate LEDS if conditions are met
if (digitalRead(PIR_Pin) == HIGH && (LightPinValue) >= 1005) {
// set the brightness of LED pin 13:
analogWrite(LedPin, LightPinValue);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
//debug
Serial.println(brightness);
}
// wait for a few milliseconds to see the dimming effect...
delay(Timer);
}
ps, in the future, please post your code like I did above, unless it is greater than about 9000 characters. This makes it easier for everyone to see. If someone tries to download and open your .ino file, the IDE wants to create a folder for it, which may be too much for some, so they will just skip looking at it.
GypsumFantastic:
Reply #1 had the answer, but in slightly terse language. I can see why you missed it.
You misunderstand how constrain works. It returns the constrained value. It doesn't modify the variable you put into it, unless you make it.
Try changing the line
constrain(brightness, 0, 255);
to this
brightness = constrain(brightness, 0, 255);
Yes, I knew that the answer was in the first post! but if I'm here not even using the constrain function properly, surely that points to the fact I don't quite know what I'm doing.... so an almost cryptic answer is of no help!