I'm fairly new to working and programming with arduinos. The project I am working on is a digital clock which uses a string of WS2812 leds to light up the correct numbers. Each digit consists of 7 leds and there are two 'colon' leds between the second and third digits. I am using the FastLED library to try to program the code. I am able to make the leds light up such that all four digits are the same number (four consecutive 5's). However, the second I try to have it display multiple different numbers, everything goes south quickly. Here are some pictures of my current results:

55:55. I can create this with any number 0-9.

55:55. I can create this with any number 0-9.

Should be 12:34.

Should be 12:34.

Here is the code that I'm working with so far:

#include <RTClib.h>

RTC_DS3231 rtc;
DateTime now;

#define NUM_LEDS 30
#define LED_PIN 6


void setup() {
  // put your setup code here, to run once:


  FastLED.addLeds<WS2812, LED_PIN>(leds, NUM_LEDS);


void loop() {
  // put your main code here, to run repeatedly:

  const int ZERO[]={0,1,2,4,5,6};
  const int ONE[]={2,6};
  const int TWO[]={0,1,3,5,6};
  const int THREE[]={1,2,3,5,6};
  const int FOUR[]={2,3,4,6};
  const int FIVE[]={1,2,3,4,5};
  const int SIX[]={0,1,2,3,4,5};
  const int SEVEN[]={2,5,6};
  const int EIGHT[]={0,1,2,3,4,5,6};
  const int NINE[]={2,3,4,5,6};

  for(int i=0; i<sizeof(ONE);i++){

  for(int i=0; i<sizeof(TWO);i++){

  for(int i=0; i<sizeof(THREE);i++){

  for(int i=0; i<sizeof(FOUR);i++){

Can anyone explain what's going wrong? Any help would be greatly appreciated. Let me know if you need clarification!

  • what happens if you turn off all LEDs first? ... try putting FastLED.show(); after the last for loop
    – jsotola
    Commented Sep 9, 2020 at 0:28
  • I did try that and nothing changed. I also have FastLED.clear(); in the set up so it at least gets cleared once when the program is uploaded.
    – jbryan
    Commented Sep 9, 2020 at 3:50
  • Start by writing a sketch that switches just one segment of one digit on, all others off. Then each second, change to the next segment. You might find the problem, be it in your usage of FastLED or the hardware. (Hardware, because it looks like a multiplexing problem.) Commented Sep 9, 2020 at 10:57

1 Answer 1


Your problem is your usage of sizeof(). It doesn't do what you think it does.

sizeof() gives you the amount of memory a variable takes up. It does not give you the number of elements in an array.

For example:

const int ONE[]={2,6};

sizeof(ONE) will not give you 2. It will give you 4. An int is two bytes in size (16 bits). An array of 2 ints is, therefore, 2x2 = 4 bytes.

So when you're iterating over a 2 byte array but counting 0-3 you're overflowing your array and reading from the next bits of memory - which happen to be the first two entries of the TWO array in this case.

To get the number of entries in an array you must divide the sizeof() by the size of the variable type the array is made up of. The most common way of doing that is:

sizeof(ONE) / sizeof(ONE[0])

It's common to set up some macros at the time of array creation so you can easily refer to it later:

const int ONE[]={2,6};
#define ONE_SIZE (sizeof(ONE) / sizeof(ONE[0]))

You should also consider using an array of byte (or better, the standard uint8_t) where the size of one element will be 1 not 2, since you don't need to store any number > 255. It's still good to do the division of the sizeof() even with a 1 byte size in case you should ever need to change the variable type in the future. It's also a good habit to get into.

  • This explains SO much! It works now. Thank you!
    – jbryan
    Commented Sep 9, 2020 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.