Changing (PWM-) values while button press

Hi everyone!

I did some testing on my Arduino beforehand but it seems I can't get it to work properly; and therefore this thread is made :slight_smile:

First to the program itself. I was trying to program a function which randomly fades 4 LEDs at different durations and different intensities but I ended up in having just some kind of semi-randomness. Gladly I found this library LEDfader which did exactly was I was trying to achieve:

Like mentioned this program creates random numbers for the durations/intensities of the LEDs. In my case the duration is between 500 and 1200ms and the intensity moves between 100 and 180. Okay. Fine!

The only problem I have been encountering was a function that increases the random intensity-value's as long as a button is pressed. It should get increased from 100/180 to 180/255.
I managed to have the values stored in variables (applause) which get called in the random()-function but I simply cannot get the increased variables to work as long the button is pressed.

I'm not to 100% sure where to put the check at (before the for-loop I guess?!) and where there should be another function which reads when the button is released. I tried different approaches but neither one of them worked!
The program shouldn't be stopped either when any action is performed so I'm not sure if that's even possible with that kind of program (for-loop).

Can you guys help me out on that one? It's driving me nuts! :frowning:

Cheers and thanks so much in advance,
Mario

Can you guys help me out on that one?

Without seeing your code? Not much chance of that.

Hey Paul, thanks for checking in!
The program I use is exactly the one mentioned in the link as example with 6 LEDs! :slight_smile:

#include <LEDFader.h>
 
#define LED_NUM 6
 
// 6 LEDs (perhaps 2 RGB LEDs)
LEDFader leds[LED_NUM] = {
  LEDFader(3),
  LEDFader(5),
  LEDFader(6),
  LEDFader(9),
  LEDFader(10),
  LEDFader(11)
};
 
void setup() {
}
 
void loop() {
 
  // Update all LEDs and start new fades if any are done
  for (byte i = 0; i < LED_NUM; i++) {
    LEDFader *led = &leds[i];
    led->update();
 
    // This LED is not fading, start a new fade
    if (led->is_fading() == false) {
      int duration = random(1000, 3000); // between 1 - 3 seconds
 
      // Fade Up
      if (led->get_value() == 0) {
        byte intensity = random(100, 255);
        led.fade(intensity, duration);
      }
      // Fade Down
      else {
        led.fade(0, duration);
      }
    }
  }
}

Here, this one! :slight_smile:

NEVERMIND! I got it working now!
I used this library here: ClickButton.h

Works like a charm! :slight_smile: