Finished: Car Indicator Light (Turn Signals and Brake Light)

I searched and searched but could not find anywhere that someone had already coded a controller for a cars indicator lights , so I (after a ridiculous amount of time and effort, seen here: (Resolved) Continue running one part of code while still checking other inputs? - #56 by loudboy - Programming Questions - Arduino Forum) I made one. This is my first full fledged sketch, so it may not be the prettiest coding out there but it's mine and I like it! I'm sure there are all sorts of tricks to make this more streamlined and simpler, but I do not know of them (yet). Feel free to make suggestions and alter this, of course. Huge thanks to Nick Gammon, AWOL, CrossRoads, and everyone else who had the patience to help me!

Vehicle Indicator Lights:

//A sketch to control the indicator light of a vehicle, including right, left blinker and brake light.



const int BrakeSwitch = 6;
const int BrakeLight = 9;
const int RightSwitch = 7;
const int RightBlink = 10;
const int LeftSwitch = 8;
const int LeftBlink = 11;


int BrakeSwitchVal = 0; //Monitors the value of the Brake switch, whether 1 or 0 (HIGH or LOW). Initially set at 0(LOW)
int previousBrakeSwitchVal = 0; //Monitors the value of the Brake switch from last time it was checked. Initally set at 0(LOW)

long RightSwitchMillis = 0; //Time stamp for when the switch last went to 1(HIGH). Initially set at 0(LOW)
int RightSwitchVal = 0; //Monitors the value of the Right Blinker switch, whether 1 or 0 (HIGH or LOW). Initially set at 0(LOW)
int previousRightSwitchVal = 0; //Monitors the value of the Right Blinker switch from last time it was checked. Initally set at 0(LOW)
int RightBlinkState = LOW; //State of the Right Blinker Light. Initally set to LOW(Off)
long RightFlashMillis = 0; //Time stamp for when rapid flash last went to 1(LOW), initially set at O(LOW)
long LeftSwitchMillis = 0;

int LeftSwitchVal = 0;
int previousLeftSwitchVal = 0;
int LeftBlinkState = LOW;
long LeftFlashMillis = 0;

long interval = 1000; // interval at which to blink (milliseconds)



void setup() 
{
  pinMode(BrakeLight, OUTPUT); 
  pinMode(BrakeSwitch, INPUT);
  
  pinMode(RightBlink, OUTPUT); 
  pinMode(RightSwitch, INPUT); 
  
  pinMode(LeftBlink, OUTPUT); 
  pinMode(LeftSwitch, INPUT); 
}



void loop()
{
  unsigned long currentMillis = millis(); //initiallizes the clock 
  
//Brake Light Code////////////////////////////////////////////////////////////////////////////////////////
BrakeSwitchVal = digitalRead(BrakeSwitch); //read and store brake switch status
  if ((BrakeSwitchVal == HIGH) && (previousBrakeSwitchVal == LOW)) //compares current switch status to previous status
   {
    previousBrakeSwitchVal = BrakeSwitchVal; //sets the current status as the old status in preparation for the next check
   }
       if ((BrakeSwitchVal == HIGH)) //checks if the brake switch is pressed and how long it has been pressed for
         {
         digitalWrite (BrakeLight, HIGH); //if the switch is being pressed on and it has been pressed for longer than one half second, the brake light goes on full and steady.
         }
         
        if (BrakeSwitchVal == LOW)
         {
           previousBrakeSwitchVal = BrakeSwitchVal; //makes sure the previousBrakeSwitch value is reset of off when the switch is in fact off.
           digitalWrite (BrakeLight, LOW); //Overall, if the brake switch is off, keep the brake light off.   
         }


//Right Blinker Code/////////////////////////////////////////////////////////////////////////////////////////////////////////
  RightSwitchVal = digitalRead(RightSwitch); //read and store Right Blinker switch status
  
  if ((RightSwitchVal == HIGH) && (previousRightSwitchVal == LOW)) //compares current switch status to previous status
    {
     previousRightSwitchVal = RightSwitchVal; //sets the current status as the old status in preparation for the next check
     RightSwitchMillis = currentMillis; //sets a time stamp at the current time
    }

    if((RightSwitchVal == HIGH) && (currentMillis - RightFlashMillis > interval/4)) //if the Right Blink switch is HIGH but less than half a second has elapsed,                                                                                                                                 
         {                                                                                                                                 // this checks if the Right Blink switch is HIGH and more than one-fifteith
        RightFlashMillis = currentMillis; // saves current time                                                                             //of a second has elapsed since the last time previousMillis10 was reset.
         
         if (RightBlinkState == LOW)
            RightBlinkState = HIGH;
          else
            RightBlinkState = LOW; //if more than one-fiftieth of a second has elapsed since the time stamp was reset, reverse the state of the Right Blinker.
            
          digitalWrite(RightBlink, RightBlinkState); // set the Right Blinker with the state of the variable:
          }
     
          
    if (RightSwitchVal == LOW) //checks if the Blinker switch is still on or not. If it is off, the following occurs...
         {
           previousRightSwitchVal = RightSwitchVal; //makes sure the previousBrakeSwitch value is reset to off when the switch is in fact off.
           RightBlinkState = LOW; //resets the blinker
           digitalWrite(RightBlink, RightBlinkState); //Overall, if the Right Blinker switch is off, keep the Right Blinker off. 
         }
         
 //Left Blinker Code. Identical to Right Blinker Code.//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  LeftSwitchVal = digitalRead(LeftSwitch);
  
  if ((LeftSwitchVal == HIGH) && (previousLeftSwitchVal == LOW))
    {
     previousLeftSwitchVal = LeftSwitchVal;
     LeftSwitchMillis = currentMillis;
    }


    if((LeftSwitchVal == HIGH) && (currentMillis - LeftFlashMillis > interval/4))                                                                                                                                 
         {
        LeftFlashMillis = currentMillis;
         
         if (LeftBlinkState == LOW)
            LeftBlinkState = HIGH;
          else
            LeftBlinkState = LOW;
            
          digitalWrite(LeftBlink, LeftBlinkState);
          }
     
          
    if (LeftSwitchVal == LOW)
         {
           previousLeftSwitchVal = LeftSwitchVal;
           LeftBlinkState = LOW;
           digitalWrite(LeftBlink, LeftBlinkState);
         }
         
}

And here is the fancy version that took me so long to make. (This is half the sketch, the second half is in the next reply.)

/*A sketch to control the indicator lights of a motorcycle, 
including an initial attention grabbing rapid-blink sequence lasting one half second followed by pulsing "flashes"
or the brake light holding full bright on.
*/


const int BrakeSwitch = 6;
const int BrakeLight = 9;

const int RightSwitch = 7;
const int RightBlink = 10;

const int LeftSwitch = 8;
const int LeftBlink = 11;

long BrakeSwitchMillis = 0; //Time stamp for when the switch last went to 1(HIGH). Initially set at 0(LOW)
int BrakeSwitchVal = 0; //Monitors the value of the Brake switch, whether 1 or 0 (HIGH or LOW). Initially set at 0(LOW)
int previousBrakeSwitchVal = 0; //Monitors the value of the Brake switch from last time it was checked. Initally set at 0(LOW)
int BrakeLightState = LOW; //State of the Brake Light. Initally set to LOW(Off)
long previousMillis9 = 0; //Time stamp for when light last went to 1(LOW), initially set at O(LOW)

long RightSwitchMillis = 0; //Time stamp for when the switch last went to 1(HIGH). Initially set at 0(LOW)
int RightSwitchVal = 0; //Monitors the value of the Right Blinker switch, whether 1 or 0 (HIGH or LOW). Initially set at 0(LOW)
int previousRightSwitchVal = 0; //Monitors the value of the Right Blinker switch from last time it was checked. Initally set at 0(LOW)
int RightBlinkState = LOW; //State of the Right Blinker Light. Initally set to LOW(Off)
long RightFlashMillis = 0; //Time stamp for when rapid flash last went to 1(LOW), initially set at O(LOW)
long RightPulseMillis = 0; //Time stamp for when the Right Blinker value i was last adjusted
int R = 0; //variable used to count up and down for the Right Blinker pulses.
int RightPulse = LOW; //variable used to determine Fade In or Fade Out. HIGH means i=225 and going down, LOW means i=0 and going up.

long LeftSwitchMillis = 0;
int LeftSwitchVal = 0;
int previousLeftSwitchVal = 0;
int LeftBlinkState = LOW;
long LeftFlashMillis = 0;
long LeftPulseMillis = 0;
int L = 0;
int LeftPulse = LOW;


long interval = 1000; // interval at which to blink (milliseconds)

void setup() 
{
  pinMode(BrakeLight, OUTPUT); 
  pinMode(BrakeSwitch, INPUT);
  
  pinMode(RightBlink, OUTPUT); 
  pinMode(RightSwitch, INPUT); 
  
  pinMode(LeftBlink, OUTPUT); 
  pinMode(LeftSwitch, INPUT); 
}

void loop()
{
  unsigned long currentMillis = millis(); //initiallizes the clock 
  
//Brake Light Code////////////////////////////////////////////////////////////////////////////////////////
BrakeSwitchVal = digitalRead(BrakeSwitch); //read and store brake switch status
  if ((BrakeSwitchVal == HIGH) && (previousBrakeSwitchVal == LOW)) //compares current switch status to previous status
   {
    previousBrakeSwitchVal = BrakeSwitchVal; //sets the current status as the old status in preparation for the next check
    BrakeSwitchMillis = currentMillis; //sets a time stamp at the current time
   }
       if ((BrakeSwitchVal == HIGH) && (currentMillis - BrakeSwitchMillis > interval/2)) //checks if the brake switch is pressed and how long it has been pressed for
         {
         digitalWrite (BrakeLight, HIGH); //if the switch is being pressed on and it has been pressed for longer than one half second, the brake light goes on full and steady.
         }
         
         
       else if((BrakeSwitchVal == HIGH) && (currentMillis - previousMillis9 > interval/50)) //if the above conditions are true, this checks if the brake switch is HIGH
                                                                                             //and more than one-fifteith of a second has elapsed since the last time previousMillis9 was reset.
         {
        previousMillis9 = currentMillis; // saves current time
         
         if (BrakeLightState == LOW)
            BrakeLightState = HIGH;
          else
            BrakeLightState = LOW; //if more than one-fiftieth of a second has elapsed since the time stamp was reset, reverse the state of the brake light.

          
          digitalWrite(BrakeLight, BrakeLightState); // set the LED with the ledState of the variable:
          }
     if (BrakeSwitchVal == LOW)
         {
           previousBrakeSwitchVal = BrakeSwitchVal; //makes sure the previousBrakeSwitch value is reset of off when the switch is in fact off.
           digitalWrite (BrakeLight, LOW); //Overall, if the brake switch is off, keep the brake light off.   
         }

//Right Blinker Code/////////////////////////////////////////////////////////////////////////////////////////////////////////
  RightSwitchVal = digitalRead(RightSwitch); //read and store Right Blinker switch status
  
  if ((RightSwitchVal == HIGH) && (previousRightSwitchVal == LOW)) //compares current switch status to previous status
    {
     previousRightSwitchVal = RightSwitchVal; //sets the current status as the old status in preparation for the next check
     RightSwitchMillis = currentMillis; //sets a time stamp at the current time
    }

   if ((RightSwitchVal == HIGH) && (currentMillis - RightSwitchMillis > interval/2) && (RightPulse == LOW) && (currentMillis - RightPulseMillis > interval/10000)) //checks if the Right Blinker switch is pressed and how long it has been pressed for,
        {                                                                                                                                                        // if the Pulse was last set LOW, and if it is time for another step in brightness.
         RightPulseMillis = currentMillis; //Time stamp used to check how long since the brightness, i, was last changed
         R++; //Adds 1 to R value. The value of R dictates light brightness
         RightBlinkState = R; //Sets the light brightness to the value of i.
         analogWrite(RightBlink, RightBlinkState); //Sets the blinker brightness to the dictates level. Why I cannot go directly use (RightBlink, R), I do not know...
         }

 
  
   if (R == 255) //checks value of R
    {
     RightPulse = HIGH; //if R is maxed out at 255, the RightPulse goes HIGH. This lets R decrease.
    }


    
   if ((RightSwitchVal == HIGH) && (currentMillis - RightSwitchMillis > interval/2) && (RightPulse == HIGH) && (currentMillis - RightPulseMillis > interval/10000)) //checks if the Right Blinker switch is pressed and how long it has been pressed for,
        {                                                                                                                                                           // if the Pulse was last set HIGH, and if it is time for another step in brightness.
         RightPulseMillis = currentMillis; //if the Right Blinker switch is pressed on and has been on for more than half a second, set a time stamp for the fade in. 
         R--; //Subtracts 1 from R value
         RightBlinkState = R; //Sets the value of light brightness to the value of R
         analogWrite(RightBlink, RightBlinkState); //set the blinker brightness to the value of RightBlinkState
          }
          
   if ((R == 0) && (currentMillis - RightSwitchMillis > interval/2)) // Checks if R is at 0 so that the brightening cycle can repeate. Also checks if the initial rapid flash sequence is over.
    {
      RightPulse = LOW; //if the above conditions are met, reset RightPulse to LOW.
    }


    if((RightSwitchVal == HIGH) && (currentMillis - RightFlashMillis > interval/50) && (currentMillis - RightSwitchMillis < interval/2)) //if the Right Blink switch is HIGH but less than half a second has elapsed,                                                                                                                                 
         {                                                                                                                                 // this checks if the Right Blink switch is HIGH and more than one-fifteith
        RightFlashMillis = currentMillis; // saves current time                                                                             //of a second has elapsed since the last time previousMillis10 was reset.
         
         if (RightBlinkState == LOW)
            RightBlinkState = HIGH;
          else
            RightBlinkState = LOW; //if more than one-fiftieth of a second has elapsed since the time stamp was reset, reverse the state of the Right Blinker.
            
          digitalWrite(RightBlink, RightBlinkState); // set the Right Blinker with the state of the variable:
          }
     
          
    if (RightSwitchVal == LOW) //checks if the Blinker switch is still on or not. If it is off, the following occurs...
         {
           previousRightSwitchVal = RightSwitchVal; //makes sure the previousBrakeSwitch value is reset to off when the switch is in fact off.
           RightBlinkState = LOW; //resets the blinker
           digitalWrite(RightBlink, RightBlinkState); //Overall, if the Right Blinker switch is off, keep the Right Blinker off. 
           R = 0; //resets R in preparation for the next usage.
         }

And the second half of that sketch since it was too long for that post :stuck_out_tongue:

 //Left Blinker Code. Identical to Right Blinker Code.//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  LeftSwitchVal = digitalRead(LeftSwitch);
  
  if ((LeftSwitchVal == HIGH) && (previousLeftSwitchVal == LOW))
    {
     previousLeftSwitchVal = LeftSwitchVal;
     LeftSwitchMillis = currentMillis;
    }

   if ((LeftSwitchVal == HIGH) && (currentMillis - LeftSwitchMillis > interval/2) && (LeftPulse == LOW) && (currentMillis - LeftPulseMillis > interval/10000))
        {
         LeftPulseMillis = currentMillis;
         L++; 
         LeftBlinkState = L;
         analogWrite(LeftBlink, LeftBlinkState);
         }

 
  
   if (L == 255)
    {
     LeftPulse = HIGH;
    }


    
   if ((LeftSwitchVal == HIGH) && (currentMillis - LeftSwitchMillis > interval/2) && (LeftPulse == HIGH) && (currentMillis - LeftPulseMillis > interval/10000)) 
        {
         LeftPulseMillis = currentMillis;
         L--;
         LeftBlinkState = L;
         analogWrite(LeftBlink, LeftBlinkState);
          }
          
   if ((L == 0) && (currentMillis - LeftSwitchMillis > interval/2))
    {
      LeftPulse = LOW;
    }


    if((LeftSwitchVal == HIGH) && (currentMillis - LeftFlashMillis > interval/50) && (currentMillis - LeftSwitchMillis < interval/2))                                                                                                                                 
         {
        LeftFlashMillis = currentMillis;
         
         if (LeftBlinkState == LOW)
            LeftBlinkState = HIGH;
          else
            LeftBlinkState = LOW;
            
          digitalWrite(LeftBlink, LeftBlinkState);
          }
     
          
    if (LeftSwitchVal == LOW)
         {
           previousLeftSwitchVal = LeftSwitchVal;
           LeftBlinkState = LOW;
           digitalWrite(LeftBlink, LeftBlinkState);
           L = 0;
         }
         
}

Sweet I have a need for this!! My dad made a turn signal light set up 20+ yeas ago for a moped that he just handed down to my daughter. She got caught in a rain storm and the blinkers quit working. I was just thinking of using an Arduino to rebuild the setup. This will be a great time saver.

Thanks

Sounds good, this will definitely work! Though really if you are just looking to correct the lights going out, I imagine trouble shooting the waterlogged 20 year old, corroded wire harness will be much simpler; not much to those things. Good luck!

Good God Man!!! Thank YOU!!!! Like you, I've been searching many an hour trying to find something "pre-cooked" for indicators... I hear angels singing and blinkers blinking... right on man... Thanks Again... and again... you get the picture...

1 Like

You are very welcome! Good luck, and I'm glad I've been of help to some part of this great community which has helped me so much.

Karma++ means Karma=Karma+1
Good job

Thank you, loudboy! The first sketch was very helpful in creating my own signal controller. I'm trying to modify the code so that releasing the signal switch would result in 3 additional flashes. I've tried blink, count, and debounce code with no luck.

Anyone know a good way to add or modify the above sketch to include this feature?

Thanks in advance!

Thank you Loudboy for making this code!
I did snatch it for my bike light controller (under development) and added some code for troubleshooting and some other functions.

i still havent tested it in a real arduino but my setup over at 123dcircuits looks promising!

Next step is to add high beam lights function, horn and a energy save mode.

Someday i hope to get good enough to be able to write a speedometer function aswell, it would be awesome to use a servo or stepper motor to create a car-type speedometer with a rotating arrow/needle (incorrect word??).

I sincerely hope you dont mind me copying and developing your work.
i will credit your work in sourcecode with a link to this thread.. :slight_smile:

My plan is to release the finished, modified code along with circuit blueprints as a opensource DIY project on my site.
Please let me know if this is a problem.. :slight_smile:

Time for a long overdue update to this post.

-After 6 years of tinkering off and on in garages, storage lockers, and backyards all across the country, I have finally got my motorcycle running and this sketch implemented. The trouble now is that the dirty voltage(or possibly the hi voltage bursts from the sparkplugs) produced by this nearly 50 year old motorcycle causes the lights to go haywire. They flash and blink constantly and randomly. I will detail my troubleshooting efforts in a separate thread([Solved!] How to reduce electrical noise from motorcycle alternator? - #2 by loudboy - Project Guidance - Arduino Forum) and post the eventual solution here in the hope of helping someone in the future.

*Update - Using sparkplugs with built in resistors (BR8ES instead of B8ES in my case, completely solved the problem.)

Hi Loudboy
I also have been tinking around with a code similar to this can you implement another feature that when you sit in traffic stop go stop go you dont want it to flash with every brake press, say give the flahing process a 10 sec break just go full on with every break press. If no brake press has been pressed after so 10 sec then the flashing will start again. I dont know if I have explained mysyself clearly.

Hi artois

Yes, this would be possible with another if/then statement which checks how long it's been since the brake switch last went HIGH. If less than 10 second, just set the brake light to HIGH. If not, proceed as normal. This check would be done at the same time the code compares the current state of the brake switch.

Hi Loudboy,

I just recently learned of the existence of the Arduino and Raspberry Pi for that matter, but one of the first things I thought to do with it was add lights to my topcase. Thanks for this code.

I am interested in if you added voltage regulation to A) the power to the Arduino, or B) the inputs from the brakes, etc. I think the Arduino is rated for 12v but bikes and cars can easily go to 13.9 if not higher.

Thanks,

Hi ActionGarrett

The first post in this thread has a Fritzing file of my schematic. The motorcycle itself has a voltage regulator and the arduino has a second one onboard.

Hi loudboy, I am a 76 year old new boy to Arduino, but learning fast. I have downloaded your code for the Turn signals and Brake, but struggling with the wiring detail. I need to find your Fritzing file but cannot see a link to it from your first post, i would be grateful if you would advise me how to retrieve it.
thanks

Please tell me how to retrieve your Fritzing file , i cannot see it in your posts, I have got as far as downloding
the code to my board but i am confused about how it is actually wired up as a circuit
Thank you
solderman