0

I'm working on a program for an addressable LED strip. It is working and at this point I'm trying to make my code better. I have 3 LED strips and I made a function which all three has to do. In the function I want to specify which one needs to be updated so I used attributes, but this doesn't seem to work. I can't find this on the FastLed documentation.

//Number of leds powered
int led_state_1 = 0;
int led_state_2 = 0;
int led_state_3 = 0;

// This is an array of leds.  One item for each led in your strip.
CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];
CRGB leds3[NUM_LEDS];

void CheckAndUpdateLed(CRGB LedArray, int led_state){
     resetLedStrip(LedArray);
     for(int whiteLed = 0; whiteLed < led_state; whiteLed = whiteLed + 1) {
      // Turn our current led on to white, then show the leds
      LedArray[whiteLed] = CRGB::White;
      // Show the leds (only one of which is set to white, from above)
      FastLED.show();
   }
}

When I change LedArray to leds1 it is working. I'm calling the function as CheckAndUpdateLed(leds1, led_state_1);

karel
  • 5,489
  • 46
  • 45
  • 50
Heis
  • 606
  • 5
  • 25
  • Since your leds1 array is global, there is no need to have it passed in as an argument. Every function in your code can just directly access it as leds1. I think you have a call by value vs call by reference issue, but there's no need to fix that. Just use leds1 and stop trying to pass it as an argument. – Delta_G Apr 14 '20 at 18:21
  • @Delta_G I want use the same function for leds2 and leds3 aswell thats why im trying to pass it as an argument – Heis Apr 15 '20 at 14:03
  • Then don't pass a copy of the array. Pass a pointer to it. Pass a CRGB*. When you pass a copy like this then the copy gets worked on and when the function ends your original array is unchanged. – Delta_G Apr 15 '20 at 15:52
  • @Delta_G true sorry about that i got another solution now i posted it – Heis Apr 15 '20 at 16:11

1 Answers1

0

I think my question was a bit unclear sorry for that. I came up with another way of doing this. Instead of 1 led strip I check them all in the same function.

#define NUM_STRIPS 3
#define NUM_LEDS_PER_STRIP 15
CRGB leds[NUM_STRIPS][NUM_LEDS_PER_STRIP];


int led_states[] = {0, 0, 0};    

void CheckAndUpdateLeds(){
      //resets the leds to black
      resetLedStrips();
      // This outer loop will go over each LED strip, one at a time
      for(int x = 0; x < NUM_STRIPS; x++) {
        // This inner loop will go over each led in the current strip, one at a time till the amount of light is the same as in the led_state
         for(int led = 0; led < led_states[x]; led = led + 1) {
          // Turn our current led on to white, then show the leds
          leds[x][led] = CRGB::White;
          FastLED.show();
        }
      }
    }

    void resetLedStrips(){
        for(int x = 0; x < NUM_STRIPS; x++) {
           for(int led = 0; led < NUM_LEDS_PER_STRIP; led = led + 1) {
             leds[x][led] = CRGB::Black;
           }
       }
    }
Heis
  • 606
  • 5
  • 25