Working with arrays (length)

I have project, where I'm aiming to create a digital clock by turning on/off a rather large amount of leds.

So my initial idea is to define an array for each led that needs to be turned on to produce the number. For instance this array would turn on the leds to produce zero (In my actual setup there will be 48 leds in this case, here each numer represents a set of 8 leds together).

int D10[] = {1, 2, 3, 4, 5, 6};

Now to avoid writing a lot of code over and over the idea was to pass the array to a function and loop over it. But here's the problem; each array would have a different length (between 16 and 56 leds) - and as far as I've understood we can't calculate the length of the array.

The loop is rather simple:

      for (int i = 0; i < 6; i++) {
        leds[D10[i]] = CRGB::Red; 
        //FastLED.show();
        //leds[4] = CRGB::Black;  
      }

Would it be an option to create another array that keeps information about the length of each array and use that in the loop? Or how would you suggest to do it?

You can certainly calculate the length of an array

byte anArray[] = {5, 6, 7, 8};
const arrayLength = sizeof(anArray) / sizeof(anarray[0]));

What you can't do is to pass an array to a function and calculate the length of the array in the function. This is because what is passed to the function is a pointer to the array rather than the array itself and the pointer will always have the same length. What you can do is to pass the length of the array toa function along with the array

@Delta_G

I'm not sure I follow? I need to know which leds should be turned on, the rest should be off. If I pass all leds in each digit I would produce the digit 8 all the time? :smiley:

@UKHeliBob

Ah ok that makes sense, thanks!

@Delta_G

Ahh yes of course that makes sense! Wonder why I didn't think of this :smiley:

Regarding the other note that was sort of my idea even if I didn't know quite how to do it. I have leds strips with 8 leds each (one module) that essencially should be treated as one as they are either all on or all off.

how are your single pixels aranged to get a digit? Something like a seven segment display?

If so - have a look at my library:

https://werner.rothschopf.net/202005_arduino_neopixel_display_en.htm

output can be done with the common Print.h interface - for example a simple

display.print(1234);

Yeah basically I'm using 7 strips of 8 leds arranged in a traditional digital digit, so thanks I'll have a loot at it :wink:

7 x 8 = 56 LEDs per digit.

See the Example 64_large_display_64bit

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.