int ledPin = 13;
int LDR_PIN = 2;
int switchState1 = 0;
int switchPin1 = 5;
int switchState2 = 0;
int switchPin2 = 4;
int interval = 300;
int state = 1;
unsigned long timenow = 0;
unsigned long timestart = 0;
void setup() {
// put your setup code here, to run once:
pinMode(LDR_PIN, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(switchPin1, INPUT_PULLUP);
pinMode(switchPin2, INPUT_PULLUP);
unsigned long timestart = millis();
}
void loop() {
// put your main code here, to run repeatedly:
switchState1 = digitalRead(switchPin1);
if(switchState1 == HIGH){
switchState2 = digitalRead(switchPin2);
if(switchState2 == HIGH){
//auto-mode photoresistor with a state and when switching start measuring time and timediff is > 300 && dark turn led on else off if >300 && light
timenow = millis();
if(digitalRead(LDR_PIN) == LOW){ //licht gemessen
if(state == 1){ // ist schon hell
if(timenow - timestart >= interval){ //lange genug Hell für Ausschalten
digitalWrite(ledPin, LOW);
}
}else{
timestart = timenow;
state = 1;//Wechsel von vorher Dunkel zu Hell
}
}else{ //dunkel
if(state == 0){ // ist schon dunkel
if(timenow - timestart >= interval){ //lange genug Dunkel für Anschalten
digitalWrite(ledPin, HIGH);
}
}else{
timestart = timenow;
state = 0;//Wechsel von vorher Hell zu Dunkel
}
}
}else{
//ON
digitalWrite(ledPin,HIGH);
}
}else{
//OFF
digitalWrite(ledPin,LOW);
}
}