Hi all
I'm making a tachometer and I have some code that's working quite well, It
- counts the number of times a photodiode is tripped
- 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 noobunsigned 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=(countsdivisor/period);
Serial.println(rpm);
counts=0;
runs=0;
last=0;
finished=0;
interrupts();
time=micros();}
runs++;
}
void ping(){
counts++;
if(last){
finished=1;
}
}