Constrain not ............Constraining ?!

Hi, I'm making a very simple LED project that uses PWM to fade on some LEDs when a PIR sensor is activated.

Aso this is combined with input from a light sensor so they only come on when its dark!

I haven't been coding that long so sorry about the mess of code! lol

The only way could think to fade the PWM output was with the classic

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

but when the LEDS are on it will just continue to increment every time through the loop......

So I tried to constrain the brightness variable, but when I look at this value in serial its just incrementing way beyond the 255 max PWM value....

it just does it until the PIR sensor input drops off and the IF statement is no longer true...

any advice would be good.

onis

LED_FADE_PROJECT.ino (1.32 KB)

The constrain function returns the constrained value. It is pointless to just throw that away.

OP's code:

/* 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.

ok thanks for the input but still have my code problem lol ....

So you changed your code? Please post it here again (don't edit your start post) in code tags :slight_smile:

no, the code is above! as in the last two posts didn't suggest how to fix it!

Hi,

and see how it is formatted.

Tom.... :slight_smile:

onisuk:
didn't suggest how to fix it!

But PaulS did...

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

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!

GypsumFantastic thanks i'll try that !

Yes that worked many thanks!

Reply #6 wasn't cryptic
:o