/*
* Code for adding comfort blinkers. I added it to my 2002 Volvo S60, but i can't think of a reason why it would not work on other cars.
* Connection to the car: the Arduino takes over the signal to the car, and reads the signal from the turn signal stalk directly. This means cutting traces on the board, and soldering wires to them. Some skill and electrics knowledge is required.
* 2 relays are used to switch on and off the turn signals. Make sure the relays do not draw more than 40mA at 5v, otherwise the Arduino might get damaged. Make sure to add a snubber/flyback diode as well, otherwise your Arduino will die an untimely death.
* More info here: https://www.reddit.com/r/Volvo/comments/ftkbtg/added_comfort_blinkers_to_my_2002_s60_using_an/ If you have any questions, you can send me a message there.
* Released under a 'sharing is caring'-licence. In other words, do what you want with it.
* Using this code is at your own risk.
*/
#define left_in 9 //Input signal for the left turn signal
#define right_in 8 //Input signal for the right turn signal
#define left_out 7 //Output signal for the left turn signal, connect to the coil of the relay
#define right_out 6 //Output signal for the left turn signal, connect to the coil of the relay
#define blinktime 2000 //Time the blinkers will be on for. 2000 ms gives me 3 blinks. If you want more or less blinks, chance the '2000' value to whatever you like. Value is in milliseconds
//#define debug 1 //If you want to debug it, enabling this line will print some info on the serial bus. This will also enable a little sequence at startup, so don't leave this on after you're done debugging
unsigned long time_start = 0; //Needed to store the time (millis()) blinking starts. Leave as-is
void setup() {
pinMode (left_in, INPUT_PULLUP); //Setting the pins to inputs and outputs. INPUT_PULLUP enables an internal pullup resistor, eliminating the use of an external one. You could use an external one as well, make sure to change it to "INPUT" then
pinMode (right_in, INPUT_PULLUP);
pinMode (left_out, OUTPUT);
pinMode (right_out, OUTPUT);
#ifdef debug //Code only runs with debug enabled
Serial.begin(9600);
Serial.println("hello"); //Check if serial is working
digitalWrite(left_out, HIGH); //Startup blink sequence, for debugging only
delay(2000);
digitalWrite(left_out, LOW);
delay(2000);
digitalWrite(right_out, HIGH);
delay(2000);
digitalWrite(right_out, LOW);
#endif
}
void loop() {
#ifdef debug //Code only runs with debug enabled
Serial.print("leftin: ");Serial.print(digitalRead(left_in));
Serial.print(" rightin: ");Serial.println(digitalRead(right_in));
#endif
//****************************LEFT
if (digitalRead(left_in) == LOW) { //Read the input for the left signal
delay(50);
if (digitalRead(left_in) == LOW) { //Read it again after 50ms. Sketchy way of debouncing the signal
digitalWrite(left_out, HIGH); //If both are true, the left signal is activated here
time_start = millis(); //Saves the current value of Millis() in a variable, to compare it to later
delay(200); //Also a little debounce thingy
while (millis() - time_start < blinktime) { //Wait in this loop for the value set in blinktime
if (digitalRead(right_in) == 0) { //If the right turn signal is activated, this will cancel the left one and switch over to the right one.
digitalWrite(left_out, LOW); // Cancel left turn signal
break; //Stop the While-loop before the time ends.
}
}
while (digitalRead(left_in) == LOW) { //Keeps the signal on after the time ends,
//for as long as the turn signal is on that position.
digitalRead(left_in); //Read the input. Without this the While-loop would never end.
delay(10);
}
digitalWrite(left_out, LOW); //Turn off the left turn signal
}
}
//******************************RIGHT
if (digitalRead(right_in) == LOW) { //Read the input for the right signal
delay(50);
if (digitalRead(right_in) == LOW) { //Read it again after 50ms. Sketchy way of debouncing the signal
digitalWrite(right_out, HIGH); //If both are true, the right signal is activated here
time_start = millis(); //Saves the current value of Millis() in a variable, to compare it to later
delay(200); //Also a little debounce thingy
while (millis() - time_start < blinktime) { //Wait in this loop for the value set in blinktime
if (digitalRead(left_in) == LOW) { //If the left turn signal is activated, this will cancel the right one and switch over to the left one.
digitalWrite(right_out, LOW); // Cancel right turn signal
break; //Stop the While-loop before the time ends.
}
}
while (digitalRead(right_in) == LOW) { //Keeps the signal on after the time ends, for as long as the turn signal is on that position.
digitalRead(right_in); //Read the input. Without this the While-loop would never end.
delay(10);
}
digitalWrite(right_out, LOW); //Turn off the right turn signal
}
}
delay(10);
}