Hall effect sensors

Right Nick, that is exactly what the 2 sketches I posted do.

Problem is that MAX 5000RPM only gives less than 100 pulses per second so counting for a fix 1000 milliseconds and do the math creates steps of 60. Measuring shorter times makes the steps bigger.

There is a 3rd way of measuring, in which one uses a ring-buffer [array] of 60 longs and every second one subtracts the current rpmcount and the content of the corresponding buffer element. This is exact the rpm in the last minute. Then one overwrites that buffer element with the current value.

Please give it a try.

//
//    FILE: rpm3.pde
//  AUTHOR: 
//    DATE: 20-oct-2012
//
// PUPROSE:
//

volatile unsigned long rpmcount = 0;
unsigned long time = 0;
unsigned long timeold = 0;

unsigned long ringbuffer[60];
int idx = 0;

void setup()
{
  Serial.begin(115200);
  attachInterrupt(0, rpm_fun, FALLING);
  for (int i=0; i< 60; i++) ringbuffer[i] = 0; 
}

void loop()
{
  time = millis();
  if (time - timeold >= 1000)
  {
    timeold = time;
    unsigned long rounds = rpmcount;
    unsigned long rpm = rounds - ringbuffer[idx];
    ringbuffer[idx] = rounds;
    idx++;
    if (idx == 60) idx = 0;

    // DO THE OUTPUT
    Serial.print(time);
    Serial.print(", ");
    Serial.println(rpm);
  }
}

void rpm_fun()
{
  rpmcount++;
}

price is of course ~240 bytes of RAM!

  • update -
    This code shows the rpm of the last minute, which is not necessary the current rpm!