Hey Guys,
I am working on RMP measurement program on arduino, most of the rpm measurement reference code runs for ever and I don't want that I want to implement it as function which I can call when ever RPM value is necessary. So I wrote the code and it did complied but I am waiting on my hall effect sensor to arrive so I cannot test it right now so I just want to show you guys the code and see if the general implementation is correct or not? Code certainly is not the finished as you will see I don't make any use of rpm value in the loop but right now I just want to figure out the rpm part is right.
Here is the code
boolean pool = 0;
unsigned long lastmillis = 0;
int rpm = 0;
volatile int rpmcount = 0;
void get_rpm(){
lastmillis = millis(); //as its a funtion which can be called at any time we upadate current time
attachInterrupt(0, rpm_fan, FALLING); //attach interrup
while(pool==0){ //wait for 1 sec to complete in the ISR
//do nothing
}
pool =0; //reset pool
rpm = rpmcount *60; //convert to rpm
rpmcount = 0; //reset rpmcount
}
void rpm_fan(){ //things to do when there is an interrupt
if (millis() - lastmillis == 1000){ //after one second
detachInterrupt(0); //Disable interrupt
pool=1;
}
else rpmcount++;
}
void setup() {
}
void loop() {
get_rpm();
}