mercredi 26 juin 2019

Stop variables from rolling over across a range of values (Arduino C++ and joystick)

I am PWM-controlling two CC-PSUs that control one LED each (one warm CCT, one cold CCT) in a range from 16 to 240, using a joystick as user interface. When the light comes on or is switched off and then on again, the default PWM value for both is 128. The PWM generator is an Adafruit Metro Mini (Arduino), but it's rather a code logic issue here.

What works with the code below is to blend between warm (left) and cold (right) or between bright (up) and dark (down), if the joystick is tilted strictly left/right (x) or strictly up/down (y). In those two cases, the PWM values don't roll over as per the if-statements, even if the joystick is kept tilted. It is acting like a "+"-shifter, if it were mechanically constrained. So far, so good.

However, if one has, for example, tilted the joystick left for slightly warmer light (PWM 176/80) and then wants it brighter from there and tilts the joystick up (and keeps it tilted), the PWM values do not "stop" at 240/144 (they should). The same goes for any other quadrant.

How would I have to enhance/change the if-statements to "stop" the PWM values from incrementing/decrementing as desired?

I hope the PWM value matrix below visualises the problem better than I can explain it verbally.

void changeBrightness()
{
  if (lightOn)
  {
    readJoystick();
    if (joyX < triggerLow) // Joystick tilted left
    {
      if ((leftPWM < 240) || (rightPWM > 16))
      {
        leftPWM += deltaPWM;
        rightPWM -= deltaPWM;
        setPWM(leftPWM, rightPWM);
      }
    }
    if (joyX > triggerHigh) // Joystick tilted right
    {
      if ((leftPWM > 16) || (rightPWM < 240))
      {
        leftPWM -= deltaPWM;
        rightPWM += deltaPWM;
        setPWM(leftPWM, rightPWM);
      }
    }
    if (joyY > triggerHigh) // Joystick tilted up
    {
      if ((leftPWM < 240) || (rightPWM < 240))
      {

        leftPWM += deltaPWM;
        rightPWM += deltaPWM;
        setPWM(leftPWM, rightPWM);
      }
    }
    if (joyY < triggerLow) // Joystick tilted down
    {
      if ((leftPWM > 16) || (rightPWM > 16))
      {
        leftPWM -= deltaPWM;
        rightPWM -= deltaPWM;
        setPWM(leftPWM, rightPWM);
      }
    }
  }
  else
  {
    setPWM(0, 0); // Turn the light off
  }
}

PWM value matrix

Aucun commentaire:

Enregistrer un commentaire