Skip to content

Instantly share code, notes, and snippets.

@mkol5222
Forked from navio/puckLight.js
Created November 15, 2018 08:55
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 mkol5222/9748c5bfb17080d84f7528227d4558c2 to your computer and use it in GitHub Desktop.
Save mkol5222/9748c5bfb17080d84f7528227d4558c2 to your computer and use it in GitHub Desktop.
PuckJS - Light switch
const LEDS = [LED1,LED2,LED3];
const onBtnClick =
(fn)=> setWatch(fn, BTN, {edge:"rising", debounce:50, repeat:true});
const turnOn = (el) => {
el.write(true);
};
const turnOff = (el) => {
el.write(false);
};
const schedule = (fn,time) => new Promise(function(ac){
(new Promise((a,r)=>{
a(fn());
})).then(()=>{
setTimeout(ac,time);
});
});
let current = 0;
let currentLed = LEDS[current];
onBtnClick(function(){
schedule(()=>{
console.log('blip');
turnOn(currentLed);
},3000)
.then(()=>{
turnOff(currentLed);
current = (current > 1) ? 0 : current + 1;
currentLed = LEDS[current];
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment