Skip to content

Instantly share code, notes, and snippets.

@dexterlabora
Created September 21, 2015 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dexterlabora/8a113a206fda2bb73f4f to your computer and use it in GitHub Desktop.
Save dexterlabora/8a113a206fda2bb73f4f to your computer and use it in GitHub Desktop.
Save energy by detecting motion to turn on lights and then turning them off after 5 minutes of inactivity.
// energysave.js
// www.internetoflego.com
// Dependencies
var five = require("johnny-five");
var board = new five.Board();
// Arduino board connection
board.on("ready", function() {
// Motion sensor components
var motionSensor = new five.Sensor.Digital({
pin: 52,
});
var motionLed = new five.Led({
pin: 42,
});
// Disco Light
var discoLed = new five.Led({
pin: 28,
});
// City Lights
var cityLed = new five.Led({
pin: 8,
});
// Sleepmode logic
var sleepMode;
motionSensor.on('change', function(){
motionLed.on();
setTimeout(function(){
motionLed.off();
},2000);
// Wake up these
cityLed.on();
discoLed.on();
// Reset sleep timer
clearTimeout(sleepMode);
console.log("sleep timer reset");
// Set sleep timer
sleepMode = setTimeout(function() {
console.log("going to sleep");
// Sleep these
cityLed.off();
discoLed.off();
}, 1000*60*5); // Sleep in 5 minutes
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment