SOLVED: Control color of Wordclock WS2812B Leds with Button

Hallo,

Relatively new to programming, I try to use a wordclock sketch which was programmed by someone who is not longer available / responding. Therefore I try to “enhance” his sketch to further levels…

That said, I want to add a button to the sketch, which, on press, changes the color of the LEDs.

(I attach the complete sketch below, here for fast reference, the (from my perspective) essential parts for fast reading…)

The color is taken care in the section Switch LED:

void SwitchLED(int MyArray[], int n) {
  //Umgebungshelligkeit überprüfen (sofern gewünscht)
  if (maxBrightness != 0) {
    Ambient = analogRead(AmbientLightPin);
    //Nur bei größeren Helligkeitsänderungen
    //soll die Helligkeit der LEDs angepasst werden:
    if ((Ambient > LastAmbient * 1.10) || (Ambient < LastAmbient * 0.90)) {
      BRIGHTNESS = map(Ambient, 0, 1023, maxBrightness, minBrightness);
      LastAmbient = Ambient;
    }
  }
  //Funktion zum Anschalten der LEDs in warmweiß
  for (i = MyArray[0]; i < MyArray[0] + n; i++) {
   leds[i] = Functions[colorindex];
    //leds[i].setRGB(255, 0, 0); // Red
    // HEX Warmweiß | Hier gewünschte LED Farbe (HEX) eintragen
    FastLED.setBrightness(BRIGHTNESS);
    //leds[i] = CHSV(140, 27, BRIGHTNESS);
    // ODER hier gewünschte Farbe in HSV
    //(  CHSV(FARBE, SÄTTIGUNG, BRIGHTNESS)  )
    //leds[i].setRGB(255, 68, 221);
    // ODER hier gewünschte Farbe in RGB
    //(  .setRGB(Rot,Grün,Blau)   )
  }
}

And the current loop (and color) function, where I try to get the color connected to the “colorindex” which counts up quite nicely on every press of the button, but, unfortunately. without changing the color…

// Pattern 1 - White light, all LEDs in the strip are white
void pattern1() {
  leds[i].setRGB(255, 255, 255); // White
  //FastLED.show();
}

// Pattern 2 - Red light, all LEDs in the strip are white
void pattern2() {
  leds[i].setRGB(255, 0, 255); // Red
  //FastLED.show();
}

void Rainbow(){
  // FastLED's built-in rainbow generator
   // random colored speckles that blink in and fade smoothly
  fadeToBlackBy( leds, NUM_LEDS, 10);
  int pos = random16(NUM_LEDS);
leds[pos] += CHSV( random8(64), 200, 255);
//FastLED.show();
}

void loop() {
  /* Schleifen Funktion, die die Uhrzeit ausliest
      und dann in die entsprechende Funktionen übergibt.
  */
  Minute = minute();
  int Std = hour();
  if (Std > 12)
    Stunde = Std - 12;
  else if (Std == 0)
    Stunde = 12;
  else
    Stunde = Std;

  CheckDST();
  displaytime();
  FastLED.delay(250);
  Serial.println(Stunde);
  Serial.println(Minute);

/*Button
  // read that state of the pushbutton value;
  int buttonVal = digitalRead(buttonPin);
  if (buttonVal == LOW && oldButtonVal == HIGH) {
    // button has just been pressed
    lightPattern = lightPattern + 1;
  }
  if (lightPattern > nPatterns) lightPattern = 1;
  oldButtonVal = buttonVal;
*/


  bool buttonVal = digitalRead(buttonPin);
  if (!buttonVal && oldButtonVal) {
    // button has just been pressed
    colorindex = (colorindex + 1) % 16;
    Serial.print("colorindex ");
    Serial.println(colorindex);
  }
  oldButtonVal = buttonVal;
 
  switch (colorindex) {
    case 1:
      pattern1();
      break;
    case 2:
      Rainbow();
      break;
    case 3:
      pattern2();
      break;
  }
 }

I was not able to get this working…
I assume, something is wrong with this line in the SwitchLED function

   leds[i] = Functions[colorindex];

One iteration before, I tried to use a function (pattern1() ), but this was not successfull as well.

Bore the setup, I tried to define Function as a “group” somehow with this:

typedef uint32_t (*SimplePatternList[])();
SimplePatternList Functions = {pattern1, Rainbow, pattern2};

but no success as well.

I would be glad, if someone could point at the part of the code to be adjusted…
Thanks for your help!
Arbe

Wordclock_basic_button.ino (14.1 KB)

CRGB leds[NUM_LEDS];

SimplePatternList Functions = {pattern1, Rainbow, pattern2};

   leds[i] = Functions[colorindex];

Since leds is an array of CRGB objects, it does not make sense to assign the address of a function to the element of the array. I can't imagine why you think that would change the color of an LED.

Hi PaulS,

as I said, it was a try which I saw in another sketch...

Do you have a hint for me, how to do better?

thanks, Arne

Hallo,

this issue was solved, I did not understood the help I got from another Forum member…(at least until now)…

Now, colors are changing nicely.
For reference for other users, please find attached the sketch.

Thank you all again!

Wordclock_basic_button_FINAL.ino (14 KB)