A reed switch will tend to 'bounce' because it's a mechanical contact.
What can happen is that it bounces so fast that the underlying hardware detects a change, but by the time the software gets to check the pin state it has changed again.
If you're using pullup I'd just to a watch on the falling edge of the signal, and would then just ignore anything that happens less than 50ms after the first detected pulse. You could use debounce but it could get thrown off by the very quick changes I said about above.
You're storing lastTime anyway, so it should be pretty easy:
var lastTime;
var RPM;
var w=setWatch(function(e) {
if (e.time<lastTime+0.05) return;
RPM = 60 / (e.time - lastTime);
lastTime = e.time;
counter++;
},
pin,
{repeat: 'true', edge:'rising'}
);
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.
A reed switch will tend to 'bounce' because it's a mechanical contact.
What can happen is that it bounces so fast that the underlying hardware detects a change, but by the time the software gets to check the pin state it has changed again.
If you're using
pullup
I'd just to a watch on the falling edge of the signal, and would then just ignore anything that happens less than 50ms after the first detected pulse. You could use debounce but it could get thrown off by the very quick changes I said about above.You're storing lastTime anyway, so it should be pretty easy: