Created
October 25, 2016 17:49
-
-
Save LosantGists/1b6c085556f1ea7b1a9d568003614524 to your computer and use it in GitHub Desktop.
A night light in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var interval = 1000 | |
| var isLedOn = false | |
| var lightThreshold = 0.7 | |
| // Initilize the LED to be off | |
| digitalWrite(D5, false) | |
| setInterval(function() { | |
| console.log("Light Level: " + analogRead()) | |
| // Read Photo Cell from A0 | |
| var lightLevel = analogRead() | |
| if (lightLevel < lightThreshold && !isLedOn) { | |
| console.log("It's Dark. Turning light on") | |
| digitalWrite(D5, true) // Send HIGH to D1 on the NodeMCU | |
| isLedOn = true | |
| } | |
| if (lightLevel > lightThreshold && isLedOn) { | |
| console.log("It's Light. Turning light off") | |
| digitalWrite(D5, false) // Send LOW to D1 on the NodeMCU | |
| isLedOn = false | |
| } | |
| }, interval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment