I added the single led turn signal in case the stop button is pusshed, and corrected a bug. This version should be OK.
#include <FastLED.h>
#define LED_PIN1 2
#define NUM_LEDS1 16
// buttons / switches for commands
#define StopButton 3
#define LeftSignal 4
#define PosSwitch 5
#define RightSignal 6
CRGB leds[NUM_LEDS1]; // 0-7 left, 8-15 right
byte NTurnLeft = NUM_LEDS1 / 2 - 1;
byte NTurnRight = NUM_LEDS1 / 2 + 1;
bool TurnLeft = false; //
bool TurnRight = false; //
bool Stop = false; // stop light on or off ?
bool Position = true; // position light on or off ?
const byte BrightnessPos = 40; // percentage
const byte BrightnessTurn = 100;
const byte BrightnessStop = 100;
unsigned long TurnDur = 30ul;
unsigned long ChronoLeft = 0;
unsigned long ChronoRight = 0;
void ReadButtons () {
TurnLeft = digitalRead(LeftSignal);
TurnRight = digitalRead(RightSignal);
Stop = !digitalRead(StopButton);
Position = !digitalRead(PosSwitch);
delay(20); // for debouncing
}
void TurnSignalRightUp () {
if (millis() - ChronoRight >= TurnDur) {
ChronoRight = millis();
int orangeR = 255 * BrightnessTurn / 100;
int orangeG = 150 * BrightnessTurn / 100;
if (Stop) leds[NTurnRight - 1] = CRGB(orangeR, orangeG, 0);
else for (int i = NUM_LEDS1 / 2; i < NTurnRight; i++) leds[i] = CRGB(orangeR, orangeG, 0);
NTurnRight ++;
if (NTurnRight > NUM_LEDS1) {
NTurnRight = NUM_LEDS1;
}
}
}
void TurnSignalLeftDown () {
if (millis() - ChronoLeft >= TurnDur) {
ChronoRight = millis();
int orangeR = 255 * BrightnessTurn / 100;
int orangeG = 150 * BrightnessTurn / 100;
if (Stop) leds[NTurnLeft] = CRGB(orangeR, orangeG, 0);
else for (int i = NUM_LEDS1 / 2 - 1; i >= NTurnLeft; i--) leds[i] = CRGB(orangeR, orangeG, 0);
NTurnLeft --;
if (NTurnLeft > 254) {
NTurnLeft = 0;
}
}
}
void LightAll (byte Bright) {
int red = 255 * Bright / 100;
fill_solid(leds, NUM_LEDS1, CRGB(red, 0, 0));
}
void setup() {
FastLED.addLeds<WS2812B, LED_PIN1, GRB>(leds, NUM_LEDS1);
pinMode (StopButton, INPUT_PULLUP);
pinMode (LeftSignal, INPUT_PULLUP);
pinMode (PosSwitch, INPUT_PULLUP);
pinMode (RightSignal, INPUT_PULLUP);
// Serial.begin(115200);
ChronoLeft = millis();
ChronoRight = ChronoLeft;
}
void loop() {
ReadButtons ();
if (!Position) {
FastLED.clear(); // Position light OFF
} else LightAll (BrightnessPos); // Position light ON
if (Stop) LightAll (BrightnessStop); // Stop light ON
if (!TurnRight) TurnSignalRightUp (); // Do the right wave up
else NTurnRight = NUM_LEDS1 / 2 + 1;
if (!TurnLeft) TurnSignalLeftDown (); // Do the left wave down
else NTurnLeft = NUM_LEDS1 / 2 - 1;
FastLED.show();
}