Hi,
I am building a speedometer for my motorcycle. I am using a reed sensor and magnet from an bicycle speedometer to pick up the pulses on an interrupt pin. What is the best approach to convert those pulses to km/h?
This is the code i use now, but i don't get a steady readout.
record the time difference between successive pulses (use millis() or micros()) and calculate the speed - you need to know/measure the distance covered per pulse.
It should be possible because i tested the sensor using the original bike speedometer and that one gave a good readout (it can mesure up to 300km/h). But with my code the value changes all the time between 0 and 100 while i was driving +-25km/h
Keep an unsigned long variable 'lastTime' recording the time you last incremented the speed counter. In the ISR, call micros() and subtract lastTime to get the interval since the last update. If it is less than the smallest reasonable interval, ignore the interrupt. Else increment the speed counter and store the value that was returned by micros in lastTime.
dc42 nailed it. You're probably getting multiple interrupts because of contact bounce. Another way to overcome this is to have a "holdoff" time. When the 1st interrupt occurs, disable the interrupt for a small, reasonable amount of time during which the contacts will bounce, and re-enable it later.
Humm... I would start by trying 5-10% of the time the wheel takes for a full turn at the "current" RPM, and some 50ms - 100ms near 0 RPM... needs tuning, specially at near stopped.
Reed switches have quote a lot of hysteresis, so I don't think the debounce time needs to be varied. It just needs to be long enough to suppress bounces, but not so long as to treat genuine pulses as bounces at or near maximum speed.