Model car LED lighting control help please

Hi Guys,

I am using a nano to control a bunch of LED lights inside a model car and I would like some help to control them with an element of randomness but still behaving like a car. I can control the LEDs without issue and have the "headlights connected to PWM so can control the brightness.

I have this working so far:

  1. a ring of LEDs spinning to simulate the engine
  2. switches in the doors to control the interior lights which are WS2812 leds so I can configure the color of the interior when a door is open.
  3. headlights are PWM controlled led rings so have low and high beam. The state is randomly selected every few seconds.

The part I have trouble getting my head around is how to make the indicators occasionally turn on and flash a random number of times whilst still having a normal interval between flashes.

The indicators should have 4 states

  1. Off
  2. Left flash
  3. Right flash
  4. Both flash

I want it to look realistic so I don't just want the indicator to flash once and I don't want the indicator flash to affect any of the other lights so using delay is out...

I would really love some ideas how to do it please.

thanks for any help :slight_smile:

Here is the code I currently have that works but no indicators LEDs yet:

#include <FastLED.h>

#define LED_PIN     2
#define NUM_LEDS    2
#define BRIGHTNESS  200
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100


// Engine LEDs
#define REDLED_1 8
#define REDLED_2 7
#define REDLED_3 6
#define REDLED_4 5
#define REDLED_5 4
#define REDLED_6 9

#define Headlight_Left 10       //PWM
#define Headlight_Right 11      //PWM

// Indicator LEDs
#define Front_Left_Indicator A0
#define Rear_Left_Indicator1 A1
#define Rear_Left_Indicator2 A2

#define Front_Right_Indicator A3
#define Rear_Right_Indicator1 A4
#define Rear_Right_Indicator2 A5

// Spare LEDs
#define Spare1 13
#define Spare2 A6

//Input Switches 
#define Door_switch 3

//WS2812B LEDs x2
#define Interior 2              

unsigned long previousMillis = 0;        // will store last time LED was updated
unsigned long Headlight_previousMillis = 0;        // will store last time LED was updated
const long engine_RPM_interval = 20;           // interval at which to spin red engine LEDs
const long Headlight_interval = 200;           // interval at which to change Headlight state  

byte    pins[6] = {REDLED_6,REDLED_5,REDLED_4,REDLED_3,REDLED_2,REDLED_1};

int Door_State = 0; 
int HeadLight_State = 0; 
int Indicator = 0;


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600); 
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(BRIGHTNESS);
  
  pinMode(Door_switch, INPUT);
  pinMode(REDLED_1, OUTPUT);
  pinMode(REDLED_2, OUTPUT);
  pinMode(REDLED_3, OUTPUT);
  pinMode(REDLED_4, OUTPUT);
  pinMode(REDLED_5, OUTPUT);
  pinMode(REDLED_6, OUTPUT);
  pinMode(Headlight_Left, OUTPUT);
  pinMode(Headlight_Right, OUTPUT);
  pinMode(Front_Left_Indicator, OUTPUT);
  pinMode(Rear_Left_Indicator1, OUTPUT);
  pinMode(Rear_Left_Indicator2, OUTPUT);
  pinMode(Front_Right_Indicator, OUTPUT);
  pinMode(Rear_Right_Indicator1, OUTPUT);
  pinMode(Rear_Right_Indicator2, OUTPUT);
  //pinMode(Spare1, OUTPUT);
  //pinMode(Spare2, OUTPUT);
  
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
unsigned long currentMillis = millis();

// Keep engine LEDs spinning
if (currentMillis - previousMillis >= engine_RPM_interval){
  previousMillis = currentMillis;
  Cycle_Engine_Leds();
}

// Choose Headlight State
if (currentMillis - Headlight_previousMillis >= Headlight_interval){
  Headlight_previousMillis = currentMillis;
  HeadLight_State = random(1,4);
  Serial.println(HeadLight_State); 
}
HeadLights();


// Read input switch to see if a car door is open
Door_State = digitalRead(Door_switch);
InteriorLights();

// every random interval 1-10minutes 
  // random choose left, right or both indicator to blink 
  // random choose number of times to blink 
  // initiate blink 

