Measuring RPM using Hall sensor A3144

Between these three different uses of revolutions, the interrupt could have fired a dozen times. You need to copy revolutions and record time, reset revolutions, then diddle around using the values you saved.

You should not be calling millis() twice, either.

Hi Paul,

So in the code below I have made the following changes : put revolutions into revs and millis() into milliseconds a variable first and then do the calculations.

volatile byte revolutions;
byte revs;                                                   //** revolutions goes into this**
unsigned int rpmilli;
float speed;
volatile unsigned long timeold;
unsigned long milliseconds;           // **millis() goes into this**
float wheel_circ = 1.634;

void setup()
{
  Serial.begin(9600);
  attachInterrupt(0, rpm_fun, FALLING);

  revolutions = 0;
  rpmilli = 0;
  timeold = 0;
}

void loop()
{
  if (revolutions >= 10)                         //** Update RPM every 10 counts**
 { 
   revs = revolutions;
   milliseconds = millis();
   
   rpmilli = revs/(milliseconds - timeold);          // **calculate the revolutions per milli(second)**
   timeold = milliseconds;
    revolutions = 0;
    speed = rpmilli * wheel_circ/ 1000;      // speed in meters per second
    speed = 2.237*speed;                    // speed in miles per hour

    Serial.print(" Speed:");
    Serial.print(speed,DEC);
    Serial.println(" mph");
  }
}

void rpm_fun()
{
  revolutions++;
}

Does the code look okay now?