Flora and Neopixels

I have a Flora and 40 neopixels connected to it. Running the strandtest, it works perfectly. Everything lights up. But once I put in my custom code, it fails to light the last 4 of the neopixels and then locks up. This previously worked with 36 lights but now it just crashes.

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include <Adafruit_NeoPixel.h>

int IRpin = 02;  // pin for the IR sensor
IRrecv irrecv(IRpin);
decode_results results;
#define PIN 6



Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN, NEO_RGBW + NEO_KHZ800);
void setup()
{
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  irrecv.enableIRIn(); // Start the receiver
}

void loop()
{

  uint32_t gold = strip.Color(255, 204, 0, 0);
  uint32_t blue = strip.Color(0, 0, 255, 0);
  uint32_t green = strip.Color(0, 255, 0, 0);
  uint32_t red = strip.Color(255, 0, 0, 0);
  uint32_t white = strip.Color(0, 0, 0, 255);
  uint32_t black = strip.Color(0, 0, 0, 0);
  uint32_t purple = strip.Color(153, 0, 255, 0);
  uint32_t gray = strip.Color(102, 102, 102, 0);
  uint32_t turquoise = strip.Color(102, 255, 255, 0);

  if (irrecv.decode(&results))
  {

    irrecv.resume();   // Receive the next value
  }

  switch (results.value)
  {


    case 16738455://0
      for ( int i = 0; i < 36; i++) {
        strip.setPixelColor(i, blue);
        strip.show();
      }    // Solid Blue
      break;

    case 16724175://1
      for ( int i = 0; i < 36; i++) {
        strip.setPixelColor(i, green);
        strip.show();
      }    // Solid Green
      break;

    case 16718055://2
      for ( int i = 0; i < 36; i++) {
        strip.setPixelColor(i, gold);
        strip.show();
      }    // Solid Gold
      break;

    case 16743045: //3
      for ( int i = 0; i < 36; i++) {
        strip.setPixelColor(i, red);
        strip.show();
      }    // Solid Red
      break;

    case 16716015: //4
      for ( int i = 0; i < 36; i++) {
        strip.setPixelColor(i, white);
        strip.show();
      }    // Solid White
      break;

    case 16726215: //5
      for ( int i = 0; i < 36; i++) {
        strip.setPixelColor(i, purple);
        strip.show();
      }    // Solid Purple
      break;

    case 16753245: //Power
      for ( int i = 0; i < 36; i++) {
        strip.setPixelColor(i, black);
        strip.show();
      }    // Solid Black
      break;

    case 16712445: rainbow(20);//Play
      // Rainbow
      break;

    case 16728765: //7
      for ( int i = 0; i < 36; i++) {
        strip.setPixelColor(i, turquoise);
        strip.show();
      }    // Solid Turquoise
      break;

    case 16734885: //6
      for ( int i = 0; i < 36; i++) {
        strip.setPixelColor(i, gray);
        strip.show();
      }    // Solid Gray
      break;
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for (j = 0; j < 256; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

This is the code I've been working with. I cant see it being a hardware issue, but I'm not sure what the issue with the code might be as it worked previously.

TIA

Sorry, "crashes" is not an adequate description of this sort of failure. What does it do, what does it not do?

The board locks up. It is stuck displaying whatever it was doing. The board won't reset with the button and won't respond to the remote I use to control it. Power has to be pulled and then restored. The sensor is still receiving signals though. The light on the IR receiver still lights up when the remote is used.

It seems to be tied to the "gold" color in the sketch but the 4 neopixels on the end don't light up at all.

Helghallen:
The board won't reset with the button and won't respond to the remote I use to control it. Power has to be pulled and then restored.

That is very strange. You need to explain what board you are using.

Please correct your first post to use "code tags" (</>) for the code.

I have edited my original post using code tags.

I am using an Adafruit Flora for this project.

The last 4 LEDs not lighting is the easy part, all your for loops are going from 0 to 35, so you never change LEDs 36 through 39.

Helghallen:
I am using an Adafruit Flora for this project.

Errr, I guess you did explain that in the title. :disappointed_relieved:

After changing the loops from 36 to 40, all the lights came on, but the gold still freezes it and prevents changes without pulling power. Also, when "gold" it fails to light the last light.

I was able to do the rainbow and it froze but the remote still responded and I was able to get out of it without having to pull the power.

Should I set the loops to higher than 40?

Here is some code that I use that utilizes the same library that you are using for the LED strip.

Perhaps, by looking, you may notice some differences, in the way I am using the library, that may go towards solving your issue?

#include "esp_system.h" //This inclusion configures the peripherals in the ESP system.
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"
#include <Adafruit_NeoPixel.h>
#include "AudioAnalyzer.h"
////
/* define event group and event bits */
EventGroupHandle_t eg;
#define evtDo_AudioReadFreq       ( 1 << 0 ) // 1
////
TickType_t xTicksToWait0 = 0;
////
QueueHandle_t xQ_LED_Info;
////
const int NeoPixelPin = 26;
const int LED_COUNT = 24; //total number of leds in the strip
const int NOISE = 10; // noise that you want to chop off
const int SEG = 6; // how many parts you want to separate the led strip into
const int Priority4 = 4;
const int TaskStack40K = 40000;
const int TaskCore1  = 1;
const int TaskCore0 = 0;
const int AudioSampleSize = 6;
const int Brightness = 180;
const int A_D_ConversionBits = 4096; // arduino use 1024, ESP32 use 4096
////
Analyzer Audio = Analyzer( 5, 15, 36 );//Strobe pin ->15  RST pin ->2 Analog Pin ->36
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
Adafruit_NeoPixel leds = Adafruit_NeoPixel( LED_COUNT, NeoPixelPin, NEO_GRB + NEO_KHZ800 );
////
int FreqVal[7];//create an array to store the value of different freq
////
void setup()
{
  eg = xEventGroupCreate();
  Audio.Init(); // start the audio analyzer
  leds.begin(); // Call this to start up the LED strip.
  clearLEDs();  // This function, defined below, de-energizes all LEDs...
  leds.show();  // ...but the LEDs don't actually update until you call this.
  ////
  xQ_LED_Info = xQueueCreate ( 1, sizeof(FreqVal) );
  //////////////////////////////////////////////////////////////////////////////////////////////
  xTaskCreatePinnedToCore( fDo_AudioReadFreq, "fDo_ AudioReadFreq", TaskStack40K, NULL, Priority4, NULL, TaskCore1 ); //assigned to core
  xTaskCreatePinnedToCore( fDo_LEDs, "fDo_ LEDs", TaskStack40K, NULL, Priority4, NULL, TaskCore0 ); //assigned to core
  xEventGroupSetBits( eg, evtDo_AudioReadFreq );
} // setup()
////
void loop() {} // void loop
////
void fDo_LEDs( void *pvParameters )
{
  int iFreqVal[7];
  int j;
  leds.setBrightness( Brightness ); //  1 = min brightness (off), 255 = max brightness.
  for (;;)
  {
    if (xQueueReceive( xQ_LED_Info, &iFreqVal,  portMAX_DELAY) == pdTRUE)
    {
      j = 0;
      //assign different values for different parts of the led strip
      for (j = 0; j < LED_COUNT; j++)
      {
        if ( (0 <= j) && (j < (LED_COUNT / SEG)) )
        {
          set(j, iFreqVal[0]); // set the color of led
        }
        else if ( ((LED_COUNT / SEG) <= j) && (j < (LED_COUNT / SEG * 2)) )
        {
          set(j, iFreqVal[1]); //orginal code
        }
        else if ( ((LED_COUNT / SEG * 2) <= j) && (j < (LED_COUNT / SEG * 3)) )
        {
          set(j, iFreqVal[2]);
        }
        else if ( ((LED_COUNT / SEG * 3) <= j) && (j < (LED_COUNT / SEG * 4)) )
        {
          set(j, iFreqVal[3]);
        }
        else if ( ((LED_COUNT / SEG * 4) <= j) && (j < (LED_COUNT / SEG * 5)) )
        {
          set(j, iFreqVal[4]);
        }
        else
        {
          set(j, iFreqVal[5]);
        }
      }
      leds.show();
    }
    xEventGroupSetBits( eg, evtDo_AudioReadFreq );
  }
  vTaskDelete( NULL );
} // void fDo_ LEDs( void *pvParameters )
////
void fDo_AudioReadFreq( void *pvParameters )
{
  int64_t EndTime = esp_timer_get_time();
  int64_t StartTime = esp_timer_get_time(); //gets time in uSeconds like Arduino Micros
  for (;;)
  {
    xEventGroupWaitBits (eg, evtDo_AudioReadFreq, pdTRUE, pdTRUE, portMAX_DELAY);
    EndTime = esp_timer_get_time() - StartTime;
    log_i( "TimeSpentOnTasks: %d", EndTime );
    Audio.ReadFreq(FreqVal);
    for (int i = 0; i < 7; i++)
    {
      FreqVal[i] = constrain( FreqVal[i], NOISE, A_D_ConversionBits );
      FreqVal[i] = map( FreqVal[i], NOISE, A_D_ConversionBits, 0, 255 );
      log_i( "Freq %d Value: %d", i, FreqVal[i]);//used for debugging and Freq choosing
    }
    xQueueSend( xQ_LED_Info, ( void * ) &FreqVal, xTicksToWait0 );
    StartTime = esp_timer_get_time();
  }
  vTaskDelete( NULL );
} // fDo_ AudioReadFreq( void *pvParameters )
////
//the following function set the led color based on its position and freq value
//
void set(byte position, int value)
{
  // segment 0, red
  if ( (0 <= position) && (position < LED_COUNT / SEG) ) // segment 0 (bottom to top), red
  {
    if ( value == 0 )
    {
      leds.setPixelColor( position, 0, 0, 0 );
    }
    else
    {
      leds.setPixelColor( position, leds.Color( value , 0, 0) );
    }
  }
  else if ( (LED_COUNT / SEG <= position) && (position < LED_COUNT / SEG * 2) ) // segment 1 yellow
  {
    if ( value == 0 )
    {
      leds.setPixelColor(position, leds.Color(0, 0, 0));
    }
    else
    {
      leds.setPixelColor(position, leds.Color( value, value, 0)); // works better to make yellow
    }
  }
  else if ( (LED_COUNT / SEG * 2 <= position) && (position < LED_COUNT / SEG * 3) ) // segment 2 pink
  {
    if ( value == 0 )
    {
      leds.setPixelColor(position, leds.Color(0, 0, 0));
    }
    else
    {
      leds.setPixelColor(position, leds.Color( value, 0, value * .91) ); // pink
    }
  }
  else if ( (LED_COUNT / SEG * 3 <= position) && (position < LED_COUNT / SEG * 4) ) // seg 3, green
  {
    if ( value == 0 )
    {
      leds.setPixelColor(position, leds.Color( 0, 0, 0));
    }
    else //
    {
      leds.setPixelColor( position, leds.Color( 0, value, 0) ); //
    }
  }
  else if ( (LED_COUNT / SEG * 4 <= position) && (position < LED_COUNT / SEG * 5) ) // segment 4, leds.color( R, G, B ), blue
  {
    if ( value == 0 )
    {
      leds.setPixelColor(position, leds.Color( 0, 0, 0));
    }
    else //
    {
      leds.setPixelColor(position, leds.Color( 0, 0, value) ); // blue
    }
  }
  else // segment 5
  {
    if ( value == 0 )
    {
      leds.setPixelColor(position, leds.Color( 0, 0, 0)); // only helps a little bit in turning the leds off
    }
    else
    {
      leds.setPixelColor( position, leds.Color( value, value * .3, 0) ); // orange
    }
  }
} // void set(byte position, int value)
////
void clearLEDs()
{
  for (int i = 0; i < LED_COUNT; i++)
  {
    leds.setPixelColor(i, 0);
  }
} // void clearLEDs()

What are you using as a power source, and how is everything connected?

Idahowalker:
Here is some code that I use that utilizes the same library that you are using for the LED strip.

Perhaps, by looking, you may notice some differences, in the way I am using the library, that may go towards solving your issue?

I'll give it a look, but most of my code is cut and paste from other stuff. I've not really gotten the hang of it all just yet.

david_2018:
What are you using as a power source, and how is everything connected?

I have a 3.7V LiPo battery for power. All connections are soldered, as it was recently working with only a small change of going from 36 to 40 Neopixels. Also, used the USB connection with the computer as well. No change in how it reacts based on power. Also, the LiPo is freshly charged.

Flora feeds power to the rings in series. Data is the same.

for ( int i = 0; i < 36; i++) {
        strip.setPixelColor(i, blue);
        strip.show();
      }    // Solid Blu

Define a variable likeint countLED = 40;and where ever you got the number 36 and 40 replace it with countLED or what ever you call the variable. Now you will only need to change the LED count in one place for the change to reflect through your code.

Idahowalker:

for ( int i = 0; i < 36; i++) {

strip.setPixelColor(i, blue);
        strip.show();
      }    // Solid Blu




Define a variable like`int countLED = 40;`and where ever you got the number 36 and 40 replace it with countLED or what ever you call the variable. Now you will only need to change the LED count in one place for the change to reflect through your code.

I should have realized that could be done, just never checked into it.