Nutsy wrote:So how do i use timers and interrupts? Im sorry its a relatively new concept to me...
There are a number of examples on this site, take a look at this..
... and google is your friend... the whole site is indexed by google, so try this for a little more light bedtime reading...
https://www.google.co.uk/search?client= ... 8gfB_ZiwDA
The idea is actually very simple... you create a very short routine, called an interrupt service routine. This is triggered every time a particular interrupt is triggered and .. services that interrupt.
For example if you set up an interrupt to happen every time a certain pin changes state, or every time it goes low. then when that event happens, your interrupt service routine (ISR) will be called automatically by the ARM processor hardware. In your ISR, you might for example place the value of the millis() timer in a certain memory location or variable, or array or whatever, set a memory flag somewhere to indicate you have serviced the routine then return from the routine.
In your main loop, you check to see if the flag has been set, and if so you calculate your rpm based on the difference between the most recent value saved by the ISR, and the previous one. then move the most recent value to the previous value variable, then clear the interrupt flag and continue round your loop doing whatever else you fancy. Since the ISR is triggered by an event outwith the main loop, it needs to return quickly, otherwise there is a risk that you will either get a second interrupt, thus overriding the value from the first, or setting up a situation where your ISR never gets a chance to complete, or you will spend so much time in the ISR that it impacts on your main loop. Hence the mantra.. Always keep your ISR as short as possible.
Designing interrupt based solutions can sometimes be a bit of a head scratcher, but I'm sure if you look through the examples, you will pick up the idea fairly easily.