Skip to content

Instantly share code, notes, and snippets.

@LosantGists
Created October 25, 2016 17:49
Show Gist options
  • Select an option

  • Save LosantGists/1b6c085556f1ea7b1a9d568003614524 to your computer and use it in GitHub Desktop.

Select an option

Save LosantGists/1b6c085556f1ea7b1a9d568003614524 to your computer and use it in GitHub Desktop.
A night light in Javascript
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