First project, car blinker light mod

Hi! I am currently writing my first arduino program, and have bumped into some trouble. :confused:
The idea I'm having is that I want my car side turn indicator to work as intended, blinking along with the rest of the blinking lights. However, when the side turn indicator has been off for about one- to two seconds, I want it to turn on and glow continuously! :grinning:

So far, I have figured out that millis() should be used, its just the logic that is being a bit of a struggle for me at the moment. :confused:

So far I have written this code:

const int blinkSig = 4; // Incoming blinker signal, assigned to pin 4
const int outSig = 3;   // Outgoing signal, heading to side turn indicator

void setup() {
    pinMode(blinkSig, INPUT);
    pinMode(outSig, OUTPUT);
}

unsigned long offtime; 

void loop() {
  unsigned long currentTime = millis();

  if(digitalRead(blinkSig)==1){ //Turn light on when signal is incoming
    digitalWrite(outSig, HIGH);
   
  } else if(offtime == 0){ //Check if timer has started. If so, 
      if(currentTime - offtime >= 500){ //check if elapsed time is greater or equal
     digitalWrite(outSig, HIGH); // than 500ms. If so, turn light on
    } 
    
  } else {
      digitalWrite(outSig, LOW); //turn light off
      offtime = 0; //starts the timer that "indicates" for how long the light has been turned off.
   }
}

Though, the timer always reset in the end of each loop iteration, leading to the issue that the if-statement where it compares the elapsed time since light was turned off , is never going to become true.
This leads to the light turning on after 500ms(as intended), but never turns off.

Does anyone have any idea how I proceed from here? All tips are greatly appreciated! :slight_smile:

EDIT: The incoming signal is going to be pulsing. In other words, the program should not be handling any of the blinking.

// Johan

Does anyone have any idea how I proceed from here?

Start with using meaningful names.

unsigned long TimerStartTime;

What timer started?

The way you use that variable implies that it holds the time when the turn signal last changed state.

You need a boolean variable, lightShouldBeBlinking, initialized to false, and set to true or false as appropriate, in loop().

Then, you need

   if(lightShouldBeBlinking)
   {
      // Do EVERYTHING involved in blinking the turn signal, periodically, here
   }

in loop(), outside of any other if/else body.

PaulS:
Start with using meaningful names.

unsigned long TimerStartTime;

What timer started?

The way you use that variable implies that it holds the time when the turn signal last changed state.

You need a boolean variable, lightShouldBeBlinking, initialized to false, and set to true or false as appropriate, in loop().

Then, you need

   if(lightShouldBeBlinking)

{
      // Do EVERYTHING involved in blinking the turn signal, periodically, here
  }



in loop(), outside of any other if/else body.

Thank you Paul! I will clarify my code and comments a little. Thanks for the tip about the boolean! Will take some time to get into how they work! :slight_smile:

What is connected to pins 3 and 4? Post a wiring diagram.

JCA79B:
What is connected to pins 3 and 4? Post a wiring diagram.

Hi! I was thinking something like this, where the button simulates the blinker signal(pin 4), and the other wire(pin 3) is connected to a relay! :slight_smile:

Where does the blink signal come from if it is not simulated with a button?

JCA79B:
Where does the blink signal come from if it is not simulated with a button?

I was thinking that signal would be taken from the wire that goes between the cars blink relay and the lamp itself, but perhaps thats a bad idea now that I think about it(too high voltage etc?) :o

Ok, so I'll change my approach. A constant signal(the same one that goes into the cars original blink relay is going to be fed into the arduino. Might make some things easier to me! :slight_smile:

So, you want the side marker lamps to blink in unison with the turn signal lamps and to be on steady when the turn signals are not blinking. Is that correct?

JCA79B:
So, you want the side marker lamps to blink in unison with the turn signal lamps and to be on steady when the turn signals are not blinking. Is that correct?

Correct! I want the side turn signal lamp to blink in union with the rest of the indicator lights, and then be turned on(steady) when not using the turn signal. Just as you said :slight_smile:

Do you want the side marker lamps to blink with the turn signals when they would otherwise be off (daytime)?

JCA79B:
Do you want the side marker lamps to blink with the turn signals when they would otherwise be off (daytime)?

Here where I live, (europe) we do not usually have the side markers (as far as I know), only the side (fender) turn indicator. I'll provide a picture of the light I'm talking about for clarification!

So basically, I want the fender turn indicator to act as a side marker while I'm not blinking, but when I'm blinking, I want it to blink together with the other turn indicators. :slight_smile:

You could do that with only a 12V relay if the side marker lamps blinked in anti phase (marker ON when turn signal is OFF) is OK.
The marker lamps powered through the relay normally closed contacts and the relay coil powered from the turn signal blinker.

JCA79B:
You could do that with only a 12V relay if the side marker lamps blinked in anti phase (marker ON when turn signal is OFF) is OK.
The marker lamps powered through the relay normally closed contacts and the relay coil powered from the turn signal blinker.

Thats how people usually do it, they short the turn indicator light with the signal from the front position light, which means when they use the turn indicator, the side indicator is on when the other turn indicators are off. The issue is that having it that way is illegal. The only way the side turn indicator is legal as a side marker is to have it blink in sync with the other ones. :confused: