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);
}
}