LED Tail Light Troubleshooting

I've recently purchased an Arduino Uno and am completely new to the concept, however I learn quick and I'm really sick of my current tail lights; hence the reason for this post. I uploaded the following code to my arduino as well as 3 leds to corresponding pins on a breadboard, however powering the arduino just causes the 3 leds to flash blink and quite frankly do whatever they want and supplying power to certain input pins makes them stay solid (the brake feature). Is it the code or just my wiring? Ive posted code and can upload wiring if needed.

// constants
const int buttonPin1 = 8; //the number of the brake light pin
const int buttonPin2 = 9; //the number of the left turn signal pin
const int buttonPin3 = 10; //the number of the hazzard pin
const int buttonPin4 = 11; //the number of right turn
const int buttonPin5 = 12; //the number of reverse pin
const int ledPin1 = 2; //right turn set
const int ledPin2 = 3; //left turn LED set
const int ledPin3 = 4; //reverse LED set

//variables
int buttonState1 = 0; //brake off until triggered
int buttonState2 = 0; //left turn off until triggered
int buttonState3 = 0; //hazard off until triggered
int buttonState4 = 0; //right off until triggered
int buttonState5 = 0; //reverse off until triggered

void setup() //set button pins as outputs and LED pins as inputs
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}

void loop(){
// read the state of the input values:
buttonState1 = digitalRead(buttonPin1); // check if the brake is triggered
buttonState2 = digitalRead(buttonPin2); // check if the left is triggered
buttonState3 = digitalRead(buttonPin3); // check if the hazard is triggered
buttonState4 = digitalRead(buttonPin4); //check if right is triggered
buttonState5 = digitalRead(buttonPin5); //check if reverse is triggered

if (buttonState1 == HIGH) {
//trigger brakes:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
}
else if (buttonState2 == HIGH) {
//blink left set:
loop; //cycle function
digitalWrite(ledPin2, HIGH); //turn LED set 1 on
delay(500); //wait with LED set 1 on
digitalWrite(ledPin2, LOW); //turn LED set 1 off
delay(500); //wait with
}
else if (buttonState3 == HIGH) {
//trigger hazards:
loop; //cycle function
digitalWrite(ledPin1, HIGH); //turn left set on
delay(500); //wait with left set on
digitalWrite(ledPin2, HIGH); //turn right on
delay(500); //wait with right set on
digitalWrite(ledPin1, LOW); //turn left set off
delay(500); //wait with left set on
digitalWrite(ledPin2, LOW); //turn right set off
delay(500); //wait with right set off
}
else if (buttonState4 == HIGH) {
//blink right set:
loop; //cycle function
digitalWrite(ledPin1, HIGH); //turn LED set 1 on
delay(500); //wait with LED set 1 on
digitalWrite(ledPin1, LOW); //turn LED set 1 off
delay(500); //wait with
}

else if (buttonState5 == HIGH) {
digitalWrite(ledPin3,HIGH);
}

}

All those delays() make baby Jesus cry. :frowning:

Q: What does "loop;" do? It compiles but I've never seen that syntax before.

Did you use pull up/down resistors on your buttons.
Leo..

millerkody:
I've recently purchased an Arduino Uno and am completely new to the concept, however I learn quick and I'm really sick of my current tail lights; hence the reason for this post. I uploaded the following code to my arduino as well as 3 leds to corresponding pins on a breadboard, however powering the arduino just causes the 3 leds to flash blink and quite frankly do whatever they want and supplying power to certain input pins makes them stay solid (the brake feature). Is it the code or just my wiring? Ive posted code and can upload wiring if needed.

// constants
const int buttonPin1 = 8; //the number of the brake light pin
const int buttonPin2 = 9; //the number of the left turn signal pin
const int buttonPin3 = 10; //the number of the hazzard pin
const int buttonPin4 = 11; //the number of right turn
const int buttonPin5 = 12; //the number of reverse pin
const int ledPin1 = 2; //right turn set
const int ledPin2 = 3; //left turn LED set
const int ledPin3 = 4; //reverse LED set

//variables
int buttonState1 = 0; //brake off until triggered
int buttonState2 = 0; //left turn off until triggered
int buttonState3 = 0; //hazard off until triggered
int buttonState4 = 0; //right off until triggered
int buttonState5 = 0; //reverse off until triggered

void setup() //set button pins as outputs and LED pins as inputs
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}

