PROJECT: Dual Threshold Nightlight
Step 1 - Build the Project
Create a nightlight that turns on when it's dark out and stays on until it's gotten bright. Two light thresholds means the light won't flicker.
Step 2 - Upload the Code
Step 3 - Read the Walkthrough
In the setup() section of the code, set the pinMode of your light sensor pin to INPUT_PULLUP. That way, when the sensor sees very bright light it sends in a value close to 0 to Maker Board. When it's dark, the value is closer to 1000.
Set the LED pin as an OUTPUT in the code so it receiving instructions from the code, not trying to send data in to the code.
In the loop() section, you will first create an integer-type variable named lightValue. When you declare the value of this variable as analogRead(A2); , you are saying "When I use the variable lightValue, I am using it in place of analogRead(A2);. However, the value of tempValue only updates to analogRead(A2) when the loop hits this line of code, not every time you use lightValue.
Next up are your two 'if' statements. In the first 'if', you're checking to see "Is it true that the lightValue is higher than 400?". If it is true, the code within the 'if' statement runs, turning on the LED on pin 6.
Only if the first 'if' statement is false is the 'else-if' statement checked. Otherwise, it's skipped. This statement asks "Is the lightValue less than 150?". If so, digitalWrite pin 6 LOW, turning the LED off.
If the light value is somewhere between your thresholds (like 250), then neither statement is true, so neither 'if' statement runs. That means your LED will 'hold its state' for every value between the thresholds. If you're using the nightlight near a window, the light should turn on only once a night and turn off only once per morning.