Hi guys,
I have a 90s car with pop-up headlights but the former owner decided it was a good idea to remove the headlight module when it malfunctioned.
I was determined to build an Arduino based management module for headlights but after so many failed attempts I thought maybe you guys could give me a hand to resolve the issues.
Plan is simple:
1- I have a master switch - if master switch is ON, headlights should always be UP.
2- If master is OFF and light signal is ON - headlights should be UP and after light signal is OFF, headlights should close after 5 seconds.
3- If headlights are DOWN and flash signal is ON - headlights should be UP and wait for 5 seconds, if flash signal is not repeated in that time, headlights should close. If flash signal is ON again during that time, countdown should reset.
I am sure it is just an easy level practice for most of you but as a hobbyist, I am not much of a coder and I cannot make this one work. Please find the code I tried down below. Thank all of you for pitching in.
#define relayRight 8
#define relayLeft 9
#define lightSignal A2
#define flashSignal A3
#define masterSignal 13
int relayStatus = 0;
int Lights = 0;
int Flash = 0;
int Master = 0;
unsigned int lightThreshold = 10;
unsigned int flashThreshold = 2;
unsigned long lightOffTime = 0;
unsigned long flashOffTime = 0;
bool signalTimerActive = false;
void setup() {
pinMode(relayLeft, OUTPUT);
pinMode(relayRight, OUTPUT);
pinMode(masterSignal, INPUT_PULLUP);
digitalWrite(relayLeft, HIGH);
digitalWrite(relayRight, HIGH);
Serial.begin(9600);
}
void loop() {
Lights = analogRead(lightSignal);
Flash = analogRead(flashSignal);
Master = digitalRead(masterSignal);
unsigned long currentTime = millis();
if (Master == LOW) {
relayStatus = 1;
HeadsUp();
} else {
// Light signal logic
if (Lights >= lightThreshold) {
relayStatus = 1;
HeadsUp();
signalTimerActive = false;
} else if (Lights < lightThreshold && relayStatus == 1) {
if (!signalTimerActive) {
lightOffTime = currentTime;
signalTimerActive = true;
}
if (currentTime - lightOffTime >= 4000) {
relayStatus = 0;
HeadsDown();
signalTimerActive = false;
}
}
if (relayStatus == 0 && Flash >= flashThreshold) {
relayStatus = 1;
HeadsUp();
flashOffTime = currentTime;
signalTimerActive = true;
}
if (signalTimerActive && (currentTime - flashOffTime >= 4000)) {
relayStatus = 0;
HeadsDown();
signalTimerActive = false;
}
}
Serial.print("Master: ");
Serial.println(Master);
Serial.print("Light: ");
Serial.println(Lights);
Serial.print("Flash: ");
Serial.println(Flash);
delay(200);
}
void HeadsUp() {
digitalWrite(relayLeft, LOW);
digitalWrite(relayRight, LOW);
Serial.println("HeadsUp");
}
void HeadsDown() {
digitalWrite(relayLeft, HIGH);
digitalWrite(relayRight, HIGH);
Serial.println("HeadsDown");
}