I’ve been playing with Photons for a while now, and I’m currently trying to port a project I put together for an Arduino Trinket to a Photon (and ultimately adding some additional functionality) but I’m running into issues. I didn’t create this original project, but I have started trying to tweak the code to get it working on a Photon correctly. It runs, but the colors are completely off (and it seems that it may be flickering in a funny way). The correct result is supposed look similar to a rocket engine flame (think lots of yellows and reds that are flickering). I’ve tried running some basic Neopixel code on my Photon setup that I found online and it ran just fine but I can’t get this specific code running properly. Anything obvious that I’m missing?
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#include <Particle.h>
#include <math.h>
#define PIN A0
#define PIXEL_COUNT 61
//arrayLength = pixel count/3
const int arrayLength=20;
#define SCALE_AMOUNT 0.5
#define FLICKER_SPEED 5
#define FLICKER_AMOUNT 50
struct RGBW {
byte r;
byte g;
byte b;
byte w;
};
RGBW colors[] = {
{ 255, 150, 0, 255}, // yellow + white
{ 255, 120, 0, 0}, // yellow + white
{ 255, 90, 0, 0}, // orange
{ 255, 30, 0, 0}, // orangie-red
{ 255, 0, 0, 0}, // red
{ 255, 0, 0, 0} // extra red
};
int NUMBER_OF_COLORS = sizeof(colors) / sizeof(RGBW);
int percentBetween(int a, int b, float percent) {
return (int)(((b - a) * percent) + a);
}
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags:
//#define WS2811 0x00 // 400 KHz datastream (NeoPixel)
//#define WS2812 0x02 // 800 KHz datastream (NeoPixel)
//#define WS2812B 0x02 // 800 KHz datastream (NeoPixel)
//#define WS2813 0x02 // 800 KHz datastream (NeoPixel)
//#define TM1803 0x03 // 400 KHz datastream (Radio Shack Tri-Color Strip)
//#define TM1829 0x04 // 800 KHz datastream ()
//#define WS2812B2 0x05 // 800 KHz datastream (NeoPixel)
//#define SK6812RGBW 0x06 // 800 KHz datastream (NeoPixel RGBW)
//#define WS2812B_FAST 0x07 // 800 KHz datastream (NeoPixel)
//#define WS2812B2_FAST 0x08 // 800 KHz datastream (NeoPixel)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIN, WS2812);
// 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.
float scale = 1;
float inc = 0.1;
float dir = 1.0;
byte pixelBrightness[arrayLength];
boolean pixelDirection[arrayLength];
// prepopulate the pixel array
void seedArray() {
for (uint16_t i=0; i < PIXEL_COUNT; i++) {
uint16_t p = i % arrayLength;
pixelBrightness[p] = random(255-FLICKER_AMOUNT, 255);
pixelDirection[p] = !!random(0, 1);
}
}
void setup() {
strip.begin();
strip.show();
}
void loop() {
// how many leds for each color
int ledsPerColor = ceil(PIXEL_COUNT / (NUMBER_OF_COLORS-1));
// the scale animation direction
if (scale <= 0.5 || scale >= 1) {
dir = dir * -1;
}
// add a random amount to inc
inc = ((float)random(0, 50)/1000);
// add the increment amount to the scale
scale += (inc * dir);
// constrain the scale
scale = constrain(scale, 0.5, 1);
for (uint16_t i=0; i < PIXEL_COUNT; i++) {
uint16_t p = i % arrayLength;
float val = ((float)i * scale) / (float)ledsPerColor;
int currentIndex = floor(val);
int nextIndex = ceil(val);
float transition = fmod(val, 1);
// color variations
if (pixelDirection[p]) {
pixelBrightness[p] += FLICKER_SPEED;
if (pixelBrightness[p] >= 255) {
pixelBrightness[p] = 255;
pixelDirection[p] = false;
}
} else {
pixelBrightness[p] -= FLICKER_SPEED;
if (pixelBrightness[p] <= 255-FLICKER_AMOUNT) {
pixelBrightness[p] = 255-FLICKER_AMOUNT;
pixelDirection[p] = true;
}
}
RGBW currentColor = colors[currentIndex];
RGBW nextColor = colors[nextIndex];
float flux = (float)pixelBrightness[p] / 255;
byte r = percentBetween(currentColor.r, nextColor.r, transition) * flux;
byte g = percentBetween(currentColor.g, nextColor.g, transition) * flux;
byte b = percentBetween(currentColor.b, nextColor.b, transition) * flux;
byte w = percentBetween(currentColor.w, nextColor.w, transition) * flux;
strip.setPixelColor(i, g, r, b, w);
}
strip.show();
}