/*
Forum: https://forum.arduino.cc/t/mit-unerklarlicher-ruckler-bei-led-effekt/1159262
Wokwi: https://wokwi.com/projects/373344458406571009
*/
#include <Adafruit_NeoPixel.h>
enum EffectDirection { FORWARD, REVERSE };
const uint8_t LEDPIN1 = 5;
const uint16_t NUMPIXELS1 = 300;
Adafruit_NeoPixel strip1(NUMPIXELS1, LEDPIN1, NEO_GRB + NEO_KHZ800);
constexpr byte ledRed = 7;
int Increment = 0;
int StartLED = 1;
int EndLED = 40;
uint32_t Color1 = 0x0000DD;
int Direction = FORWARD;
void setup() {
Serial.begin(115200);
strip1.begin();
strip1.clear();
strip1.show();
pinMode(ledRed,OUTPUT);
}
int count = 0;
void loop() {
int ml = (EndLED - StartLED);
if (Increment >= ml) {
Increment = 0;
digitalWrite(ledRed,HIGH);
count++;
}
if (Increment == 10) {digitalWrite(ledRed,LOW);}
Increment++;
if (Direction == REVERSE) {
for (uint16_t i = StartLED; i <= EndLED; i++) {
strip1.setPixelColor(i, ((sin(Increment + i) * 127 + 128) / 255) * ((Color1 >> 16) & 0xFF),
((sin(Increment + i) * 127 + 128) / 255) * ((Color1 >> 8) & 0xFF),
((sin(Increment + i) * 127 + 128) / 255) * (Color1 & 0xFF));
}
}
else { // Forward
for (uint16_t i = StartLED; i <= EndLED; i++) {
strip1.setPixelColor(i, ((sin(i - Increment) * 127 + 128) / 255) * ((Color1 >> 16) & 0xFF),
((sin(i - Increment) * 127 + 128) / 255) * ((Color1 >> 8) & 0xFF),
((sin(i - Increment) * 127 + 128) / 255) * (Color1 & 0xFF));
}
}
strip1.show();
delay(50);
if (count >= 2){
count = 0;
Increment = 0;
if (Direction == REVERSE) {
Direction = FORWARD;
Serial.println("FORWARD");
} else {
Direction = REVERSE;
Serial.println("REVERSE");
}
}
}