// every random interval 1-10minutes 
  // random choose 
  // random choose number of times to blink 
  // initiate blink 




}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Indicators(){


}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Cycle_Engine_Leds(){
  static byte led = 0;
  digitalWrite(pins[led], LOW);
  if (++led >= sizeof(pins)/sizeof(pins[0])) led = 0;
  digitalWrite(pins[led], HIGH);
}  


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void InteriorLights(){
  if (Door_State == LOW){
    for( int i = 0; i < NUM_LEDS; ++i){
        leds[i] = CRGB::Blue;   
        FastLED.show();
    }
  }
  if (Door_State == HIGH){
    for( int i = 0; i < NUM_LEDS; ++i){
        leds[i] = CRGB::Black;  
        FastLED.show();
    }
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void HeadLights(){
  if (HeadLight_State == 3){
    analogWrite(Headlight_Left, 255);   // turn the LED on (HIGH is the voltage level)
    analogWrite(Headlight_Right, 255);   // turn the LED on (HIGH is the voltage level)
  }
  if (HeadLight_State == 2){
    analogWrite(Headlight_Left, 50);   // turn the LED on (HIGH is the voltage level)
    analogWrite(Headlight_Right,50);   // turn the LED on (HIGH is the voltage level)
  }
  if (HeadLight_State == 1){
   analogWrite(Headlight_Left, 0);   // turn the LED on (HIGH is the voltage level)
   analogWrite(Headlight_Right,0);   // turn the LED on (HIGH is the voltage level)
  }
}

Looking at your code, I think you already know everything you need to know to achieve this.

As with the other features, you will need a variable to hold the time of the last change in the indicators' state, a variable to hold the interval until the next change of state, plus a couple of state variables, one to hold the state 0..3 you mentioned above and another to hold the current flashing on/off state. Also a counter variable to count down the number of flashes remaining.

Thanks again Paul, I feel that I am close to conceptualising the flash and your advice of saving the flash state makes more sense than what I was thinking so ill play with that concept for a bit.

Hello
You can use the BlinkWithOutDelay example of the IDE to build the function void Indicators(int blink);. The function parameter blink provides the information for the desired blink functionalty as specified already.

The indicators should have 4 states

    Off
    Left flash
    Right flash
    Both flash

Have a nice day and enjoy coding in C++.

Thanks Paulpaul , I am already abusing the Blink without delay example as the base for all of this code :slight_smile:

The BWOD example is the mother of all timers used inside the Arduino biotop. ::nerd_face:

I can imagine lol

Another approach would be to have a total of 7 states:

Off
Left flash, currently on
Right flash, currently on
Both flash, currently on
Left flash, currently off
Right flash, currently off
Both flash, currently off

Whether that will be easier than having 2 separate state variables, I'm not sure, maybe it won't make much difference.

that might give you an idea using OOP for blinking:

// Model car LED lighting control
// https://forum.arduino.cc/t/model-car-led-lighting-control-help-please/918210
// by noiasca
// 2021 10 25

// Tested pin settings by noiasca
constexpr byte frontLeftIndicator = 2;
constexpr byte rearLeftIndicator1 = 3;
constexpr byte rearLeftIndicator2 = 4;
constexpr byte frontRightIndicator = 5;
constexpr byte rearRightIndicator1 = 6;
constexpr byte rearRightIndicator2 = 7;
constexpr byte leftButton = A0;
constexpr byte rightButton = A1;
constexpr byte bothButton = A2;
constexpr byte offButton = A3;

/*
  // Alternative pin settings
  constexpr byte frontLeftIndicator = A0;
  constexpr byte rearLeftIndicator1 = A1;
  constexpr byte rearLeftIndicator2 = A2;
  constexpr byte frontRightIndicator = A3;
  constexpr byte rearRightIndicator1 = A4;
  constexpr byte rearRightIndicator2 = A5;

  constexpr byte leftButton = 2;
  constexpr byte rightButton = 3;
  constexpr byte bothButton = 4;
  constexpr byte offButton = 5;
*/

enum State {IDLE, LEFT, RIGHT, BOTH};   // the States for a state machine

class Indicator {
    State indicatorState;
    bool blinkState = false;
    unsigned long previousMillis;      // last blink timestamp
    const byte leftPin[3];             // GPIOs for the LEDs
    const byte rightPin[3];
    uint16_t onInterval;               // on time
  public:
    Indicator(byte left0, byte left1, byte left2, byte right0, byte right1, byte right2, uint16_t onInterval = 500) :
      leftPin {left0, left1, left2},
      rightPin {right0, right1, right2},
      onInterval {onInterval}
    {}
    void begin() {
      for (auto &pin : leftPin) pinMode(pin, OUTPUT);
      for (auto &pin : rightPin) pinMode(pin, OUTPUT);
    }
    void set(uint16_t _on) { // modify on/off times during runtime
      onInterval = _on;
    }
    void setIndicatorState(State _IndicatorState) {           
      if (indicatorState != _IndicatorState)
      {
        indicatorState = _IndicatorState;
        Serial.print(F("new State:")); Serial.println(indicatorState);
        for (auto &pin : leftPin) digitalWrite(pin, LOW);     // switch off indicators
        for (auto &pin : rightPin) digitalWrite(pin, LOW);
      }
    }
    void update() {
      if (indicatorState != IDLE ) {
        uint32_t currentMillis = millis();
        if (currentMillis - previousMillis >= onInterval) {
          previousMillis = currentMillis; // save the last time you blinked the LED
          blinkState = !blinkState;       // invert state
          byte newButtonState = LOW;
          if (blinkState) newButtonState = HIGH;
          switch (indicatorState)
          {
            case IDLE : ;   // intent no use of this case - avoid compiler warning
            case LEFT :
              for (auto &pin : leftPin) digitalWrite(pin, newButtonState);
              break;
            case RIGHT :
              for (auto &pin : rightPin) digitalWrite(pin, newButtonState);
              break;
            case BOTH :
              for (auto &pin : leftPin) digitalWrite(pin, newButtonState);
              for (auto &pin : rightPin) digitalWrite(pin, newButtonState);
              break;
          }
        }
      }
    }
};

// now create a indicator object with all 6 LEDs:
Indicator indicator(frontLeftIndicator, rearLeftIndicator1, rearLeftIndicator2, frontRightIndicator, rearRightIndicator1, rearRightIndicator2);

void setup() {
  Serial.begin(115200);
  randomSeed(analogRead(A7));
  indicator.begin();
  setupButton();
}

void setupButton()
{
  pinMode(leftButton, INPUT_PULLUP);
  pinMode(rightButton, INPUT_PULLUP);
  pinMode(bothButton, INPUT_PULLUP);
  pinMode(offButton, INPUT_PULLUP);
}

void handleButton()              // read buttons and change states accordingly
{
  if (digitalRead(offButton) == LOW) indicator.setIndicatorState(State::IDLE);
  if (digitalRead(leftButton) == LOW) indicator.setIndicatorState(LEFT);
  if (digitalRead(rightButton) == LOW) indicator.setIndicatorState(RIGHT);
  if (digitalRead(bothButton) == LOW) indicator.setIndicatorState(BOTH);
}

void handleRandom()              // change states randomly
{
  uint32_t rnd = random(42000);  // the higher the lesser changes by time
  if (rnd == 42)
  {
    byte newStateNum = random(4);  // will give 0..3 as we have currently 4 states
    switch (newStateNum)           // convert a number into states for the enumeration
    {
      case 1 : indicator.setIndicatorState(LEFT); break;
      case 2 : indicator.setIndicatorState(RIGHT); break;
      case 3 : indicator.setIndicatorState(BOTH); break;
      default : indicator.setIndicatorState(IDLE);
    }
  }
}

void loop() {
  indicator.update(); // must be called repeatedly
  handleButton();     // a handler should be able to set different states
  handleRandom();     
}

You can adopt/remove the random handler for testing with buttons.
As you see I'm using different pins, but it should be straight forward to adopt this.

Thank heaps Noiasca,
Wow this looks way more complicated than I can understand. I have never done anything with classes. but I will take a look now.

With Arduino you are using classes anyway. So why not start writing your own classes.
If you break it down into its componets it's nothing special.

First, the class needs some member variables.
Then just jump over that constructor (the lines after public:) - just take it as it is for the moment.

The begin method initializes the pins - like you do it in setup for usual.
The update method is build based on blink without delay.

The others member functions are not complicated at all ... one sets the on/off time and the setIndicatorState is the "interface"/API to the outer world - it enables other components to switch on/off the blinking.

Additionally we are using an enumeration, it's more or less all possible states you have defined - "OFF" (I prefere IDLE), LEFT, RIGHT, BOTH - it just avoids some magical numbers in the sketch.

so beside this funny constructor it's really not complicated.

If you walk through the sketch you will find 90% of things you already know.
The other 10% can be asked :wink:
have fun.

thanks heaps I will spend some time to understand if for sure as I would like to improve.

for now I have to sleep... its late here in New Zealand and I cant seem to visualise how to get my current code working...

here it is if anyone feels like commenting please and thanks :slight_smile:

#include <FastLED.h>

#define LED_PIN     2
#define NUM_LEDS    2
#define BRIGHTNESS  200
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100


// Engine LEDs
#define REDLED_1 8
#define REDLED_2 7
#define REDLED_3 6
#define REDLED_4 5
#define REDLED_5 4
#define REDLED_6 9

#define Headlight_Left 10       //PWM
#define Headlight_Right 11      //PWM

// Indicator LEDs
#define Front_Left_Indicator A0
#define Rear_Left_Indicator1 A1
#define Rear_Left_Indicator2 A2

#define Front_Right_Indicator A3
#define Rear_Right_Indicator1 A4
#define Rear_Right_Indicator2 A5

// Spare LEDs
#define Spare1 13
#define Spare2 A6

//Input Switches 
#define Door_switch 3

//WS2812B LEDs x2
#define Interior 2              

unsigned long previousMillis = 0;                  // will store last time Engine LED was updated
unsigned long Headlight_previousMillis = 0;        // will store last time LED was updated
unsigned long Indicator_previousMillis = 0;        // will store last time LED was updated
unsigned long Indicator_Flash_previousMillis = 0;  // will store last time LED was updated

const long engine_RPM_interval = 20;              // interval at which to spin red engine LEDs
const long Headlight_interval = 2000;             // interval at which to change Headlight state  
const long Indicator_interval = 5000;             // interval at which to change Indicator mode
const long Indicator_Flash_interval = 500;        // interval at which to change Indicator state 

byte    pins[6] = {REDLED_6,REDLED_5,REDLED_4,REDLED_3,REDLED_2,REDLED_1};

int Door_State = 0; 
int HeadLight_State = 0; 
int Indicator = 0;
int Indicator_Mode = 0; 
int Indicator_Count = 0; 
int Indicator_State = 0; 
int Indicator_min_flash = 5;
int Indicator_max_flash = 20;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600); 
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(BRIGHTNESS);
  
  pinMode(Door_switch, INPUT);
  pinMode(REDLED_1, OUTPUT);
  pinMode(REDLED_2, OUTPUT);
  pinMode(REDLED_3, OUTPUT);
  pinMode(REDLED_4, OUTPUT);
  pinMode(REDLED_5, OUTPUT);
  pinMode(REDLED_6, OUTPUT);
  pinMode(Headlight_Left, OUTPUT);
  pinMode(Headlight_Right, OUTPUT);
  pinMode(Front_Left_Indicator, OUTPUT);
  pinMode(Rear_Left_Indicator1, OUTPUT);
  pinMode(Rear_Left_Indicator2, OUTPUT);
  pinMode(Front_Right_Indicator, OUTPUT);
  pinMode(Rear_Right_Indicator1, OUTPUT);
  pinMode(Rear_Right_Indicator2, OUTPUT);
  //pinMode(Spare1, OUTPUT);
  //pinMode(Spare2, OUTPUT);
  
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
unsigned long currentMillis = millis();

// Keep engine LEDs spinning
if (currentMillis - previousMillis >= engine_RPM_interval){
  previousMillis = currentMillis;
  Cycle_Engine_Leds();
}

// Choose Headlight State
if (currentMillis - Headlight_previousMillis >= Headlight_interval){
  Headlight_previousMillis = currentMillis;
  HeadLight_State = random(1,4);
  Serial.print("Headlight State = "); 
  Serial.println(HeadLight_State); 
}
HeadLights();

// Choose Indicator State
if (currentMillis - Indicator_previousMillis >= Indicator_interval){
  Indicator_previousMillis = currentMillis;
  if (Indicator_State == 0){
  Indicator_Mode = 1;//random(1,5);
  Indicator_Count = random(Indicator_min_flash,Indicator_max_flash);
  Indicator_State = 1; 
  Serial.print("Indicator State = "); 
  Serial.println(Indicator_State); 
  }
}
if (Indicator_State == 1){
Indicators(); 
}

// Read input switch to see if a car door is open
Door_State = digitalRead(Door_switch);
InteriorLights();






}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//int Indicator = 0;
//int Indicator_Mode = 0;         // left right or both 
//int Indicator_Count = 0;        // number of flashes remaining
//int Indicator_State = 0;        // on or off 
//int Indicator_min_flash = 5;
//int Indicator_max_flash = 20;

