I have coded a class for this problem. Works great and stable for me. I'm sharing it.
watchClass=function(pin,callback){ var lastTime=0; lastState=1; var interval=setInterval(function(){ currentState=digitalRead(pin); currentTime=getTime(); if (currentState!=lastState) { var e={ state:currentState, lastTime:lastTime, time:currentTime } lastTime=currentTime; lastState=currentState; callback(e); } },30); } pinMode(E2, "input_pulldown"); x=new watchClass(E2,function(e){ console.log(e); });
@fobus started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I have coded a class for this problem. Works great and stable for me. I'm sharing it.