Am I using Constrain function correctly ?

I'm controlling a PWM output for LED floodlights, from two pushbuttons on a RemoteXY (iPhone, Bluetooth) control system.

One button increases the light level, the other decreases.

  if (RemoteXY.L_Inc) {
    lights++;
    delay(20);
  }
 
 if (RemoteXY.L_Dec) {
    lights--;
    delay(20);
  }

  constrain(lights, 0, 100);

Here is the code to drive the output, which needs to be inverted because of the MOSFET driver I'm using.

// PWM Control (inverted, due to MOSFET drive)  
  analogWrite(pin_Lights, map(lights, 100, 0, PWM_Min, PWM_Max));

PWM_Min and PWM_Max are defined variables, currently set a 0 and 255 respectively.

What I am actually getting, if I press and hold the L_Inc button, it the lights fade up from zero to max, then turn off abruptly , then fade up again, this continues as long as I hold the L_Inc button. continuously.... it is as though constrain is not capping the lights variable at 100, but resetting it to 0, which is not how it is described in the reference.....

Has anyone else seen this behaviour, or can shed light on what I am doing wrong ?

You are throwing away the result of the constrain function.

vaj4088:
You are throwing away the result of the constrain function.

If your answer was meant to be cryptic, it got me thinking....

dammit !!!

lights = constrain(lights, 0, 100);

You did the correct thing.

Some people need to have their hand held, some people just need to think. You did well. Hurray!