void Indicators(){
 unsigned long currentMillis = millis();
 if (currentMillis - Indicator_Flash_previousMillis >= Indicator_Flash_interval){
  Indicator_Flash_previousMillis = currentMillis;
 if (Indicator_Count <= Indicator_min_flash){
  Indicator_State = 0;                          // lets the code go back and choose new indicator parameters.  
  }else
  {
   if (Indicator_Mode == 1){                    // flash left 
    if (Indicator_State == 0){                  // Indicator off
      digitalWrite(Front_Left_Indicator, LOW);
      digitalWrite(Rear_Left_Indicator1, LOW);
      digitalWrite(Rear_Left_Indicator2, LOW);
      Indicator_State = 1;
      Indicator_Count--;
    }
    if (Indicator_State == 1){                  // Indicator on
      digitalWrite(Front_Left_Indicator, HIGH);
      digitalWrite(Rear_Left_Indicator1, HIGH);
      digitalWrite(Rear_Left_Indicator2, HIGH);
      Indicator_State = 0;
      Indicator_Count--;
    }
    
  }
   if (Indicator_Mode == 2){                    // flash right 
    
   } 
   if (Indicator_Mode == 3){                    // flash both
    
  }
   if (Indicator_Mode == 4){
    
   } 
  }
 }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Cycle_Engine_Leds(){
  static byte led = 0;
  digitalWrite(pins[led], LOW);
  if (++led >= sizeof(pins)/sizeof(pins[0])) led = 0;
  digitalWrite(pins[led], HIGH);
}  


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void InteriorLights(){
  if (Door_State == LOW){
    for( int i = 0; i < NUM_LEDS; ++i){
        leds[i] = CRGB::Blue;   
        FastLED.show();
    }
  }
  if (Door_State == HIGH){
    for( int i = 0; i < NUM_LEDS; ++i){
        leds[i] = CRGB::Black;  
        FastLED.show();
    }
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void HeadLights(){
  if (HeadLight_State == 3){
    analogWrite(Headlight_Left, 255);   // turn the LED on (HIGH is the voltage level)
    analogWrite(Headlight_Right, 255);   // turn the LED on (HIGH is the voltage level)
  }
  if (HeadLight_State == 2){
    analogWrite(Headlight_Left, 50);   // turn the LED on (HIGH is the voltage level)
    analogWrite(Headlight_Right,50);   // turn the LED on (HIGH is the voltage level)
  }
  if (HeadLight_State == 1){
   analogWrite(Headlight_Left, 0);   // turn the LED on (HIGH is the voltage level)
   analogWrite(Headlight_Right,0);   // turn the LED on (HIGH is the voltage level)
  }
}











  

I think it's much simplier if you just do one thing after the other.
Strip down your sketch to blinking only.
As I was lazy, I'm using mine as base:

// Model car LED lighting control
// https://forum.arduino.cc/t/model-car-led-lighting-control-help-please/918210
// by noiasca
// 2021 10 25 old fashioned - not so nice "without" OOP

// Tested pin settings by noiasca
constexpr byte frontLeftIndicator = 2;
constexpr byte rearLeftIndicator1 = 3;
constexpr byte rearLeftIndicator2 = 4;
constexpr byte frontRightIndicator = 5;
constexpr byte rearRightIndicator1 = 6;
constexpr byte rearRightIndicator2 = 7;

/*
  // Alternative pin settings
  constexpr byte frontLeftIndicator = A0;
  constexpr byte rearLeftIndicator1 = A1;
  constexpr byte rearLeftIndicator2 = A2;
  constexpr byte frontRightIndicator = A3;
  constexpr byte rearRightIndicator1 = A4;
  constexpr byte rearRightIndicator2 = A5;
*/

enum State {IDLE,   // will get 0
            LEFT,   // 1
            RIGHT,  // 2
            BOTH    // 3 ... but honestly - we don't care. We will use IDLE/LEFT/RIGHT/BOTH
           };   // the States for a state machine
State indicatorState;                                   // a variable to store the state of the indicator
bool blinkState = false;                                // on or off
constexpr uint16_t onInterval = 500;                    // on time
const byte leftPin[] {frontLeftIndicator, rearLeftIndicator1, rearLeftIndicator2};
const byte rightPin[] {frontRightIndicator, rearRightIndicator1, rearRightIndicator2};

void beginIndicator() {
  for (auto &pin : leftPin) pinMode(pin, OUTPUT);
  for (auto &pin : rightPin) pinMode(pin, OUTPUT);
}

void setIndicatorState(State _IndicatorState) {
  if (indicatorState != _IndicatorState)
  {
    indicatorState = _IndicatorState;
    Serial.print(F("new State:")); Serial.println(indicatorState);
    for (auto &pin : leftPin) digitalWrite(pin, LOW);     // switch off indicators
    for (auto &pin : rightPin) digitalWrite(pin, LOW);
  }
}

void updateIndicator() {
  static uint32_t previousMillis = 0;       // must survive - hence static
  if (indicatorState != IDLE ) {
    uint32_t currentMillis = millis();
    if (currentMillis - previousMillis >= onInterval) {
      previousMillis = currentMillis;       // save the last time you blinked the LED
      blinkState = !blinkState;             // invert state
      byte newButtonState = LOW;
      if (blinkState) newButtonState = HIGH;
      switch (indicatorState)
      {
        case IDLE : ;   // intent no use of this case - avoid compiler warning
        case LEFT :
          for (auto &pin : leftPin) digitalWrite(pin, newButtonState);
          break;
        case RIGHT :
          for (auto &pin : rightPin) digitalWrite(pin, newButtonState);
          break;
        case BOTH :
          for (auto &pin : leftPin) digitalWrite(pin, newButtonState);
          for (auto &pin : rightPin) digitalWrite(pin, newButtonState);
          break;
      }
    }
  }
}

void handleRandom()                // change states randomly
{
  uint32_t rnd = random(42000);    // the higher the lesser changes by time
  if (rnd == 42)
  {
    byte newStateNum = random(4);  // will give 0..3 as we have currently 4 states
    switch (newStateNum)           // convert a number into states for the enumeration
    {
      case 1 : setIndicatorState(LEFT); break;
      case 2 : setIndicatorState(RIGHT); break;
      case 3 : setIndicatorState(BOTH); break;
      default : setIndicatorState(IDLE);
    }
  }
}

void setup() {
  Serial.begin(115200);
  randomSeed(analogRead(A7));
  beginIndicator();
}

void loop() {
  updateIndicator(); // must be called repeatedly
  handleRandom();
}

Good morning to New Zealand

I´ve made a class-less sketch proposal for your model car.
This sketch uses some extremly useful OOP instructions like STRUC, ARRAY and Range-based for loop to get a smart solution. Thus will generate a small and good maintainable sketch using the mother of all timers.
The Sketch has been tested and you may have to change the pin addresses for the flash lights. A small HMI is added to play around with the sketch, too.

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  https://forum.arduino.cc/t/model-car-led-lighting-control-help-please/918210
*/
#define ProjectName "Model car LED lighting control help please"
// hardware and timer settings
constexpr byte Front_Left_Indicator {2};       // portPin o---|220|---|LED|---GND
constexpr byte Rear_Left_Indicator1  {3};     // portPin o---|220|---|LED|---GND
constexpr byte Rear_Left_Indicator2 {4};      // portPin o---|220|---|LED|---GND
constexpr byte Front_Right_Indicator {5};       // portPin o---|220|---|LED|---GND
constexpr byte Rear_Right_Indicator1  {6};     // portPin o---|220|---|LED|---GND
constexpr byte Rear_Right_Indicator2  {7};      // portPin o---|220|---|LED|---GND
constexpr unsigned long BlinkDuration {500}; // times in msec
constexpr unsigned long TestDuration {250}; // times in msec
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
struct TIMER {              // has the following members
  unsigned long duration;   // memory for interval time
  bool repeat_;             // control for blinking
  bool control_;            // control for start/stop
  unsigned long stamp;      // memory for actual time
};
struct FLASHER {
  TIMER timer;
  byte indicators[3];
} flasher [] {
  {BlinkDuration, true, true, 0, {Front_Left_Indicator, Rear_Left_Indicator1, Rear_Left_Indicator2}},
  {BlinkDuration, true, true, 0, {Front_Right_Indicator, Rear_Right_Indicator1, Rear_Right_Indicator2}},
};
// ------------------ USER FUNCTIONS ---------------
void startTimer(TIMER &timer) {
  timer.control_ = true;
  timer.stamp = currentTime;
}
void stoppTimer(TIMER &timer) {
  timer.control_ = false;
}
bool checkTimer(TIMER & time_) {  // generic time handler using TIME struct
  if (currentTime - time_.stamp >= time_.duration && time_.control_) { // the mother of all timers
    if (time_.repeat_) time_.stamp = currentTime;
    else time_.control_ = false;
    return true;
  } else return false;
}
enum {Left, Right, All, Off};
void indicators() {
  switch ((Serial.read() & 0xf) - 1) {
    case Left:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      for (auto indicator : flasher[Left].indicators) digitalWrite(indicator, HIGH);
      startTimer(flasher[Left].timer);
      stoppTimer(flasher[Right].timer);
      break;
    case Right:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      for (auto indicator : flasher[Right].indicators) digitalWrite(indicator, HIGH);
      stoppTimer(flasher[Left].timer);
      startTimer(flasher[Right].timer);
      break;
    case All:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      for (auto indicator : flasher[Left].indicators) digitalWrite(indicator, HIGH);
      for (auto indicator : flasher[Right].indicators) digitalWrite(indicator, HIGH);
      startTimer(flasher[Left].timer);
      startTimer(flasher[Right].timer);
      break;
    case Off:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      stoppTimer(flasher[Left].timer);
      stoppTimer(flasher[Right].timer);
      break;
  }
}
// ------------------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
  for (auto &flash : flasher) for (auto indicator : flash.indicators) pinMode(indicator, OUTPUT);
  Serial.println(F("check flash lights"));
  for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, HIGH), delay(TestDuration);
  for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW), delay(TestDuration);
  Serial.println(F("[1] flash LEFT - [2] flash RIGHT - [3] flash ALL - [4] flash OFF"));
}
void loop () {
  currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  if (Serial.available() > 0) indicators();
  for (auto &flash : flasher) {
    if (checkTimer(flash.timer))
      for (auto indicator : flash.indicators) digitalWrite(indicator, !digitalRead(indicator));
  }
}

