NeoPixel POV project help

Status
Not open for further replies.

DJ Namaste

Active member
Hello
I found a way to perform POV - Persistence Of Vision images using the Adafruit Neopixel library with a bit of code I modified. The code runs on Teensy lc but on teensy 2.0 the dynamic memory is something like 95% and it will not run the code. The teensy will run FastLED POV code fine but I have put a lot of time into the NeoPixel Library. Im hoping to get a few images along with rainbow fades and stuff. My question is can I clean up the code so that it is able to run on slower processors like teensy 2.0?

I know it can be done in an easier way but any help would be appreciated.

Here is the code. There is only blue values entered to keep the file size lower. The code below works okay on teensy lc but not on teensy 2.0.

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>

#define PIN 17

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(32, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.

void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code


strip.begin();
strip.show(); // Initialize all pixels to 'off'
}

void loop() {
PovX(0);
}


void PovX(uint8_t wait) {
int blue[32][32] =

{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 192, 192, 255, 255, 255, 255, 192, 192, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 192, 192, 255, 255, 255, 255, 255, 255, 255, 255, 192, 192, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 192, 192, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 192, 192, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 192, 192, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 192, 192, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 128, 192, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 192, 128, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 128, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 128, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 192, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 192, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 192, 255, 192, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 192, 255, 192, 0, 0, 0, 0, 0, 0,
0, 0, 128, 192, 0, 0, 0, 192, 192, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 192, 192, 0, 0, 0, 192, 128, 0, 0,
0, 0, 255, 255, 192, 0, 0, 128, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 128, 0, 0, 192, 255, 255, 0, 0,
0, 0, 192, 255, 128, 0, 0, 192, 255, 192, 0, 0, 0, 255, 255, 255, 255, 255, 255, 0, 0, 0, 192, 255, 192, 0, 0, 128, 255, 192, 0, 0,
0, 255, 128, 255, 255, 128, 0, 255, 255, 0, 0, 0, 0, 0, 192, 255, 255, 192, 0, 0, 0, 0, 0, 255, 255, 0, 128, 255, 255, 128, 255, 0,
0, 255, 255, 255, 255, 255, 0, 255, 255, 128, 0, 0, 0, 255, 255, 192, 192, 255, 255, 0, 0, 0, 128, 255, 255, 0, 255, 255, 255, 255, 255, 0,
0, 0, 0, 0, 128, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 128, 0, 0, 0, 0,
0, 0, 0, 0, 0, 128, 0, 128, 255, 255, 255, 255, 255, 255, 192, 0, 0, 192, 255, 255, 255, 255, 255, 255, 128, 0, 128, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 255, 255, 192, 0, 0, 0, 0, 192, 255, 255, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 255, 255, 128, 255, 255, 128, 255, 255, 255, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 192, 255, 255, 255, 255, 255, 255, 255, 255, 192, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 192, 255, 255, 255, 255, 255, 255, 192, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 255, 255, 192, 255, 255, 255, 255, 255, 255, 192, 255, 255, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 128, 255, 0, 0, 192, 255, 255, 255, 192, 255, 255, 192, 255, 255, 255, 192, 0, 0, 255, 128, 0, 0, 0, 0, 0, 0,
0, 128, 255, 255, 0, 255, 255, 255, 255, 0, 0, 255, 255, 255, 192, 192, 192, 192, 255, 255, 255, 0, 0, 255, 255, 255, 255, 0, 255, 255, 128, 0,
0, 192, 255, 255, 255, 255, 255, 192, 0, 0, 0, 0, 128, 255, 255, 255, 255, 255, 255, 128, 0, 0, 0, 0, 192, 255, 255, 255, 255, 255, 192, 0,
0, 0, 0, 128, 192, 255, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 255, 192, 128, 0, 0, 0,
0, 0, 0, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 0, 0,
0, 0, 0, 128, 255, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 255, 128, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

};

int green[32][32] =
{


};

int red[32][32] =
{
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// { 0,220, 0, 0, 0, 0, 0, 0,220, 0},
// { 0, 0,220, 0, 0, 0, 0,220, 0, 0},
// { 0, 0, 0,220, 0, 0,220, 0, 0, 0},
// { 0, 0, 0, 0,250,220, 0, 0, 0, 0},
// { 0, 0, 0, 0,220,220, 0, 0, 0, 0},
// { 0, 0, 0,220, 0, 0,220, 0, 0, 0},
// { 0, 0,220, 0, 0, 0, 0,220, 0, 0},
// { 0,220, 0, 0, 0, 0, 0, 0,220, 0},
// { 220, 0, 0, 0, 0, 0, 0, 0, 0,220},


};



for(int row=0; row<32; row++){
for(int col=0; col<32; col++){
int index = col+1;
uint32_t c = strip.Color(red[row][col], green[row][col], blue[row][col]);
strip.setPixelColor(index, c);
strip.setPixelColor(index+1, c);
strip.setPixelColor(index+2, c);
}
strip.show();
for(int i=0; i<strip.numPixels(); i++){
strip.setPixelColor(i,0);
}
int wait = 2;

delay(wait);
}


}
 
First of all, the general feeling is that neopixel/ws2812s are too slow for POV. This is because the protocol is fixed at 800kHz, which is too slow, and the eye can see the separate frames. Normally, dotstar/APA102's are thought of as the multiple LED of choice for POV. I don't do POV stuff, I'm just echoing what I've read elsewhere.

Second, on the Teensy 3.x/LC, you really should use the static const declaration on the red, green, and blue arrays. Otherwise the compiler must treat these as auto arrays, and initialize them each time the PovX function is called (the compiler would have a duplicate definition in read-only memory, and then copy the whole array to the auto array on the stack). You want static so that the array is initialized only once in static memory, and not on the stack, and you want const to tell the compiler that the values don't change, and that you can put the arrays into read-only memory, which is typically used for program code (on the ARM, not on AVR processors like the Teensy 2.0).

Third your array uses a lot of extra space. You use int, which on Arm processors like the Teensy 3.x/LC, is 32-bits, but the maximum value returned is 255. You could use the uint8_t type which would save about 3/4 of the memory. The way you have it declared using int, the 3 arrays would take 12,288 bytes. If you use uint8_t, it would take 3,072 bytes. Note, the LC only has 8,192 bytes of read/write memory, so those arrays as you've written it without using static const would overflow the memory on an LC (the Teensy 3.2 has 65,536 bytes of memory, so it is less of an issue there).

Fourth, on 8-bit processors like the Teensy 2.0, int is 16 bits (2 bytes) and not 32 bits (4 bytes). So your 3 arrays would take 6,144 bytes of read/write memory. However, the ATmega32u4 used in the Teensy 2.0 only has 2,560 bytes of read/write memory. In addition, you cannot use static const on that processor to move the memory to the flash memory used by the program due to the design of the architecture. Instead you have to use something called PROGMEM that puts the large read-only arrays into flash memory, and then you have to call a function to move the bytes from flash memory to read/write memory.

Finally, the AVR processors are much slower than the Teensy, so it is likely that all of the processing you are doing would be slow enough that you would see the effects of that processing. A lot of effort has gone into the FASTLED and Octows2811 libraries on the Teensy 3.x to speed things up to be as fast as possible.
 
Last edited:
Status
Not open for further replies.
Back
Top