void loop(){
// read the state of the input values:
buttonState1 = digitalRead(buttonPin1); // check if the brake is triggered
buttonState2 = digitalRead(buttonPin2); // check if the left is triggered
buttonState3 = digitalRead(buttonPin3); // check if the hazard is triggered
buttonState4 = digitalRead(buttonPin4); //check if right is triggered
buttonState5 = digitalRead(buttonPin5); //check if reverse is triggered

if (buttonState1 == HIGH) {
//trigger brakes:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
}
else if (buttonState2 == HIGH) {
//blink left set:
loop; //cycle function
digitalWrite(ledPin2, HIGH); //turn LED set 1 on
delay(500); //wait with LED set 1 on
digitalWrite(ledPin2, LOW); //turn LED set 1 off
delay(500); //wait with
}
else if (buttonState3 == HIGH) {
//trigger hazards:
loop; //cycle function
digitalWrite(ledPin1, HIGH); //turn left set on
delay(500); //wait with left set on
digitalWrite(ledPin2, HIGH); //turn right on
delay(500); //wait with right set on
digitalWrite(ledPin1, LOW); //turn left set off
delay(500); //wait with left set on
digitalWrite(ledPin2, LOW); //turn right set off
delay(500); //wait with right set off
}
else if (buttonState4 == HIGH) {
//blink right set:
loop; //cycle function
digitalWrite(ledPin1, HIGH); //turn LED set 1 on
delay(500); //wait with LED set 1 on
digitalWrite(ledPin1, LOW); //turn LED set 1 off
delay(500); //wait with
}

else if (buttonState5 == HIGH) {
digitalWrite(ledPin3,HIGH);
}

}

Hi there, a few comments to help you get the help you need, I am no expert but I can strongly recommend you look at the following:

  1. Use code tags - this makes it very easy for others to copy the code to test/edit/modify etc.

  2. Naming conventions - instead of "const int buttonPin1 = 8; //the number of the brake light pin" name it something like "const int brakelight = 8; //the number of the brake light pin.
    It is much easier to follow your program now, and in the future, than having to look back to see what buttonpin1 was for.

  3. Delays- every delay freezes the controller for the delay period, so if you turn on the left indicator then put the brakes on the brake light won't light up until the indicator delay period completes. Look for blink without delay and demonstration code for doing several things at once to see a better way to handle delays and pauses.

  4. if - else statements - Please read through your code and imagine what is going to happen as it runs through, as I see it this is what will happen:

if (buttonState1 == HIGH) { press the Brake pedal this instruction becomes true
//trigger brakes:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
}
LED's switch on and stay on (there is no instruction to turn off if buttonstate1 goes low)

else if (buttonState2 == HIGH) {
//blink left set:
loop; //cycle function
digitalWrite(ledPin2, HIGH); //turn LED set 1 on
delay(500); //wait with LED set 1 on
digitalWrite(ledPin2, LOW); //turn LED set 1 off
delay(500); //wait with
}

This will not be tested if your foot is on the brake and none of the other possible states can't be tested while this state is true.

Remember the program is sequential meaning it tests the instructions continuously from top to bottom so if one instruction says turn on and the next says turn off it will simply appear to be off.
However if there is no off instruction it will stay on for ever....

*edited to add Code Tags comment

I'm not seeing what's wrong but it's best to SIMPLIFY! Start with just one function (maybe the break lights) and when that works add the next function, etc.

Take advantage of the serial monitor so you can "see" what the program is doing. i.e. The way the Digital Read Serial Example shows the state of a pin. You can also send-out helpful little messages like "left blinker activated, etc.


You could probably live with the delays except for the break light. The break light needs to come-on "NOW" when you hit the break and it's no-good if your program is sitting there delaying the blinkers until the loop comes-around again and turns-on the break light.

So eventually, you'll need to use the Blink Without Delay timing method. With this method you can run several different delays at the same time, each with it's one previous time and interval. (You probably don't need multiple-different-simultaneous timers, but it's easy to do.) See the post at the top of the forum titled Demonstration code for several things at the same time.