Have a nice day and enjoy coding in C++.

Thanks Paul. That works really well! Now I will hack it to pieces to see if I can get it to work without keyboard inputs and add the other lights.

Hi Paul,

Hope you are well.

I have managed to string the headlights, interior lights and 'engine' lights together and make it work. however I am having trouble converting the indicators to work randomly as opposed to needing a keyboard input to change state.

My thinking was to simply change the indicator switch statement to work on a variable that gets randomly changed from 1-4 in another timer inside the mian loop called "// Choose Indicator State"

Currently I don't get any changes but both left and right side indicators are flashing.

Can you please tell me what I have missed?

thanks again :slight_smile:

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  https://forum.arduino.cc/t/model-car-led-lighting-control-help-please/918210
*/


#include <FastLED.h>
#define LED_PIN     2
#define NUM_LEDS    2
#define BRIGHTNESS  200
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100

//Input Switches 
#define Door_switch 3

//WS2812B LEDs x2
#define Interior 2 

// Engine LEDs
#define REDLED_1 8
#define REDLED_2 7
#define REDLED_3 6
#define REDLED_4 5
#define REDLED_5 4
#define REDLED_6 9

#define Headlight_Left 10       //PWM
#define Headlight_Right 11      //PWM

int Door_State = 0; 
int HeadLight_State = 0; 
int Indicator_State = 0; 

