Created
September 27, 2018 11:37
-
-
Save navio/86741f3cf70af0778a555d736316abde to your computer and use it in GitHub Desktop.
PuckJS - Light switch
This file contains 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
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