Any hints how I can make the power button make the led full brightness

Does McWhorther publish his solutions?

I'd like to see what he expects you to be able to do at this point, I'm betting it is done in a very simple manner.

Later.

McWhorther seems to use neither switch/case, the ternary operator nor functions at this point.

He does not publish, or TBC I could not find solutions to homework problems. But looking ahead to the ultimate sketch he expects you will arrive at, we see indeed he continues in his straight code all in the loop style. Appropriate for where he's led you.

He uses a global variable

float dFact = 1; // dimming factor

And using the way he links any IR button to an action, handles dimming (and undimming) by tweaking and constraining the brightness factor, viz:

  if (myCom == "dn") {
    dFact = dFact * 0.75;  // dim
  }

  if (myCom == "up") {
    dFact = dFact * 1.30;  // brighten
     if (dFact > 1) {
      dFact = 1;  //... fix overflow
     }
  } 

I expect you could fit that into your switch statement.

Later, at the point of writing the values, the brightness factor is applied:

  analogWrite(rPin, rBright * dFact);
  analogWrite(gPin, gBright * dFact);
  analogWrite(bPin, bBright * dFact);

I guess he isn't terribly worried about the physiology of human vision WRT PWM modulated RGB LEDs.

Everyone learns differently: I think it is useful to see things done literally as is McW's habit, and perhaps suffer a bit while writing code that way.

At the very least it makes the actual necessary steps of the algorithm the issue, rather than any fancy languages features you dress them up in.

a7

THat is correct

HE never used a switch statement but a lot of if then's .
I do not like it and have learned some more c++ with exercism.io solutions.

you find my solution complex ??

So far it’s very readable

oke

I get the feeling that Alto777 finds my solution complex

Still looking how I can decrease or increase the brightness and how I can convert to RGB what the arduino seems to need

IM still confused

So red will be 0, 100, 50

But how do I now increase or decrease the brightness ?

oke, looks like I have to increase the last number between 50 and 100

Yes the last value will drive brightness. Not sure why you say only from 50 though.

Only if you are using GRB... and it will be closer to purple (or if using BRG, orange)
See post 28 for HSV...

Increase the color variables.

0+1, 100+1, 50+1
I have a running Neopixel only using RGB values, and it gives my eyes all 16 million combinations, smoothly. I will dig-up the code.

He was taking HSB color space

Color H S B
Red 0 100 100
Orange 30 100 100
Yellow 60 100 100
Green 120 100 100
Cyan 180 100 100
Blue 240 100 100
Indigo 275 100 100
Violet 300 100 100
Magenta 330 100 100
White 0 0 100
Black 0 0 0
Gray 0 0 50
Pink 350 50 100
Brown 30 100 60

In HSB the H (hue) value represents the color’s position on the color wheel from 0 to 360 degrees, S (saturation) is how pure the color is from 0 to 100%, and B (value/brightness) is how bright the color is from 0 to 100%.

@J-M-L im so far right now

but still confused how I can make that the power button the brightness stays always the same so at 100

I did not find your solution complex.

a7

What is this simulation in Post #29 to do? I pressed Power, FF, RW, play, return, test and 1 - 9 without any effect.

That is correct.

Post #29 is a attempt where work on to make things work with hsl colors.

I still have to figure out how I can convert hsl colors to rgb and how to make a fade between color1 and color2.

Working version with rgb you can find it in post #16
and on this url : https://wokwi.com/projects/454673983435304961

so if anybody have hints how to convert the hsl colors to rgb or make the fade im happy to listen.

it's a classical function - say you have Hue in 0–360 and Saturation and Value are 0–100 and you want to return RGB 0–255:


struct RGB {
  uint8_t r;
  uint8_t g;
  uint8_t b;
};

RGB hsvToRgb(float h, float s, float v) {
  s /= 100.0f;
  v /= 100.0f;

  float r = 0, g = 0, b = 0;

  if (s == 0) {
    r = g = b = v;
  } else {
    h = fmod(h, 360.0f);
    if (h < 0) h += 360.0f;
    float hf = h / 60.0f;
    int i = (int)floor(hf);
    float f = hf - i;
    float p = v * (1.0f - s);
    float q = v * (1.0f - s * f);
    float t = v * (1.0f - s * (1.0f - f));

    switch (i) {
      case 0: r = v; g = t; b = p; break;
      case 1: r = q; g = v; b = p; break;
      case 2: r = p; g = v; b = t; break;
      case 3: r = p; g = q; b = v; break;
      case 4: r = t; g = p; b = v; break;
      case 5: default: r = v; g = p; b = q; break;
    }
  }

  RGB color;
  color.r = (uint8_t)round(r * 255.0f);
  color.g = (uint8_t)round(g * 255.0f);
  color.b = (uint8_t)round(b * 255.0f);
  return color;
}

thanks

oke,

That works as far as I can tell.

Next the fading from one color to another if you choose another color

in the HSB model you can decide of a fixed path which is "going around" the color wheel by incrementing or decrementing H (of course stay within 0-360 as you cross boundaries)

you don't touch S and B as you go around the wheel.

oke

And I think it would be nice that always the shortest path is chosen.
Still some thinking to do.