unsigned long previousMillis = 0;                  // will store last time Engine LED was updated
unsigned long Headlight_previousMillis = 0;        // will store last time LED was updated
unsigned long Indicator_previousMillis = 0;        // will store last time LED was updated
unsigned long Indicator_Flash_previousMillis = 0;  // will store last time LED was updated

const long engine_RPM_interval = 20;              // interval at which to spin red engine LEDs
const long Headlight_interval = 2000;             // interval at which to change Headlight state  
const long Indicator_interval = 5000;             // interval at which to change Indicator mode
const long Indicator_Flash_interval = 500;        // interval at which to change Indicator state 

byte    pins[6] = {REDLED_6,REDLED_5,REDLED_4,REDLED_3,REDLED_2,REDLED_1};


#define ProjectName "Model car LED lighting"
// hardware and timer settings
constexpr byte Front_Left_Indicator {A0};       // portPin o---|220|---|LED|---GND
constexpr byte Rear_Left_Indicator1  {A1};     // portPin o---|220|---|LED|---GND
constexpr byte Rear_Left_Indicator2 {A2};      // portPin o---|220|---|LED|---GND
constexpr byte Front_Right_Indicator {A3};       // portPin o---|220|---|LED|---GND
constexpr byte Rear_Right_Indicator1  {A4};     // portPin o---|220|---|LED|---GND
constexpr byte Rear_Right_Indicator2  {A5};      // portPin o---|220|---|LED|---GND
constexpr unsigned long BlinkDuration {400}; // times in msec
constexpr unsigned long TestDuration {250}; // times in msec
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
struct TIMER {              // has the following members
  unsigned long duration;   // memory for interval time
  bool repeat_;             // control for blinking
  bool control_;            // control for start/stop
  unsigned long stamp;      // memory for actual time
};
struct FLASHER {
  TIMER timer;
  byte indicators[3];
} flasher [] {
  {BlinkDuration, true, true, 0, {Front_Left_Indicator, Rear_Left_Indicator1, Rear_Left_Indicator2}},
  {BlinkDuration, true, true, 0, {Front_Right_Indicator, Rear_Right_Indicator1, Rear_Right_Indicator2}},
};
// ------------------ USER FUNCTIONS ---------------
void startTimer(TIMER &timer) {
  timer.control_ = true;
  timer.stamp = currentTime;
}
void stoppTimer(TIMER &timer) {
  timer.control_ = false;
}
bool checkTimer(TIMER & time_) {  // generic time handler using TIME struct
  if (currentTime - time_.stamp >= time_.duration && time_.control_) { // the mother of all timers
    if (time_.repeat_) time_.stamp = currentTime;
    else time_.control_ = false;
    return true;
  } else return false;
}
enum {Left, Right, All, Off};
void indicators() {
  //switch ((Serial.read() & 0xf) - 1) {
  switch (Indicator_State) {
    case 1:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      for (auto indicator : flasher[Left].indicators) digitalWrite(indicator, HIGH);
      startTimer(flasher[Left].timer);
      stoppTimer(flasher[Right].timer);
      break;
    case 2:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      for (auto indicator : flasher[Right].indicators) digitalWrite(indicator, HIGH);
      stoppTimer(flasher[Left].timer);
      startTimer(flasher[Right].timer);
      break;
    case 3:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      for (auto indicator : flasher[Left].indicators) digitalWrite(indicator, HIGH);
      for (auto indicator : flasher[Right].indicators) digitalWrite(indicator, HIGH);
      startTimer(flasher[Left].timer);
      startTimer(flasher[Right].timer);
      break;
    case 4:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      stoppTimer(flasher[Left].timer);
      stoppTimer(flasher[Right].timer);
      break;
  }
}
// ------------------------------------------------
void setup() {
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(BRIGHTNESS);
  pinMode(Door_switch, INPUT);
  pinMode(REDLED_1, OUTPUT);
  pinMode(REDLED_2, OUTPUT);
  pinMode(REDLED_3, OUTPUT);
  pinMode(REDLED_4, OUTPUT);
  pinMode(REDLED_5, OUTPUT);
  pinMode(REDLED_6, OUTPUT);
  pinMode(Headlight_Left, OUTPUT);
  pinMode(Headlight_Right, OUTPUT);
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
  for (auto &flash : flasher) for (auto indicator : flash.indicators) pinMode(indicator, OUTPUT);
  Serial.println(F("check flash lights"));
  for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, HIGH), delay(TestDuration);
  for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW), delay(TestDuration);
  Serial.println(F("[1] flash LEFT - [2] flash RIGHT - [3] flash ALL - [4] flash OFF"));
}
void loop () {
  unsigned long currentMillis = millis();
  currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  
  // Keep engine LEDs spinning
  if (currentMillis - previousMillis >= engine_RPM_interval){
  previousMillis = currentMillis;
  Cycle_Engine_Leds();
  }

// Choose Headlight State
if (currentMillis - Headlight_previousMillis >= Headlight_interval){
  Headlight_previousMillis = currentMillis;
  HeadLight_State = random(1,4);
  Serial.print("Headlight State = "); 
  Serial.println(HeadLight_State); 
}
HeadLights();

// Choose Indicator State
if (currentMillis - Indicator_previousMillis >= Indicator_interval){
  Indicator_previousMillis = currentMillis;
  Indicator_State = random(1,5);
  Serial.print("Indicator State = "); 
  Serial.println(Indicator_State); 
}




  if (Serial.available() > 0) indicators();
  for (auto &flash : flasher) {
    if (checkTimer(flash.timer))
      for (auto indicator : flash.indicators) digitalWrite(indicator, !digitalRead(indicator));
  }

// Read input switch to see if a car door is open
Door_State = digitalRead(Door_switch);
InteriorLights();


  
}



