# Automotive light sensor

Hi,

I’m just starting to work with arduino and it’s programming language.

Because of my profession as a car technician in a off road brand dealer ship I’ve installed a lot of aftermarket lights on vehicles.
Because some off road vehicles have a lot of extra lights, you have to put a lot of extra ugly switches inside the car.

Therefore my first project is an in car system that reports different kinds of information about the car and can execute some things.
For the first function a made a light sensor based on 2 LDR’s that came with the arduino starters kit i bought.

It was quiet easy to switch on and off a LED based on the average of the 2 sensors using voltage deviders.
But when this would be in a car, you don’t want your lights to go on the milisecond you pas under a bridge, or at night, when you drive under a light.

I worked on this function literely a few hours because I knew what the sensor had to do, but translate it in the programming language with statements and cover all situation was hard.
I learned a lot with writing this code, especially about millis().

``````// Set constant integers for LDR - Light Depended Resistor pin numbers
const int LightSensor1Pin = 0;      // Arduino A0
const int LightSensor2Pin = 1;      // Arduino A1
// Set the LightSensor variable to keep track of the avarage of the two LDR's
int LightSensor = 0;

// Settings for light sensor
// The threshold daytime/night time
const int Trigger = 750;
// 2 variables to keep track if the function was counting to go on or off
boolean CountOn = false;
boolean CountOff = false;
// Variales to be used inside the function
// Variable to use to see how long the arduino is running in milliseconds
unsigned long MillisTemp;
// Variables to be set with the uptime of the arduino in ms to calculate
// how long it's above or under the threshold
unsigned long TriggerCountOn;
unsigned long TriggerCountOff;
// Variables to store the result of the calculation
unsigned long TriggerCountOn2;
unsigned long TriggerCountOff2;

// Variable to keep track of the state of the LED, will be used to digitalWrite to OUTPUT
boolean LightSensorState = false;
// Output pin to write state of LightSensorState
const int LightSensorOutputPin = 12;

// Function to calculate average from multiple LDR sensors
int CalculateLight() {
return LightSensor;
}

// Background running function @ each loop for managing the lightsensor
// Some settings you can adjust for the On delay time and Off delay time in milliseconds
// Delay Off time will also be used to overlap light at night
// Delay On time will also be used to overlap Bridges etc at daytime
const long DelayOn = 5000;
const long DelayOff = 30000;
// The function, run LightSensorActivate() @ beginning of loop();
void LightSensorActivate() {
LightSensor = CalculateLight();
if(LightSensor <= Trigger && !LightSensorState) {
if(CountOff) {
TriggerCountOff2 = 0;
CountOff = false;
}
if(CountOn) {
MillisTemp = millis();
TriggerCountOn2 = MillisTemp - TriggerCountOn;
if(TriggerCountOn2 >= DelayOn) {
LightSensorState = true;
CountOn = false;
TriggerCountOn2 = 0;
}
}
else {
CountOn = true;
TriggerCountOn = millis();
}
}
else if(LightSensor <= Trigger && LightSensorState) {
if(CountOff) {
TriggerCountOff2 = 0;
CountOff = false;
}
}
else if (LightSensor > Trigger && LightSensorState) {
if(CountOn) {
TriggerCountOn2 = 0;
CountOn = false;
}
if(CountOff) {
MillisTemp = millis();
TriggerCountOff2 = MillisTemp - TriggerCountOff;
if(TriggerCountOff2 >= DelayOff) {
LightSensorState = false;
CountOff = false;
TriggerCountOff2 = 0;
}
}
else {
CountOff = true;
TriggerCountOff = millis();
}
}
else if(LightSensor > Trigger && !LightSensorState) {
if(CountOn) {
TriggerCountOn2 = 0;
CountOn = false;
}
}
digitalWrite(LightSensorOutputPin, LightSensorState);
}

void setup() {
pinMode(LightSensorOutputPin, OUTPUT);
}

void loop() {
LightSensorActivate();
}
``````

My question to you guys is:

1. Can my function be written in a better, more efficient way?
2. Are there better kinds of light sensors to use in this kind of application?