Here is the code I wrote if any of you are micro controller guys...
DISCLAIMER... If you build this device, and it hurts you or anyone else... or costs anyone money because it failed or didn't work safely...or..if your wife gets pissed off because you've been in the garage for a week... it's all on you .. build at your own risk... this is all cheap parts, and code written by a moron... you've been warned.
/*
this sketch is used with a photocell / resistor voltage divider to sense ambient light levels
to control a generic 4 relay board where "low" on the board relay input pin
energizes that relay coil, closing the NO contacts. the photocell is connected between 5v and
an analog pin, the 10k relay is connected from that same analog pin to ground
light levels are read in 1024 levels, then divided by 4 to allow byte size levels(255)
2 momentary swithes are used to set low light on and hi light off values.
light level setting values are written to eeprom each time they are set to survive power off state
if both buttons pressed, code restore default values and writes to eeprom
*/
#include <EEPROM.h> //use eeprom library for storage of levels
int led = 13; //onboard LED pin, used to indicate if lights should be on or off
int relay1pin = 9; //relay 1 used to drive indicator on rocker or dash
int relay2pin = 10; //secondary headlight switch (5/6 from dwg on only with HL) relay 2
int relay3pin = 11; //headlamp sw 1/2 (parking, also on w HL) relay 3
int relay4pin = 12; //headlamp sw 1/3 (headlights) relay 4
int switch1pin1 = 2; //on level setting switch
int switch1pin2 = 4; //on level setting switch - this pin is forced as output to low to be used as a switch "ground"
int switch2pin1 = 5; //on level setting switch
int switch2pin2 = 7; //on level setting switch - this pin is forced as output to low to be used as a switch "ground"
int sw1state = 0; // variable for switch 1 state
int sw2state = 0; // variable for switch 2 state
int onlevel; //threshold level where relays 2,3,4 are turned on (lights on)
int offlevel; //threshold level where relays 2,3,4 are turned off (lights off)
int defaultonlevel = 75; //threshold level where relays 2,3,4 are turned on (lights on)
int defaultofflevel = 175; //threshold level where relays 2,3,4 are turned off (lights off)
int cycletime = 250; // photoresitor sample time in ms
int offdelay = 10000; //delay to wait to resample level and then turn lights off if still above off level in ms
int headlampstate = 0; //state of headlamps 0=lights are off 1=lights are on
int photocellPin = 7; // analog pin that the cell and 10K pulldown are connected to
int photocellReading; // the analog reading from the photocell/resitor voltage divider (light sensor)
void setup(void) { // boot up values
Serial.begin(9600); //for debug
pinMode(switch1pin1, INPUT_PULLUP); //used to pull switch high by default
pinMode(switch2pin1, INPUT_PULLUP); //used to pull switch high by default
pinMode(switch1pin2, OUTPUT); //set pins as outputs
pinMode(switch2pin2, OUTPUT);
pinMode(relay1pin, OUTPUT);
pinMode(relay2pin, OUTPUT);
pinMode(relay3pin, OUTPUT);
pinMode(relay4pin, OUTPUT);
digitalWrite(led, LOW); //turn NANO indicator led off
digitalWrite(switch1pin2, LOW); //set to ground level 0 volts
digitalWrite(switch2pin2, LOW); //set to ground level 0 volts
digitalWrite(relay1pin, LOW); //close relay 1 contact, use for dash indicator
digitalWrite(relay2pin, HIGH); //open relay 2 contact, lights off
digitalWrite(relay3pin, HIGH); //open relay 3 contact, lights off
digitalWrite(relay4pin, HIGH); //open relay 4 contact, lights off
onlevel = EEPROM.read(1); //get threshold level (lights on) from eeprom and set program variable to match
offlevel = EEPROM.read(0); //get threshold level (lights off) from eeprom and set program variable to match
}
void loop(void) { //get to work
photocellReading = analogRead(photocellPin) / 4; //sample light sensor then divide by 4 so values can be stored in single bytes in eeprom
sw1state = digitalRead(switch1pin1); //sample switch position
sw2state = digitalRead(switch2pin1); //sample switch position
// debug info output via the Serial monitor
Serial.print(" Light = "); //For Diagnostics
Serial.print(headlampstate);
Serial.print(" SW1 = ");
Serial.print(sw1state);
Serial.print(" SW2 = ");
Serial.print(sw2state);
Serial.print(" On = ");
Serial.print(onlevel);
Serial.print(" Off = ");
Serial.print(offlevel);
Serial.print(" reading = ");
Serial.println(photocellReading);
delay(cycletime); // wait a little bit each cycle then check switch positions and compare light sensor value to on and off levels
if (sw1state == 0) { //if someone pushed switch one, set on level to current light sensor value
onlevel = photocellReading;
EEPROM.write(1, onlevel); //write current onlevel to eeprom location 1
digitalWrite(relay1pin, HIGH); //blink relay 1 once to acknoledge button press
delay(500);
digitalWrite(relay1pin, LOW);
}
if (sw2state == 0) { // see if someone pushed switch two
if (sw1state == 0) { //see someone also pushed switch one, if so set on and off levels to default, and blink status light 3 times
onlevel = defaultonlevel;
offlevel = defaultofflevel;
EEPROM.write(1, onlevel); //write current onlevel to eeprom location 1
EEPROM.write(0, offlevel); //write current offlevel to eeprom location 0
digitalWrite(relay1pin, HIGH); //blink relay 1 3X to indicate reset to default
delay(500);
digitalWrite(relay1pin, LOW);
delay(500);
digitalWrite(relay1pin, HIGH);
delay(500);
digitalWrite(relay1pin, LOW);
delay(500);
digitalWrite(relay1pin, HIGH);
delay(500);
digitalWrite(relay1pin, LOW);
}
else {
offlevel = photocellReading; // if only sw2 was pressed, set off level to current light level
EEPROM.write(0, offlevel); //write current offlevel to eeprom location 1
digitalWrite(relay1pin, HIGH); //blink relay 1 once to acknoledge button press
delay(500);
digitalWrite(relay1pin, LOW);
}
}
if (photocellReading > offlevel){ //check to see if it is bright nuff to turn or keep lights off
if (headlampstate == 1){ //see if lights are on, if so get ready to shut em off
delay(offdelay); //wait to be sure it wasnt just a momentary bright spot
photocellReading = analogRead(photocellPin) / 4; //read photocell and divide that by 4 again
if (photocellReading > offlevel)headlampstate = 0; //if its still bright enough after waiting.. turn em off
}
}
if (photocellReading < onlevel){ //see if its dark enough to turn lights on
headlampstate = 1; //set variable to say turn em on
}
// logic to actuate relays
if (headlampstate == 0){ // zero means turn them off
digitalWrite(led, LOW); //turn indicator LED on NANA off
digitalWrite(relay2pin, HIGH); //open relay 2 turn off lights
digitalWrite(relay3pin, HIGH); //open relay 3 turn off lights
digitalWrite(relay4pin, HIGH); //open relay 4 turn off lights
}
else {
digitalWrite(led, HIGH); // anything but zero means turn em on... turn on LED on NANO to indicate
digitalWrite(relay2pin, LOW); // close relay 2 to turn on lights
digitalWrite(relay3pin, LOW); // close relay 3 to turn on lights
digitalWrite(relay4pin, LOW); // close relay 4 to turn on lights
}
} //back to the top of the loop