Trouble with a chasing code sequence for 60-LED Pololu Strip

Hi all,

I am trying to create a chasing code sequence for my 60-LED Pololu strip, using the Arduino Leonardo. I need white lights to chase in one direction, and blue lights to chase in the opposite direction, on some kind of timer. I am a novice, and I’ve hit a wall. Any help would be much appreciated.

:slight_smile: Eileen


/* LedStripGradient: Example Arduino sketch that shows
* how to control an Addressable RGB LED Strip from Pololu.
*
* To use this, you will need to plug an Addressable RGB LED
* strip from Pololu into pin 12.  After uploading the sketch,
* you should see a white LED moving along the strip
*/
 
#include <PololuLedStrip.h>
 
// Create an ledStrip object on pin 12.
PololuLedStrip<12> ledStrip;
 
// Create a buffer for holding 120 colors.  Takes 360 bytes.
#define LED_COUNT 60
rgb_color colors[LED_COUNT];
int blue_led = LED_COUNT - LED_COUNT;
int white_led = LED_COUNT - 1;
 
void setup()
{
  // set all LEDs to be off ("black")
  for(byte i = 0; i < LED_COUNT; i++)
  {
    colors[i] = (rgb_color){0, 0, 0};
  }
}
 
void loop()
{
  // Update the colors.
  colors[white_led] = (rgb_color){0, 0, 0};  // turn off previous white LED
  colors[blue_led] = (rgb_color){0, 0, 0};  // turn off previous blue LED
  white_led = (white_led + 2)%LED_COUNT;  // increment white LED position
  blue_led = (blue_led - 2)%LED_COUNT;  // decrement blue LED position
  colors[white_led] = (rgb_color){150, 150, 150};  // set new LED to be white
  colors[blue_led] = (rgb_color){0, 20, 150};  // set new LED to be blue
   
  // Write the colors to the LED strip.  It takes about 8 ms to update 120 LEDs (4m)
  ledStrip.write(colors, LED_COUNT);
   
  delay(500); // changing this delay will change the speed of your LED
}

Hello, Eileen.

Could you explain what trouble you are running into? What are the LEDs doing? Could you tell me more about your setup?

- Jeremy

Hi Jeremy,

When I run that code, a blue LED will blink ever other LED on the 60 LED strip, in a chasing sequence. If I eliminate blue_led from the code, the white one will run, but they won’t both run together.

I am trying to create a code in which a blue led and a white led chase in opposite directions at the same time, at a fairly slow pace. Does that make sense?

Thanks,

Eileen

In the code that you posted it looks like you set blue_led to zero and then later decrement it in this line:

blue_led = (blue_led - 2)%LED_COUNT;  // decrement blue LED position

The modulus operator returns the remainder of a division and does not perform any type of absolute value, so that line results in a negative number. This means that when you call colors[blue_led] you are accessing something that is off the end of the array. That could cause all sorts of undefined behaviors. Since you are trying to decrement blue_led, I recommend changing the line in your loop where you set that variable to something like this instead:

blue_led = (blue_led + LED_COUNT - 2) % LED_COUNT;  // decrement blue LED position

-Claire