Go Down

Topic: Measuring the RPM using Pulses (Read 731 times) previous topic - next topic

jay2012

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.


PaulS

Quote
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

You don't want to unconditionally reset counter. Resetting duration is silly, since it is always value by the pulseIn() function.

You need to determine if it is time to reset counter. Look at the millis() function, and the blink without delay example to see how to time activities.

Go Up