I have created this code to control the turning off and on of my car headlights based on the sensor value of a photo cell. That part I got working, but what I want to do is once the headlights are on there needs to be 10 seconds of brightness before the headlights will turn off again in order to reduce flicker. I do want to do this without using delay as I will be expanding upon this code to do much more. Im having nothing but problems trying to get it to work using Millis, anyone have any ideas? Thanks
// initialize controllers and variables
int sensorPin = A0; // imput from photosensor
const int switchPin = 12; // imput from headlight combination switch, headlights + tail lights
const int autoswitchPin = 2; // imput from combination switch, now used for turning auto headlights on
int headlightController = 4; // output to headlight controller relay
int runninglightController = 5; // output to running light controller relay
int dashlightController = 6; // output to dash light controller relay
int autoswitchCutoff = 7; // this out put activates a normally closed relay circuit to act as a shutoff to the auto switch
// begin variables
int sensorValue = 0; // variable to store the value coming from the sensor
int switchValue = 0; // variable to store the value coming from the switch
int autoswitchValue = 0; // variable to store the value coming from the auto switch
unsigned long startTime; // the value returned from millis when the switch is pressed
int duration = 10000; // variable to store the duration
unsigned long previousMillis;
void setup() {
// initialize outputs
pinMode(headlightController, OUTPUT);
pinMode(runninglightController, OUTPUT);
pinMode(dashlightController, OUTPUT);
pinMode(autoswitchCutoff, OUTPUT);
// initialize inputs
pinMode(switchPin, INPUT);
pinMode(autoswitchPin, INPUT);
digitalWrite(2, HIGH);
Serial.begin(9600);
}
void loop(){
unsigned long currentMillis = millis(); // Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statement
unsigned long startTime = currentMillis - previousMillis;
// read the state of the switch value
switchValue = digitalRead(switchPin);
// read the state of the auto switch value
autoswitchValue = digitalRead(autoswitchPin);
// read the value from the photo sensor
sensorValue = analogRead(sensorPin);
Serial.print("Digital reading = ");
Serial.println(sensorValue); // serial monitor the raw analog reading of the sensor
// manual lighting mode
if (switchValue == LOW)
digitalWrite(autoswitchCutoff, HIGH);
else {
digitalWrite(autoswitchCutoff, LOW);
}
if (switchValue == LOW)
digitalWrite(headlightController, HIGH);
else {
digitalWrite(headlightController, LOW);
}
if (switchValue == LOW)
digitalWrite(runninglightController, HIGH);
else {
digitalWrite(runninglightController, LOW);
}
if (switchValue == LOW)
digitalWrite(dashlightController, HIGH);
else {
digitalWrite(dashlightController, LOW);
}
// auto lighting section
if((autoswitchValue == LOW) && (sensorValue < 410)) {
digitalWrite(headlightController, HIGH);
previousMillis = currentMillis;
}
while((sensorValue > 410) && ( startTime = 10000)) {
digitalWrite(headlightController, LOW);
}
}