1

Topic: LED Fading

Hi everybody,

with a control button, I would like to dim an LED:

This works without Problems:

void loop()
{
  for (int helligkeit =1; helligkeit <= 255; helligkeit = helligkeit +2)
  {
    analogWrite(ledPin, helligkeit);
    delay(10);
  }

But, as soon as I initiate this routine by a Remotexy button, it seems, a loop is processed (LED is dimmed down to 0, then starts again from the beginning). Does somebody has any idea?

Thanks!!!!

2

Re: LED Fading

Well the code is in a loop, so it will continue looping....

the "for" statement starts helligkeit  at value 1, then that is written to the ledPin.

A small delay is executed, then helligkeit is incremented by 2, and then the new value is written to the PWM pin.

This repeats itself until helligkeit increments beyond 255.

Then the code repeats itself, all over again.

If you want to control the brightness of your LED with RemoteXY, there's a couple of ways you could do it.


1. Easiest way is to use a Slider control. These send a value of 0..100 to the controller, which you can then map to the 0..255 PWM pin with :-

analogWrite(ledPin, (map(slider_value, 0, 100, 0, 255)));

2. Use buttons to "bump" the ledPin value Up and Down by a certain amount (your choice) each time the buttons are pressed.  e.g.

if (RemoteXY.buttonUp) {
 RemoteXY.buttonUp = 0;
 ledPin_value = ledPin_value + 5;
 delay(100);
}

do the same for .buttonDown ( ledPin_value -5 ! )

then check that ledPin_value is within limits for the PWM ( 0-255 );

constrain(ledPin_value, 0, 255);

then write the value to the PWM pin.

analogWrite(ledPin, ledPin_value);

You'll notice that I immediately write the button value back to zero as soon as I "see" it. This is to stop the button being acted on repeatedly until the RemoteXY interface resets it back to zero. Without it, you'll almost certainly get "overshoot", unless that delay(100) is much, much larger.

Have fun....

2B, or not 2B, that is the pencil ...