////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Cycle_Engine_Leds(){
  static byte led = 0;
  digitalWrite(pins[led], LOW);
  if (++led >= sizeof(pins)/sizeof(pins[0])) led = 0;
  digitalWrite(pins[led], HIGH);
}  



////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void InteriorLights(){
  if (Door_State == LOW){
    for( int i = 0; i < NUM_LEDS; ++i){
        leds[i] = CRGB::Blue;   
        FastLED.show();
    }
  }
  if (Door_State == HIGH){
    for( int i = 0; i < NUM_LEDS; ++i){
        leds[i] = CRGB::Black;  
        FastLED.show();
    }
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void HeadLights(){
  if (HeadLight_State == 3){
    analogWrite(Headlight_Left, 255);   // turn the LED on (HIGH is the voltage level)
    analogWrite(Headlight_Right, 255);   // turn the LED on (HIGH is the voltage level)
  }
  if (HeadLight_State == 2){
    analogWrite(Headlight_Left, 50);   // turn the LED on (HIGH is the voltage level)
    analogWrite(Headlight_Right,50);   // turn the LED on (HIGH is the voltage level)
  }
  if (HeadLight_State == 1){
   analogWrite(Headlight_Left, 0);   // turn the LED on (HIGH is the voltage level)
   analogWrite(Headlight_Right,0);   // turn the LED on (HIGH is the voltage level)
  }
}

greetings,

I still cant understand how to get the sketch to ignore the serial input that Paul made to control the indicator modes (left flash, right flash, both flash) input and just randomly choose one mode every few seconds.

I am convinced that changing the first line the the indicators() function to ignore the serial input and just look at the randomly selected Indicator_Mode should work but it doesn't.

any help would be really appreciated

Here is my latest complete code that works and handles all the inputs and outputs correctly except for the indicators.

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  https://forum.arduino.cc/t/model-car-led-lighting-control-help-please/918210
*/

#define ProjectName "Model car LED lighting"

#include <FastLED.h>


#define LED_PIN     2
#define NUM_LEDS    2
#define BRIGHTNESS  200
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100

//Input Switches 
//#define Door_switch 
#define Door_switch 11  //12
#define Trunk_switch 3 //11

//WS2812B LEDs x2
#define Interior 2 

// Engine LEDs
#define REDLED_1 9
#define REDLED_2 8
#define REDLED_3 6
#define REDLED_4 7
#define REDLED_5 5
#define REDLED_6 4
//byte    pins[6] = {9,8,6,7,5,4};



#define Headlights 10       //PWM

#define Brake_Lights 13


int Door_State = 0; 
int Trunk_State = 0; 
int HeadLight_State = 0; 
int Indicator_Mode = 0; 

unsigned long previousMillis = 0;                  // will store last time Engine LED was updated
unsigned long Headlight_previousMillis = 0;        // will store last time LED was updated
unsigned long Indicator_previousMillis = 0;        // will store last time LED was updated
unsigned long Indicator_Flash_previousMillis = 0;  // will store last time LED was updated

const long engine_RPM_interval = 20;              // interval at which to spin red engine LEDs
const long Headlight_interval = 2000;             // interval at which to change Headlight state  
const long Indicator_interval = 5000;             // interval at which to change Indicator mode
const long Indicator_Flash_interval = 500;        // interval at which to change Indicator state 

byte    pins[6] = {REDLED_6,REDLED_5,REDLED_4,REDLED_3,REDLED_2,REDLED_1};


// hardware and timer settings
constexpr byte Front_Left_Indicator {A0};       // portPin o---|220|---|LED|---GND
constexpr byte Rear_Left_Indicator1  {A1};     // portPin o---|220|---|LED|---GND
constexpr byte Rear_Left_Indicator2 {A2};      // portPin o---|220|---|LED|---GND
constexpr byte Front_Right_Indicator {A3};       // portPin o---|220|---|LED|---GND
constexpr byte Rear_Right_Indicator1  {A4};     // portPin o---|220|---|LED|---GND
constexpr byte Rear_Right_Indicator2  {A5};      // portPin o---|220|---|LED|---GND

constexpr unsigned long BlinkDuration {400}; // times in msec
constexpr unsigned long TestDuration {250}; // times in msec
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
struct TIMER {              // has the following members
  unsigned long duration;   // memory for interval time
  bool repeat_;             // control for blinking
  bool control_;            // control for start/stop
  unsigned long stamp;      // memory for actual time
};
struct FLASHER {
  TIMER timer;
  byte indicators[3];
} flasher [] {
  {BlinkDuration, true, true, 0, {Front_Left_Indicator, Rear_Left_Indicator1, Rear_Left_Indicator2}},
  {BlinkDuration, true, true, 0, {Front_Right_Indicator, Rear_Right_Indicator1, Rear_Right_Indicator2}},
};
// ------------------ USER FUNCTIONS ---------------
void startTimer(TIMER &timer) {
  timer.control_ = true;
  timer.stamp = currentTime;
}
void stoppTimer(TIMER &timer) {
  timer.control_ = false;
}
bool checkTimer(TIMER & time_) {  // generic time handler using TIME struct
  if (currentTime - time_.stamp >= time_.duration && time_.control_) { // the mother of all timers
    if (time_.repeat_) time_.stamp = currentTime;
    else time_.control_ = false;
    return true;
  } else return false;
}
enum {Left, Right, All, Off};
void Indicators() {
  //switch ((Serial.read() & 0xf) - 1) {    // use this line for keyboard control of indicators over serial...
  switch (Indicator_Mode) {
    case 1:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      for (auto indicator : flasher[Left].indicators) digitalWrite(indicator, HIGH);
      startTimer(flasher[Left].timer);
      stoppTimer(flasher[Right].timer);
      break;
    case 2:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      for (auto indicator : flasher[Right].indicators) digitalWrite(indicator, HIGH);
      stoppTimer(flasher[Left].timer);
      startTimer(flasher[Right].timer);
      break;
    case 3:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      for (auto indicator : flasher[Left].indicators) digitalWrite(indicator, HIGH);
      for (auto indicator : flasher[Right].indicators) digitalWrite(indicator, HIGH);
      startTimer(flasher[Left].timer);
      startTimer(flasher[Right].timer);
      break;
    case 4:
      for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW);
      stoppTimer(flasher[Left].timer);
      stoppTimer(flasher[Right].timer);
      break;
  }
}
// ------------------------------------------------
void setup() {
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(BRIGHTNESS);
  pinMode(Door_switch, INPUT_PULLUP);
  pinMode(Trunk_switch, INPUT_PULLUP);
  pinMode(REDLED_1, OUTPUT);
  pinMode(REDLED_2, OUTPUT);
  pinMode(REDLED_3, OUTPUT);
  pinMode(REDLED_4, OUTPUT);
  pinMode(REDLED_5, OUTPUT);
  pinMode(REDLED_6, OUTPUT);
  pinMode(Headlights, OUTPUT);
  pinMode(Brake_Lights, OUTPUT);
  
  
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  //pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
  for (auto &flash : flasher) for (auto indicator : flash.indicators) pinMode(indicator, OUTPUT);
  Serial.println(F("check flash lights"));
  for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, HIGH), delay(TestDuration);
  for (auto &flash : flasher) for (auto indicator : flash.indicators) digitalWrite(indicator, LOW), delay(TestDuration);
  Serial.println(F("[1] flash LEFT - [2] flash RIGHT - [3] flash ALL - [4] flash OFF"));
}
void loop () {
  unsigned long currentMillis = millis();
  currentTime = millis();

  
  // Keep engine LEDs spinning if trunk open
  if (currentMillis - previousMillis >= engine_RPM_interval){
  previousMillis = currentMillis;
  if (Trunk_State == LOW){
    Cycle_Engine_Leds();
  }
  if (Trunk_State == HIGH){
    Turn_Off_Engine_Leds();
  }
  }

// Choose Headlight State
if (currentMillis - Headlight_previousMillis >= Headlight_interval){
  Headlight_previousMillis = currentMillis;
  HeadLight_State = random(1,4);
  //Serial.print("Headlight State = "); 
  //Serial.println(HeadLight_State); 
}

HeadLights();                                                         /////////////////////////////////////////////
//Indicators();
// Choose Indicator State
if (currentMillis - Indicator_previousMillis >= Indicator_interval){
  Indicator_previousMillis = currentMillis;
  Indicator_Mode = random(1,5);
  Serial.print("Indicator State = "); 
  Serial.println(Indicator_Mode); 
}

  //if (Serial.available() > 0) Indicators();
  Indicators();
  for (auto &flash : flasher) {
    if (checkTimer(flash.timer))
      for (auto indicator : flash.indicators) digitalWrite(indicator, !digitalRead(indicator));
  }

// Read input switch to see if a car door is open
Door_State = digitalRead(Door_switch);
  //Serial.print("Door State = "); 
  //Serial.println(Door_State); 

// Read input switch to see if a Trunk lid is open
Trunk_State = digitalRead(Trunk_switch);
  //Serial.print("Trunk State = "); 
  //Serial.println(Trunk_State); 

  
InteriorLights();


  
}



