Problem with my code

Hello!

I bought my first arduino and i started with my first project, it is almost succesfuly done but i have problem with one thing.

So my project is coming/leaving home module for car. If i lock/unlock car it turns on fog lights for 20 seconds if there is dark outside.
Im taking signal from turn signals (lock/unlock) and other signal from igniton. So if i unlock car and i get behind wheel before 20 seconds are elapsed and i turn on ignition it immediately turns off foglights.

Till here is everything ok, but here is problem. When ignition is on (while driving) it shouldnt turn on the fog lights. What should i do that while ignition is on, module will not turn on fog lights?

Im a bit lost here so i will be very thankful for help!

Here is my code:

#include <elapsedMillis.h>
const int ignition = 2;
const int turnsignal = 13;
const int foglight =  8;
int buttonState = 0;
int sensor = A0;
int sensorValue = 0;

elapsedMillis timer0;

#define interval 20000

boolean timer0Fired;

void setup() {
 // put your setup code here, to run once:
 pinMode(foglight, OUTPUT);
 pinMode(ignition, INPUT);
 pinMode(turnsignal, INPUT);
 timer0 = 0;
 attachInterrupt(digitalPinToInterrupt(2),izklop,HIGH);
 Serial.begin(9600);
}

void loop() {
   sensorValue = analogRead(sensor);
   Serial.println(sensorValue);
  
{

 buttonState = digitalRead(turnsignal);
 delay(100);
 if(sensorValue < 50) //setting a threshold value
 
 if (buttonState == HIGH){
     digitalWrite(foglight, HIGH);
     
     timer0Fired = false;
     timer0 = 0;
     }
   
   if ((!timer0Fired) && (timer0 > interval)) {
   timer0Fired = true; // don't execute this again
   digitalWrite(foglight, LOW); // turn led off after 5 sec  
   } 
 }
}
 void izklop(){
  digitalWrite(foglight, LOW);
  }

Why in heavens name are you using an interrupt to determine whether the ignition is on or off ?

Which Arduino are you using and how are you connecting the inputs, bearing in mind that they will be at 12V ? Where are you taking the ignition on/off input from ?

UKHeliBob:
Why in heavens name are you using an interrupt to determine whether the ignition is on or off ?

Which Arduino are you using and how are you connecting the inputs, bearing in mind that they will be at 12V ? Where are you taking the ignition on/off input from ?

I tought i must use it because im interrupting program before usual routine ends (before 20 seconds are elapsed)...

I solved 12v inputs with voltage divider (10k and 4.7k). Ignition input is taken directly from ignition lock.

No need to use an interrupt. Just poll the ignition input using digitalRead(). I am assuming that the elapsedMillis library provides a non blocking way of timing.

UKHeliBob:
I am assuming that the elapsedMillis library provides a non blocking way of timing.

Yep, I saw it on a recent thread and tried it out. It's BWOD in a box....

You might want to give this "if"....

  if(sensorValue < 50) //setting a threshold value

.... a pair if { & } to make sure it's acting on what you want it act on and make it easier to follow.

JimboZA:
You might want to give this "if"....

  if(sensorValue < 50) //setting a threshold value

.... a pair if { & } to make sure it's acting on what you want it act on and make it easier to follow.

I did that and now is working just like i wanted.

Thanks for help, you saved my day!