Hey all,
I have been trying to find the rpm of a motor using the hall effect sensor to pick up the magnetic pulses,and i have written this code which is able to detect the number of pulses and their duration.
int inputPin = 3;
unsigned long counter = 0;
unsigned long duration = 0;
unsigned long timeout = 1000000; // in microseconds
void setup() {
pinMode(inputPin, INPUT);
// the input pin to a HIGH reading.
digitalWrite(inputPin, HIGH);
Serial.begin(9600);
Serial.println("start");
}
void loop() {
duration = pulseIn(inputPin, HIGH, timeout);
{
counter++;
Serial.print(counter);
Serial.print(", ");
Serial.print(duration);
Serial.println("");
}
}
Here the counter keeps incrementing and ,the final output gives me the number of high's and the respective durations.Now the problem is i wanted the number of pulses in a given time interval,like for eg: the output should be like " 3 , 0.5sec" which means the 3 pulses in 0.5 seconds.or "1 ,0.5 sec"...and not just an up counter..i tried refreshing the counter after the loop that is , put a statements counter =0, and duration =0; at the end but it didnt work,i am thinking about using the attachinterrupt command..?..any help guys?..i havent yet written the code to convert this into rpm.