The pattern for you hazards is screwy. You need to turn-on both lights and wait, then turn-off both lights and wait. (You could alternate the lights or make something like both-left-both-right-both-left pattern, but that's probably technically-illegal.)

As mentioned your brake light and reverse light would stay on as they are never set low once the button is released. Here's a few simple fixes but please check your pins assignments as i think i changed some around as they where confusing to keep track off. Oh and your code ran so that if a indicator was on or the brake you could not use the hazards or reverse which is NOT ideal... which i corrected along with the correct if statement order.

Edit: code adjusted to work in all states and in order of priority, just like a normal car and with no delays, i assume your car shares the break clusters with the indicators and hazards, as i see you have no pin set for the break cluster and you share the same pins throughout, but i have seen such so i guessed so...

I.E

left indicator active = flash left cluster on for 500 ms and off for 500ms even if the break is on.
right indicator active = flash right cluster on for 500 ms and off for 500ms even if the break is on.
hazard active = flash left and right cluster "at the same time" on for 500 ms and off for 500ms.
"note even if the indicator is active the hazard shall flash both clusters and when the hazard is released the previous state shall continue.
break active, even if indicators or hazard is active = constant on for both left and right clusters, until break is released, then previous state shall continue.
Reverse active = reverse light on.
and of course if none of above are active said lights will be off.

// LAW STATES: lights must blink at the rate of not less than 60 nor more than 120 flashes per minute.

// input pins
const byte brakeLight_Pin = 8;     //the number of the brake light pin
const byte leftTurn_Pin = 9;       //the number of the left turn signal pin
const byte rightTurn_pin = 10;     //the number of right turn signal pin
const byte hazard_Pin = 11;        //the number of the hazard pin
const byte reverse_Pin = 12;       //the number of reverse pin
// output pins - LEDS
const byte right_light_Pin = 2;    //right turn LED set
const byte left_light_Pin = 3;     //left turn LED set
const byte reverse_light_Pin = 4;  //reverse LED set


void setup() {
  pinMode(right_light_Pin, OUTPUT);
  pinMode(left_light_Pin, OUTPUT);
  pinMode(reverse_light_Pin, OUTPUT);
}

void loop() {
  lightClusters();
}

void lightClusters() {
  const long interval = 500;
  unsigned long currentMillis = millis();
  if (digitalRead(leftTurn_Pin) == HIGH && digitalRead(hazard_Pin) == LOW) { // if left turn and NO hazard
    digitalWrite(right_light_Pin, LOW);
    static unsigned long previousMillis = 0;
    static bool ledState = true;
    if (currentMillis - previousMillis >= interval) {
      if (ledState) {
        digitalWrite(left_light_Pin, HIGH);
      }
      else {
        digitalWrite(left_light_Pin, LOW);
      }
      ledState = !ledState;
      previousMillis = currentMillis;
    }
  }
  else if (digitalRead(right_light_Pin) == HIGH && digitalRead(hazard_Pin) == LOW) { // else if right turn and NO hazard
    digitalWrite(left_light_Pin, LOW);
    static unsigned long previousMillis = 0;
    static bool ledState = true;
    if (currentMillis - previousMillis >= interval) {
      if (ledState) {
        digitalWrite(right_light_Pin, HIGH);
      }
      else {
        digitalWrite(right_light_Pin, LOW);
      }
      ledState = !ledState;
      previousMillis = currentMillis;
    }
  }
  else if (digitalRead(hazard_Pin) == HIGH && digitalRead(brakeLight_Pin) == LOW) {  // else if hazard is active and NO break.
    static unsigned long previousMillis = 0;
    static bool ledState = true;
    if (currentMillis - previousMillis >= interval) {
      if (ledState) {
        digitalWrite(left_light_Pin, HIGH);
        digitalWrite(right_light_Pin, HIGH);
      }
      else {
        digitalWrite(left_light_Pin, LOW);
        digitalWrite(right_light_Pin, LOW);
      }
      ledState = !ledState;
      previousMillis = currentMillis;
    }
  }
  if (digitalRead(brakeLight_Pin) == HIGH) { // ignoring all other states, if break is active.
    digitalWrite(left_light_Pin, HIGH);
    digitalWrite(right_light_Pin, HIGH);
  }
  else if (digitalRead(leftTurn_Pin) == LOW && digitalRead(right_light_Pin) == LOW && digitalRead(hazard_Pin) == LOW) { // else if NO signals, hazard or break, Turn off left and right cluster.
    digitalWrite(left_light_Pin, LOW);
    digitalWrite(right_light_Pin, LOW);
  }
  if (digitalRead(reverse_Pin) == HIGH) { // ignoring all other states, if reverse is active.
    digitalWrite(reverse_light_Pin, HIGH);
  }
  else { // ignoring all other states, if reverse is NOT active.
    digitalWrite(reverse_light_Pin, LOW);
  }
}

Else if you do have separate clusters for each then it is much easier, in the UK we have a cluster for the indicators and another for the breaks. Here's that code if so.

// LAW STATES: lights must blink at the rate of not less than 60 nor more than 120 flashes per minute.

// input pins
const byte brakeLight_Pin = 8;     //the number of the brake light pin
const byte leftTurn_Pin = 9;       //the number of the left turn signal pin
const byte rightTurn_pin = 10;     //the number of right turn signal pin
const byte hazard_Pin = 11;        //the number of the hazard pin
const byte reverse_Pin = 12;       //the number of reverse pin
// output pins - LEDS
const byte right_light_Pin = 2;    //right turn LED set
const byte left_light_Pin = 3;     //left turn LED set
const byte reverse_light_Pin = 4;  //reverse LED set
const byte break_light_Pin = 5;    //break LED set ??

void setup() {
  pinMode(right_light_Pin, OUTPUT);
  pinMode(left_light_Pin, OUTPUT);
  pinMode(break_light_Pin, OUTPUT); // ??
  pinMode(reverse_light_Pin, OUTPUT);
}

void loop() {
  lightClusters();
}

void lightClusters() {
  const long interval = 500;
  unsigned long currentMillis = millis();
  if (digitalRead(leftTurn_Pin) == HIGH && digitalRead(hazard_Pin) == LOW) { // if left turn and NO hazard
    digitalWrite(right_light_Pin, LOW);
    static unsigned long previousMillis = 0;
    static bool ledState = true;
    if (currentMillis - previousMillis >= interval) {
      if (ledState) {
        digitalWrite(left_light_Pin, HIGH);
      }
      else {
        digitalWrite(left_light_Pin, LOW);
      }
      ledState = !ledState;
      previousMillis = currentMillis;
    }
  }
  else if (digitalRead(right_light_Pin) == HIGH && digitalRead(hazard_Pin) == LOW) { // else if right turn and NO hazard
    digitalWrite(left_light_Pin, LOW);
    static unsigned long previousMillis = 0;
    static bool ledState = true;
    if (currentMillis - previousMillis >= interval) {
      if (ledState) {
        digitalWrite(right_light_Pin, HIGH);
      }
      else {
        digitalWrite(right_light_Pin, LOW);
      }
      ledState = !ledState;
      previousMillis = currentMillis;
    }
  }
  else if (digitalRead(hazard_Pin) == HIGH) {  // else if hazard is active
    static unsigned long previousMillis = 0;
    static bool ledState = true;
    if (currentMillis - previousMillis >= interval) {
      if (ledState) {
        digitalWrite(left_light_Pin, HIGH);
        digitalWrite(right_light_Pin, HIGH);
      }
      else {
        digitalWrite(left_light_Pin, LOW);
        digitalWrite(right_light_Pin, LOW);
      }
      ledState = !ledState;
      previousMillis = currentMillis;
    }
  }
  else { // else if NO signals or hazard, Turn off left and right cluster.
    digitalWrite(left_light_Pin, LOW);
    digitalWrite(right_light_Pin, LOW);
  }
  if (digitalRead(brakeLight_Pin) == HIGH) { // ignoring all other states, if break is active.
    digitalWrite(break_light_Pin, HIGH);
  }
  else { // else if NO break
    digitalWrite(break_light_Pin, LOW);
  }
  if (digitalRead(reverse_Pin) == HIGH) { // ignoring all other states, if reverse is active.
    digitalWrite(reverse_light_Pin, HIGH);
  }
  else { // ignoring all other states, if reverse is NOT active.
    digitalWrite(reverse_light_Pin, LOW);
  }
}

Blackfin:
Q: What does "loop;" do? It compiles but I've never seen that syntax before.

It evaluates the value of the function pointer "loop".
"loop" exists, so the pointer is non-zero, so the expression evaluates to "true".

The result of the evaluation is then discarded.

Nett result: nothing happens.

If you're lucky, the compiler spotted this and didn't bother to waste any more time generating code.

KawasakiZx10r:
The buttons where never set as inputs so i doubt they where working,

I had to stop right there. ALL AVR IO pins are INPUT LOW on startup. That is the default.

I'm a bit mystified as to why you use the Arduino at all unless you're trying to make lights blink/whatever in a different fashion than your car does. You only need a circuit to replace the bulbs.

Before you go modify your car as to brake lights, check your liability. Ask your insurance agent, for example, or a lwayer or a judge. If anything happens where your lights are in question, you will lose.

GoForSmoke:
I'm a bit mystified as to why you use the Arduino at all unless you're trying to make lights blink/whatever in a different fashion than your car does. You only need a circuit to replace the bulbs.

Before you go modify your car as to brake lights, check your liability. Ask your insurance agent, for example, or a lwayer or a judge. If anything happens where your lights are in question, you will lose.

Its a 76 nova, guy i bought it from decided trailer lights were a good idea.

So how does he have them hooked up? Do they work?

they work as they should but there are trailer lights bolted to my rear bumper. Most ghetto garbage ive ever seen. not to mention i have to change the bulb every other week.

So how does he have them hooked up? Do they work?
[/quote]

GoForSmoke:

If you can get the wiring back to the proper holes or maybe it's still there and you only need to hack the garbage off....

Car 12V does spike and leds with resistors alone will go poof. Thing to do is put a 12V Zener diode across 12V and GND, Zener diode will conduct > rated V straight to ground. Then you get a proper leds and resistors circuit in there.

I do have G4 12V led disks with 12 leds on the disk, They run on 12VAC or 12VDC really bright to look at (like a flashbulb) but they do get hot, need air circ. They're plug replacements for 12VAC halogen yard lights but they work with DC too.

I liked the Nova but never had one. Chevy lost sales on the name, no va is Spanish for doesn't go.

Did the guy you got it from leave the original lights plastic, trim, etc, in place? Because compared to bulbs, wow is that expensive! But just the bulb replacements all done up, I see under $20 ea.

GoForSmoke:
If you can get the wiring back to the proper holes or maybe it's still there and you only need to hack the garbage off....

Car 12V does spike and leds with resistors alone will go poof. Thing to do is put a 12V Zener diode across 12V and GND, Zener diode will conduct > rated V straight to ground. Then you get a proper leds and resistors circuit in there.

I do have G4 12V led disks with 12 leds on the disk, They run on 12VAC or 12VDC really bright to look at (like a flashbulb) but they do get hot, need air circ. They're plug replacements for 12VAC halogen yard lights but they work with DC too.

I liked the Nova but never had one. Chevy lost sales on the name, no va is Spanish for doesn't go.

Did the guy you got it from leave the original lights plastic, trim, etc, in place? Because compared to bulbs, wow is that expensive! But just the bulb replacements all done up, I see under $20 ea.

1976 Chevy Nova Custom & Factory Tail Lights – CARiD.com

the fiberglass enclosures that held the bulbs had disintegrated, hence his hack job. the plan was to build my own enclosure as its a very simple design and the official ones are almost $100 for just the housings. not to mention LEDs are much more efficient

You don't NEED a controller for that and not using one out on public roads will reduce your liability.

If you wanted to add display leds, EL wire and like making a moving show then you need the controller.

Will you need to make your own reflector? And how about the lenses and all seen from outside?

Heck... enough regular leds right behind a lens might be all you need. Well maybe with a diffuser of some kind to blend the led spots together, translucent stuff like milk jug plastic.

Just make sure that you get air circulation or heat sinks that do for where the leds are, a strip metal can be a heat sink. Considering how the outside must be sealed against weather you might have a challenge there though thought, how many regular red leds would be needed for a tail light? For a single COB led (they can run hot just fine) you will need a reflector whereas regular leds could go on a panel that attaches to the same mount points as the lens.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Did you write the code?
It would be best you wrote out your requirements for your code.
That is, what inputs, what outputs, what each input will do to the outputs.

Then stage by stage start coding.

For example,
Write code that JUST reads your input buttons/switches
When you have that working.
Write code that JUST makes your output function, even just to turn ON and OFF.
When you have that working.
Write code that JUST combines ONE of your functions ONLY, using what you have learnt from the previous steps.
When you have that function working.
Write code that JUST combines the NEXT of your functions ONLY, using what you have learnt from the previous steps.
Get each function working separately THEN.
Combine each function, one by one, each time getting it to work, before adding another.

I know it sounds long and laborious, but you will learn much more and make it easier in each step to avoid any major program bugs.
Sorry but writing in stages is better than having a mass of code and so many errors, not knowing where to start.

If you started in stages, the very FIRST thing you would have discovered, that there is a specific way you need to wire your buttons/switches to the controller to have reliable detection of switch logic.

Tom... :slight_smile: