Code or hardware issue?

Martaine2005

May 12, 2015
5,276
Joined
May 12, 2015
Messages
5,276
Code:
// these libraries must be installed prior to uploading
// includes
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
#include <avr/power.h>
#endif

// set number of pixels in strip
int numPix = 16;
// set pin to control neopixels
int neoPin = 4;
// set initial brightness 0-255
int brightness = 255;

// create led object
Adafruit_NeoPixel ledStrip = Adafruit_NeoPixel(numPix, neoPin, NEO_GRBW + NEO_KHZ800);

// assign pins to TX and RX for player
static const uint8_t PIN_MP3_TX = 2;
static const uint8_t PIN_MP3_RX = 3;
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

// create the player object
DFRobotDFPlayerMini myPlayer;

void setup() {
  // initialize neopixels
  ledStrip.begin();
  ledStrip.setBrightness(brightness);
  ledStrip.show();

  // initialize serial port for output
  Serial.begin(9600);
  // initialize player serial port
  softwareSerial.begin(9600);

  // connect to player - print result
  if (myPlayer.begin(softwareSerial)) {
    Serial.println("Connection successful.");
    // set initial volume 0-30
    myPlayer.volume(30);
  } else {
    Serial.println("Connection failed.");
  }
}

void loop() {
  // volume defines both the led brightness and delay after flash
  int volMin = 15;
  int volMax = 31;
  int randomVol = random(volMin, volMax);

  // upper value should be one more than total tracks
  int randomTrack = random(1, 9);

  // lightning variables
  // use rgbw neopixel adjust the following values to tweak lightning base color
  int r = random(40, 80);
  int g = random(10, 25);
  int b = random(0, 10);
  // return 32 bit color
  uint32_t color = ledStrip.Color(r, g, b, 50);
  // number of flashes
  int flashCount = random (5, 15);
  // flash white brightness range - 0-255
  int flashBrightnessMin =  10;
  int flashBrightnessMax =  255;
  // flash duration range - ms
  int flashDurationMin = 5;
  int flashDurationMax = 75;
  // flash off range - ms
  int flashOffsetMin = 0;
  int flashOffsetMax = 75;
  // time to next flash range - ms
  int nextFlashDelayMin = 1;
  int nextFlashDelayMax = 50;
  // map white value to volume - louder is brighter
  int flashBrightness = map(randomVol, volMin, volMax, flashBrightnessMin, flashBrightnessMax);

  // map flash to thunder delay - invert mapping
  int thunderDelay = map(randomVol,  volMin, volMax, 1000, 250);

  // randomize pause between strikes
  // longests track length - ms
  int longestTrack = 18000;
  // intensity - closer to longestTrack is more intense
  int stormIntensity = 30000;
  long strikeDelay = random(longestTrack, stormIntensity);

  // debug serial print
  Serial.println("FLASH");
  Serial.print("Track: ");
  Serial.println(randomTrack);
  Serial.print("Volume: ");
  Serial.println(randomVol);
  Serial.print("Brightness: ");
  Serial.println(flashBrightness);
  Serial.print("Thunder delay: ");
  Serial.println(thunderDelay);
  Serial.print("Strike delay: ");
  Serial.println(strikeDelay);
  Serial.print("-");

  for (int flash = 0 ; flash <= flashCount; flash += 1) {
    // add variety to color
    int colorV = random(0, 50);
    if (colorV < 0) colorV = 0;
    // flash segments of neopixel strip
    color = ledStrip.Color(r + colorV, g + colorV, b + colorV, flashBrightness);
    ledStrip.fill(color, 0, 4);
    ledStrip.show();
    delay(random(flashOffsetMin, flashOffsetMax));
    ledStrip.fill(color, 8, 4);
    ledStrip.show();
    delay(random(flashOffsetMin, flashOffsetMax));
    ledStrip.fill(color, 4, 4);
    ledStrip.show();
    delay(random(flashOffsetMin, flashOffsetMax));
    ledStrip.fill(color, 9, 14);
    ledStrip.show();
    delay (random(flashDurationMin, flashDurationMax));
    ledStrip.clear();
    ledStrip.show();
    delay (random(nextFlashDelayMin, nextFlashDelayMax));
  }
  // pause between flash and thunder
  delay (thunderDelay);

  // trigger audio - randomize volume and track
  myPlayer.volume(randomVol);
  myPlayer.play(randomTrack);

  delay(strikeDelay);
}

