#define TACHO 3 //can only be 2 or 3 if a 328 based board.
#define READINGS 10
volatile uint16_t pulseCount=0;
uint32_t lastRead;
uint16_t timeToNext=1000/READINGS; //read ten times per second
uint16_t rpm;
uint8_t readings[READINGS];
uint16_t total;
void setup() {
Serial.begin(115200);
pinMode(TACHO,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(TACHO),interrupt,RISING);
tone(8,60);
}
void interrupt(){
pulseCount++;
}
void loop(){
if((millis()-lastRead)>timeToNext){
lastRead=millis();
cli(); //prevent interrupting following two lines
rpm=pulseCount; //get the pulse count
pulseCount=0;
sei(); //enable interrupts again
total=rpm;
for(uint8_t i=0;i<READINGS-1;i++){ //scroll the readings and sum them as we go
total+=readings[i+1];
readings[i]=readings[i+1];
}
readings[9]=rpm;
Serial.println(total*60);
}
}