1
votes

I have an arduino using fastLED with a strip of 600 ws2812b lights. I'm running the following code:

#include "FastLED.h"
#define DATA_PIN    5       // change to your data pin
#define COLOR_ORDER GRB      // if colors are mismatched; change this
#define NUM_LEDS    600       // change to the number of LEDs in your strip
#define BRIGHTNESS 32
#define WRAP_NUM 55
#define LED_TYPE    WS2812B

CRGB leds[NUM_LEDS];
int startIndex=0;
int bottom=1;

void setup()
{
  delay(3000);

  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
}

void loop()
{
    shapeTwirl();
}

void shapeTwirl()
{
    FastLED.clear();

    static int heart[]={bottom*WRAP_NUM};
    for(int i=0;i<sizeof(heart);i++)
    {
        leds[(heart[i]+startIndex)]=CRGB::Red;
    }
    FastLED.show();
    delay(70);

    startIndex=(startIndex+1)%WRAP_NUM;
}

I have my lights in a circle and this makes a red dot wrap around the circle. However, a blue dot roughly 100 lights away also spins around. There is nothing in my code to make blue light. I have tracked this down to using

int bottom=1;

If I replace bottom with a number in the code, I get rid of the blue dot and it works properly. The problem is also solved if I #define bottom 1;. It doesn't matter if I define bottom where it is now or within shapeTwirl. This leads me to believe there is something wrong with using a variable for bottom, but i've tried using an int, static int, unsigned int to no avail.

Why are the wrong lights turning on?

I'm using an arduino uno to control the lights and external power to power them.

1

1 Answers

1
votes

Note: you should check that the Arduino IDE is configured to print all warnings.


Your code invokes undefined behaviour.

This line:

static int heart[]={bottom*WRAP_NUM};

results in an array of one element initialised with the value of bottom * WRAP_NUM, independently from the value of bottom. I remark this because this might or might not be what you intended.

Here is your problem:

for(int i=0; i < sizeof(heart); i++)

sizeof(heart) returns the number of bytes of your array, which is 2 because that is the size of an int on Arduino. As a consequence, in the instruction of the loop body

leds[(heart[i]+startIndex)]=CRGB::Red;

heart[i] accesses an invalid memory location at the second loop iteration (i == 1), meaning that some other random location might be overwritten with your colour.

If you want to know how many int are stored in your array then you need to replace it with sizeof(heart) / sizeof(int):

for(int i=0; i < (sizeof(heart) / sizeof(int)); i++)

As for the reason why you see a blue light, I would check the following things:

  • Verify that #define COLOR_ORDER GRB is really what you want: i suspect that CRGB::Red would require RGB as COLOR_ORDER.
  • Verify that the wiring is correct