I’m having problems with illuminating more than 31 addressable LEDs.
The above code has numPix=16.
I have changed it to 4 and 8 with no problems. 16 to 24 without issues too. But it’s stuck at 31. I can parallel more strips and even two 8x8 matrix with only 31 illuminating on each.
At first, I thought it was my power supply 9V 1.5A.
So tried another external power supply for the LEDs 5V 6A. Same problem. I then injected voltage at the last strip to no avail.
I’m not sure what is causing this issue.
Any help would be appreciated.
PS, I am using arduino nano clone.
 

bertus

Moderator
Nov 8, 2019
3,835
Joined
Nov 8, 2019
Messages
3,835
Hello,

I found this about the number of pixels:
There’s no inherent limit in the maximum length of a NeoPixel chain, but eventually you’ll encounter any of various practical limits:

RAM: NeoPixels require some RAM from the host microcontroller; more pixels = more RAM.
It’s only a few bytes each, but as most microcontrollers are pretty resource-constrained, this becomes a very real consideration for large projects.

Power: each NeoPixel draws a little bit of current; more pixels = more power. Power supplies likewise have some upper limit.

Time: NeoPixels process data from the host microcontroller at a fixed data rate; more pixels = more time and lower animation frame rates.

From this page:

Bertus
 

Martaine2005

May 12, 2015
5,276
Joined
May 12, 2015
Messages
5,276
Thanks @bertus.
I did read that a couple of days ago.
As I’m using the same code data for each pixel, the host ram (arduino) should be able to handle it. It’s not a code for complex animations which would require more ram.

But in the interest of accuracy, I’ll try the setup with an Uno and Mega.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,270
Joined
Nov 17, 2011
Messages
14,270
I am using arduino nano clone
The Github repository for the Adafruit_Neopixel library lists the compatible controllers. The Arduino Nano is not (officially) among them.
However, the library basically seems to work - at least up to 31 pixels in your case.

C++:
    ledStrip.fill(color, 8, 4);
    ledStrip.show();
    delay(random(flashOffsetMin, flashOffsetMax));
    ledStrip.fill(color, 4, 4);
    ledStrip.show();
    delay(random(flashOffsetMin, flashOffsetMax));
    ledStrip.fill(color, 9, 14);
In this section of code you fill different sections of the LED strip with color, but only up to LED #23 (last instruction, start at 9, fill 14). Did you update these numbers when changing the number of pixels?
 

Martaine2005

May 12, 2015
5,276
Joined
May 12, 2015
Messages
5,276
The Github repository for the Adafruit_Neopixel library lists the compatible controllers. The Arduino Nano is not (officially) among them.
However, the library basically seems to work - at least up to 31 pixels in your case.

C++:
    ledStrip.fill(color, 8, 4);
    ledStrip.show();
    delay(random(flashOffsetMin, flashOffsetMax));
    ledStrip.fill(color, 4, 4);
    ledStrip.show();
    delay(random(flashOffsetMin, flashOffsetMax));
    ledStrip.fill(color, 9, 14);
In this section of code you fill different sections of the LED strip with color, but only up to LED #23 (last instruction, start at 9, fill 14). Did you update these numbers when changing the number of pixels?
No I didn’t. I didn’t understand that section of code.
This evening I will //comment them and see exactly what they do. Afterwards I’ll play with the numbers.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,270
Joined
Nov 17, 2011
Messages
14,270
Here's a more detailed explanation. Note: from my interpretation of the Neopixel library only. Untested.
If you comment this section in your code, the LEDs will show nothing.
C++:
ledStrip.fill(color, 8, 4); //fill the buffer for LEDs 8,9,10 and 11 (start with 8, then fill 4 LEDs) with "color"
ledStrip.show();                                //output the buffer to the LED strip
delay(random(flashOffsetMin, flashOffsetMax)); 
ledStrip.fill(color, 4, 4);                    //fill the buffer for LEDs 4, 5, 6 and 7 (start with 4, then fill 4 LEDs) with "color"
ledStrip.show();                                //output the buffer to the LED strip
delay(random(flashOffsetMin, flashOffsetMax));
ledStrip.fill(color, 9, 14);     //fill the buffer for LEDs 9 ... 23 (start with 9, then fill 14 LEDs) with "color"
ledStrip.show();                                //output the buffer to the LED strip
 
Top