You can compute the shortest direction by normalizing the difference.

int shortestHueStep(float startH, float endH) {
  float diff = endH - startH;

  if (diff > 180.0f) diff -= 360.0;
  else if (diff < -180.0f) diff += 360.0;

  return (diff > 0) ? 1 : (diff < 0) ? -1 : 0;
}

It works by checking if the raw difference is outside the ±180° range and adjusting accordingly. The return value is 1 for clockwise, -1 for counterclockwise, and 0 if the hues are equal so you can use that value as the increment (make sure you stay in the 0-360° limits) for each step you take.

I find printing formatted values help me "see/quantify" the patterns I programmed versus the patterns I want. [edit]In this sim... trim() demonstrates the numbers, fade() demonstrates timing.

@J-M-L

I have a bug somewhere with the fade efffect.
Or I get a fade color or no color at all.

Hopefully I can find it tomorrow or do I have a idea where I went wrong

You should print the raw numbers you are using and changing. Your "fade" works, but slowly, and "by 1s". Use the PWR, <<, >>, 1, 2, and 3 and watch your serial monitor.

same code with numbers

#include <IRremote.hpp>

const byte irReceivePin = 2;

const byte rPin = 9;
const byte gPin = 6;
const byte bPin = 5;

byte brightness = 255;

byte r, g, b;
byte h, s, l;

struct RGB {
  uint8_t r;
  uint8_t g;
  uint8_t b;
};

float currentH = 0, currentS = 0, currentL = 0;
float targetH  = 0, targetS  = 0, targetL  = 0;

const int fadeSteps = 100;   // smoothness
const int fadeDelay = 10;    // speed (ms)



int shortestHueStep(float startH, float endH) {
  float diff = endH - startH;

  if (diff > 180.0f) diff -= 360.0;
  else if (diff < -180.0f) diff += 360.0;

  return (diff > 0) ? 1 : (diff < 0) ? -1 : 0;
}


void fadeToTarget() {
  float lStep = (targetL - currentL) / fadeSteps;

  for (int i = 0; i < fadeSteps; i++) {

    currentL += lStep;

    RGB color = hsvToRgb(currentH, currentS, currentL);

    int oldcolorr, oldcolorg, oldcolorb;
    if (oldcolorr != color.r || oldcolorg != color.g || oldcolorb != color.b) {
      Serial.print("r ");
      Serial.print(color.r);
      Serial.print(" g ");
      Serial.print(color.g);
      Serial.print(" b ");
      Serial.println(color.b);
      oldcolorr = color.r;
      oldcolorg = color.g;
      oldcolorb = color.b;
    }

    analogWrite(rPin, color.r);
    analogWrite(gPin, color.g);
    analogWrite(bPin, color.b);

    delay(fadeDelay);

  }

  currentH = targetH;
  currentS = targetS;
  currentL = targetL;
}

RGB hsvToRgb(float h, float s, float v) {
  s /= 100.0f;
  v /= 100.0f;

  float r = 0, g = 0, b = 0;

  if (s == 0) {
    r = g = b = v;
  } else {
    h = fmod(h, 360.0f);
    if (h < 0) h += 360.0f;
    float hf = h / 60.0f;
    int i = (int)floor(hf);
    float f = hf - i;
    float p = v * (1.0f - s);
    float q = v * (1.0f - s * f);
    float t = v * (1.0f - s * (1.0f - f));

    switch (i) {
      case 0: r = v; g = t; b = p; break;
      case 1: r = q; g = v; b = p; break;
      case 2: r = p; g = v; b = t; break;
      case 3: r = p; g = q; b = v; break;
      case 4: r = t; g = p; b = v; break;
    case 5: default: r = v; g = p; b = q; break;
    }
  }

  RGB color;
  color.r = (uint8_t)round(r * 255.0f);
  color.g = (uint8_t)round(g * 255.0f);
  color.b = (uint8_t)round(b * 255.0f);
  return color;
}



void handleCommand() {
  if (IrReceiver.decode()) {
    switch (IrReceiver.decodedIRData.decodedRawData) {
      case 0x5DA2FF00 : // POWER for full bright white
        targetH = 0 ;
        targetS = 0 ;
        targetL = 100;
        break;

      case 0x3DC2FF00 : // undo for all lights off
        l = 0 ;
        break;

      case 0xCF30FF00: // red
        targetH = 0;
        targetS = 100;
        targetL = 100;
        break;

      case 0xE718FF00: // green
        targetH = 120;
        targetS = 100;
        targetL = 100;
        break;

      case 0x857AFF00: // blue
        targetH = 240;
        targetS = 100;
        targetL = 100;
        break;




      case 0X1FE0FF00: // brightness down
        targetL = max(50, targetL - 1);
        break;

      case 0x6F90FF00: // brightness up
        targetL = min(100, targetL + 1);
        break;





      default: break;
    }

    // weer omzetten in rgb

    fadeToTarget();

    IrReceiver.resume();
  }
}

void setup() {
  pinMode(rPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  pinMode(bPin, OUTPUT);

  Serial.begin(115200);
  IrReceiver.begin(irReceivePin);
  Serial.println(F("Ready"));
}

void loop() {
  handleCommand();
}