/*
from forum post..
https://forum.arduino.cc/t/ws2811-leds-arent-doing-what-i-think-they-should/1214162
quick solve is to add some delay to slow down the animations..
proper solutiuon is to use non-blocking delays, keeping loop fast and using it..
1.22.2024 ~q
*/
#include <Adafruit_NeoPixel.h>
#define PIN 2 // input pin Neopixel is attached to
#define NUMPIXELS 16 // number of neopixels in strip
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
byte stateSys = 0;
int currPixel = 0;
unsigned long lastShow;
unsigned long intervalShow = 100;
void setup() {
// Initialize the NeoPixel library.
pixels.begin();
// clear all
for (int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
}
} // end setup
void loop() {
switch (stateSys) {
case 0: if (RaceForward()) {
stateSys++;
} break;
case 1: if (RaceBackward()) {
stateSys++;
} break;
case 2: if (RaceForward()) {
stateSys++;
} break;
case 3: if (RaceBackward()) {
stateSys++;
} break;
case 4: if (SlowFill(pixels.Color(0, 0, 250))) {
stateSys = 0;
} break;
}
/* quick solve.. add some delays..
// race forward
// white
for (int j=0; j<2; j++){
for (int i=0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(250, 250, 250));
if (i>0)
pixels.setPixelColor(i-1, pixels.Color(0, 0, 0));
pixels.show();
delay(100);
}
// race back
for (int i = NUMPIXELS-1; i > -1; i--)
{
if (i<NUMPIXELS-1)
pixels.setPixelColor(i+1, pixels.Color(0, 0, 0));
pixels.setPixelColor(i, pixels.Color(250, 250, 250));
pixels.show();
delay(100);
}
} // end race
// set all to blue
for (int i=0; i < NUMPIXELS; i++)
{
// green, red, blue
pixels.setPixelColor(i, pixels.Color(0, 0, 250));
pixels.show();
delay(10);
}// end blue
*/
} // end loop
bool RaceForward() {
bool result = false;
if (millis() - lastShow >= intervalShow) {
lastShow = millis();
pixels.setPixelColor(currPixel, pixels.Color(250, 250, 250));
if (currPixel > 0)
pixels.setPixelColor(currPixel - 1, pixels.Color(0, 0, 0));
pixels.show();
currPixel++;
if (currPixel == NUMPIXELS) {
result = true; //done
currPixel = NUMPIXELS - 1;
}
}
return result;
}
bool RaceBackward() {
bool result = false;
if (millis() - lastShow >= intervalShow) {
lastShow = millis();
pixels.setPixelColor(currPixel, pixels.Color(250, 250, 250));
if (currPixel < NUMPIXELS - 1)
pixels.setPixelColor(currPixel + 1, pixels.Color(0, 0, 0));
pixels.show();
currPixel--;
if (currPixel == -1) {
result = true; //done
currPixel = 0;
}
}
return result;
}
bool SlowFill(uint32_t color) {
bool result = false;
if (millis() - lastShow >= intervalShow) {
lastShow = millis();
pixels.setPixelColor(currPixel, color);
pixels.show();
currPixel++;
if (currPixel == NUMPIXELS) {
result = true; //done
currPixel = 0;
}
}
return result;
}