Clouds LED PWM Implementation in

Hi all,

I’m wondering if someone could help me figure out what this extra reference to the red_ and green_ arrays are for in the Cloud’s driver? I understand how this code is being used to implement a PWM signal… but these two lines are kind of stumping me!

If I break down lines 71 or 74… here is what I’m thinking:

The red_[i] >= pwm_counter_ operation is evaluated first because of operator precedence, and then we && the result with this extra red_[i] term. But… I feel like I’m not fully seeing the need for the extra red_[i] term.

Is it only useful when red_[i] or green_[i] = 0 and pwm_counter_ = 0? I can see how the right side of the expression would evaluate to 1 in this situation and the extra red_[i] or green_[i] helps keep the term at 0.

Is there any other use for the additional red_[i] or green_[i] term on the left or is it just used in the red/green = 0, pwmcounter = 0 scenario?

If it was simply:

if (red_[i] >= pwm_counter_)

Then the LED wouldn’t be entirely off with a value of 0 in red_[i]. It would be lit with a duty cycle of 1/8.

If it was simply:

if (red_[i] > pwm_counter_)

Then the LED wouldn’t be entirely on with a value of 255 in red_[i]. It would be lit with a duty cycle of 7/8.

if (red_[i] && red_[i] >= pwm_counter_) solves both problems: lit 100% of the time when red_[i] is set to 255, and completely off when red_[i] is set to 0. Given that there are many situations in which the brightness is either 0 and 255 (when navigating modes/blend functions for example), I think it’s important to get these corner cases right!

I know it’s like 6 months later… but I just wanted to thank you for taking the time to explain the code to me!

I finally got around to using some of this code on my Mutable-inspired stompbox. Mostly working with the UI stuff now… Thus far it’s kind of a mish mash of code from Clouds and Plaits that I’ve been re-working for my hardware and the LL peripheral drivers. I messed with set_intensity to make the 4 LEDs blend from Green to Red depending on the position of the pots.

  void set_intensity(uint8_t channel, uint8_t value) {
uint8_t red = 0;
uint8_t green = 0;
if (value < 128) {
  green = 255;
  red = value << 1;
} else {
  green = 255 - ((value - 128) << 1);
  red = 255;
set_status(channel, red, green);


I’m having so much fun playing with your code and I’m learning so much! Thank you so much for all you do!

Not so great video of my implementation on hardware:

View this post on Instagram

A post shared by Kinotone Audio (@kinotoneaudio)

1 Like