Timer overflow interrtups - how ?

Hi all

I'm making a tachometer and I have some code that's working quite well, It

  1. counts the number of times a photodiode is tripped
  2. every set amount of time it works out counts/microseconds since last checked.

the 'set amount of time' though is determined by a pretty crude method, so i was wondering how do I set up a timer overflow interrupt ?
I've seen a couple of examples but really can't work out what anything means! Here's my code just in case:

// this updates RPM at least every 50000 runs, up to a max rate
// of every 4000 runs. This is about to 22 to 289ms
// it starts timing from the moment it finishes doing maths to
// the moment the next blade passes (after min time has elapsed)

// if u wonder why I don't just use a timer instead of 'runs', it's
// because i'm a noob :smiley:

unsigned long time=0;
unsigned long time2=0;
unsigned long rpm;
unsigned long period;
volatile unsigned long counts=0;
int potpin=0; // <---- change to suit your motor and pot pin
int motorpin=5;
int motor;
byte last=0;
byte finished=0;
byte first=1;
byte started=0;
int blades=6; // <--- change this, it's number of blades
unsigned long divisor;
unsigned long runs=0;
void setup(){
Serial.begin(9600);
attachInterrupt(0,ping,RISING);
divisor=60000000/blades;
}

void loop(){
if(runs>4000){
last=1;
}
if(finished || runs>50000){
time2=micros(); /* putting noInterrupts after reading the time
means for some reason the time is more accurate, although you run
the risk of interrupt tripping between reading time and
calling noInterrupts, but srsly how likely is that ? /
noInterrupts();
period=(time2-time);
rpm=(counts
divisor/period);
Serial.println(rpm);
counts=0;
runs=0;
last=0;
finished=0;
interrupts();
time=micros();

}
runs++;
}
void ping(){
counts++;
if(last){
finished=1;
}
}

The Arduino-based interrupts do not support the Timers. Your code and your description don't really seem to match.

I'm finding it difficult to understand why you need to keep track of time. How critical is the timing? Do you really need a time based interrupt or can you just check your variable in the loop (as looks like you are doing now)?

It's not timing critical, but as it is now it takes trial and error to 'set' the time interval. I want an easier way to set the interval

Plus i've always wanted to know how to do a timer interrupt. So far i know it starts with

ISR(TIMER0_OVF_vect . .. .

but how can i set how often it will overflow ?

thanks :smiley:

prawnstar:
It's not timing critical, but as it is now it takes trial and error to 'set' the time interval. I want an easier way to set the interval

Plus i've always wanted to know how to do a timer interrupt. So far i know it starts with

ISR(TIMER0_OVF_vect . .. .

but how can i set how often it will overflow ?

thanks :smiley:

You could always download the MsTimer2 library and read their source code: Arduino Playground - MsTimer2

Tutorial on AVR timers.
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=50106