Loop is only calling function once.

I have two led strips. One has a stationary (for now) color and the other has different colors set every 1000ms. I have a method that should take in the color I would like the strip and which Adafruit_NeoPixel strip it is. When the program runs, I get WHITE on strip and YELLOW on the other. Those are the correct first two colors, but the “rear_strip” never changes off yellow. Am I calling the function incorrectly? Code is below.

#include <Adafruit_NeoPixel.h>
// Dropping this, changes the color of later LED's
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
#define REAR_LED_PIN    12
#define CRYSTAL_LED_PIN 13

// How many NeoPixels are attached to the Arduino?
#define REAR_LED_COUNT 24
#define CRYSTAL_LED_COUNT 10

//How Bright you want these strips? Set BRIGHTNESS to about 1/5 (max = 255)
#define REAR_BRIGHT 255
#define CRYSTAL_BRIGHT 255

// Colors
const auto RED = Adafruit_NeoPixel::Color(255,0,0);
const auto BLUE = Adafruit_NeoPixel::Color(0,0,255);
const auto PURPLE = Adafruit_NeoPixel::Color(128,0,128);
const auto BLACK = Adafruit_NeoPixel::Color(0,0,0);
const auto WHITE = Adafruit_NeoPixel::Color(255,255,255);
const auto GREEN = Adafruit_NeoPixel::Color(0,128,0);
const auto YELLOW = Adafruit_NeoPixel::Color(255,255,0);

// Declare our NeoPixel strip objects:
Adafruit_NeoPixel rear_strip(REAR_LED_COUNT, REAR_LED_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel crystal_strip(CRYSTAL_LED_COUNT, CRYSTAL_LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  rear_strip.begin();           // INITIALIZE NeoPixel rear_strip object (REQUIRED)
  crystal_strip.begin();
  rear_strip.show();            // Turn OFF all pixels ASAP
  crystal_strip.show();
  rear_strip.setBrightness(REAR_BRIGHT);
  crystal_strip.setBrightness(CRYSTAL_BRIGHT);
}

void loop() {
  setColor(YELLOW, rear_strip);
  setColor(WHITE, crystal_strip);
  delay(1000);
  setColor(RED, rear_strip);
  delay(1000);
  setColor(BLUE, rear_strip);
  delay(1000);
  setColor(PURPLE, rear_strip);
  delay(1000);
  setColor(BLACK, rear_strip);
  delay(1000);
  setColor(WHITE, rear_strip);
  delay(1000);
  setColor(GREEN, rear_strip);
  delay(1000);
}

void setColor(uint32_t color, Adafruit_NeoPixel strip) {
  for(int i = 0; i < strip.numPixels(); i++) { // For each pixel in the passed strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
  }
}

Please try posting questions about Adafruit devices on the dedicated forum.

Adafruit Forum Arduino

I don’t see this as an Adafruit question. The OP simply asked a programming question.

OP- Try putting Serial.println() statements in the loop to see where the program is hanging up.

Have you tested rear strip by itself just setting it say blue (ie no changes as it loops), then edit the sketch to make it red, etc, so you know it’s actually working correctly?

And if it is able to display each colour, I wonder then if there’s a need for some kind of “release” command to turn the previous colour off before setting it to a new one? (<<<< wild guess, that :wink: )

SteveMann:
I don’t see this as an Adafruit question. The OP simply asked a programming question.

OP- Try putting Serial.println() statements in the loop to see where the program is hanging up.

I will have to google how to do this and come back. I don’t think I have ever been able to see the debugger in real time. But the issue is clearly with the setColor(uint32_t color, Adafruit_NeoPixel strip) declaration. Specifically passing the Adafruit_NeoPixel object. When I break this out into two methods, it works perfectly.

blomcrestlight:
Have you tested rear strip by itself just setting it say blue (ie no changes as it loops), then edit the sketch to make it red, etc, so you know it’s actually working correctly?

And if it is able to display each colour, I wonder then if there’s a need for some kind of “release” command to turn the previous colour off before setting it to a new one? (<<<< wild guess, that :wink: )

That is a good thought and I had tried that. Both strips work fine on their own. And they also work find when I use two identical methods only changing the name of the strip inside each.

I am rather sure at this point it is the declaration of the Adafruit_NeoPixel object.

joerdie:

void setColor(uint32_t color, Adafruit_NeoPixel strip) {

for(int i = 0; i < strip.numPixels(); i++) { // For each pixel in the passed strip…
    strip.setPixelColor(i, color);        //  Set pixel’s color (in RAM)
    strip.show();                          //  Update strip to match
  }
}

First, you are passing a copy of the strip object to this method - you are passing it by value. This copy "dissapears"after the function call is complete, and this can do odd things to the state of the strip. Pass it by reference instead.

Second, and less importantly, we usually tell the strip to update itself after setting all the colours we want.

Try this instead:

void setColor(uint32_t color, Adafruit_NeoPixel& strip) {
  for(int i = 0; i < strip.numPixels(); i++) { // For each pixel in the passed strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  }
  strip.show();                          //  Update strip to match
}
1 Like