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

/* * Turn on an LED when light level falls below a certain level. You * might need to change the levels to reflect your environment. */ void setup() { pinMode(A2,INPUT_PULLUP); //Light sensor pinMode(6,OUTPUT); //LED } void loop() { //Read the light sensor const int lightValue = analogRead(A2); //Between 150 and 400, the light will stay in whatever state // it is in. When a threshold is hit, the state changes if (lightValue > 400){ //If dark enough, turn LED on digitalWrite(6,HIGH); } else if (lightValue < 150){//If light enough, turn off digitalWrite(6,LOW); } } //Change the thresholds to adjust when the light turns on and off // (c) 2017 Let's Start Coding. License:

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.