6. Turn on LED when it's dark - Spark Core
Now I know how to use the photo resistor. Let’s create useful application, which would turn on LED when it’s dark :)
I use exact same bread board setting in 5. Photo Resistor.
The point here is we’re using isDark & isOn variables.
int voltage = analogRead(A0); bool isDark = voltage <= 50;
// Turn on LED when it's dark int led = D7; bool isOn = false; void setup() { Serial.begin(9600); pinMode(A0, INPUT); pinMode(led, OUTPUT); digitalWrite(led, LOW); } void loop() { int voltage = analogRead(A0); bool isDark = voltage <= 50; Serial.print(isDark); if (isDark && isOn) { // keep it on } else if (isDark && !isOn) { // it's dark! digitalWrite(led, HIGH); isOn = true; } else { digitalWrite(led, LOW); isOn = false; } delay(500); }
https://github.com/higepon/Spark-Core-Examples/blob/master/6.Turn%20on%20LED%20when%20dark.ino