Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Loop is only calling function once.
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Loop is only calling function once.

by joerdie on Wed Mar 18, 2020 11:15 pm

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.

Code: Select all | TOGGLE FULL SIZE
#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
  }
}


joerdie
 
Posts: 2
Joined: Wed Mar 18, 2020 11:13 pm

Re: Loop is only calling function once.

by adafruit_support_bill on Thu Mar 19, 2020 5:44 am

I don't see a problem in the code. It could be a power issue. What happens if you reduce the brightness?

Code: Select all | TOGGLE FULL SIZE
//How Bright you want these strips? Set BRIGHTNESS to about 1/5 (max = 255)
#define REAR_BRIGHT 25
#define CRYSTAL_BRIGHT 25

adafruit_support_bill
 
Posts: 78955
Joined: Sat Feb 07, 2009 10:11 am

Re: Loop is only calling function once.

by joerdie on Thu Mar 19, 2020 4:10 pm

When I lower the brightness I get the same result. I think the issue is when I pass the Adafruit_NeoPixel object? But I am not 100% sure. Is that how I should be passing the object to a method?

joerdie
 
Posts: 2
Joined: Wed Mar 18, 2020 11:13 pm

Re: Loop is only calling function once.

by adafruit_support_bill on Thu Mar 19, 2020 6:21 pm

Calling it that way, I C++ will try to pass by value. That could cause a stack crash.

You can try passing the address of the strip:

Code: Select all | TOGGLE FULL SIZE
  setColor(YELLOW, &rear_strip);


And changing your function as follows:

Code: Select all | TOGGLE FULL SIZE
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
  }
}

adafruit_support_bill
 
Posts: 78955
Joined: Sat Feb 07, 2009 10:11 am

Re: Loop is only calling function once.

by jim_lee on Sat Mar 21, 2020 7:29 pm

Take show() out of your loop.

-jim lee

jim_lee
 
Posts: 669
Joined: Thu May 24, 2012 8:24 pm

Please be positive and constructive with your questions and comments.