I want to isolate the two animations and turn them into functions, which I can later tack onto the NeoPixel strandtest code as additional animations.
Each function works fine by itself, but when both are on together, animation1 does not play - it jumps right to animation2 which loops forever. However, the LED refresh for animation2 slows down a tiny bit. Is the code is somehow repeatedly jumping through animation1 but w/o staying long enough to play it?
If I comment out animation2, then animation1 plays fine. And vice-versa. But animation1 will not play when both functions are active.
I tried to compare this to the NeoPixel strandtest functions, it looks very similar, but I can't figure out the problem... Help! What am I missing here?
Code: Select all
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIXEL_PIN 1 // Pin connected to the NeoPixel strip.
#define PIXEL_COUNT 7 // Number of NeoPixels.
#define PIXEL_TYPE NEO_GRB + NEO_KHZ800
#define SPEED_MS 175 // Animation speed (in milliseconds). This is how
// long to spend in a single animation frame.
//Higher values are slower.
//over 200 it gets choppy
// Color animation values. Each value is a 24-bit RGB color value that will be displayed
// at that current step in the animation.
const int colorSteps = 24; // Number of steps in the animation.
const uint32_t colorAnimation[colorSteps] PROGMEM =
{ 0xFF0000, 0xDA2424, 0xB64848, 0x916D6D, 0x6D9191, 0x48B6B6, 0x24DADA, 0x00FFFF, // Red-cyan
0xFFFF00, 0xDADA24, 0xB6B648, 0x91916D, 0x6D6D91, 0x4848B6, 0x2424DA, 0x0000FF, // Yellow-blue
0x00FF00, 0x24DA24, 0x48B648, 0x6D916D, 0x916D91, 0xB648B6, 0xDA24DA, 0xFF00FF // Green-magenta
};
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
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
// Initialize and clear the NeoPixel strip.
strip.begin();
strip.clear();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
animation1();
animation2();
}
void animation1(){
for (int i = 0; i < PIXEL_COUNT; ++i) {
// solid color pulse of all pixels.
uint8_t currentStep = (millis()/SPEED_MS)%(colorSteps*2-2);
if (currentStep >= colorSteps) {
currentStep = colorSteps-(currentStep-(colorSteps-2));
}
// Note that colors are stored in flash memory so they need to be read
// using the pgmspace.h functions.
uint32_t color = pgm_read_dword_near(&colorAnimation[currentStep]);
strip.setPixelColor(i, color);
strip.show();
}
}
void animation2(){
for (int i = 0; i < PIXEL_COUNT; ++i) {
// Moving color pulse. Use position to change brightness.
uint8_t currentStep = (millis()/SPEED_MS-i)%(colorSteps*2-2);
if (currentStep >= colorSteps) {
currentStep = colorSteps-(currentStep-(colorSteps-2));
}
// Note that colors are stored in flash memory so they need to be read
// using the pgmspace.h functions.
uint32_t color = pgm_read_dword_near(&colorAnimation[currentStep]);
strip.setPixelColor(i, color);
strip.show();
}
}