////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Cycle_Engine_Leds(){
  static byte led = 0;
  digitalWrite(pins[led], LOW);
  if (++led >= sizeof(pins)/sizeof(pins[0])) led = 0;
  digitalWrite(pins[led], HIGH);
}  

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Turn_Off_Engine_Leds(){
    digitalWrite(pins[0], LOW);
    digitalWrite(pins[1], LOW);
    digitalWrite(pins[2], LOW);
    digitalWrite(pins[3], LOW);
    digitalWrite(pins[4], LOW);
    digitalWrite(pins[5], LOW);
  }


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void InteriorLights(){
  if (Door_State == LOW){
    for( int i = 0; i < NUM_LEDS; ++i){
        leds[i] = CRGB::Yellow;   
        FastLED.show();
    }
  }
  if (Door_State == HIGH){
    for( int i = 0; i < NUM_LEDS; ++i){
        leds[i] = CRGB::Black;  
        FastLED.show();
    }
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void HeadLights(){
  if (HeadLight_State == 3){
    analogWrite(Headlights, 255);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(Brake_Lights, HIGH);
  }
  if (HeadLight_State == 2){
    analogWrite(Headlights, 50);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(Brake_Lights, HIGH);
  }
  if (HeadLight_State == 1){
   analogWrite(Headlights, 0);   // turn the LED on (HIGH is the voltage level)
   digitalWrite(Brake_Lights, LOW);
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.