Pages: [1]   Go Down
Author Topic: Measuring the RPM using Pulses  (Read 690 times)
0 Members and 1 Guest are viewing this topic.
New Delhi,India
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50956
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: