Measuring speed of bike using inductive sensor.


I have used one inductive sensor and placed my sensor on bike frame such that whenever the spokes of bike coincide with the sensor, it will generate the magnetic field surrounding the coil changes. So what I did is first determine the RPM value and then multiplied the RPM value with circumference of the wheel. The result would be in meters per minute. I am not getting right result. Please help. I think that my formula is not right?

volatile byte revolutions;

unsigned int rpmilli;
float speed;

unsigned long timeold;

void setup()
  attachInterrupt(0, rpm_fun, RISING);

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

void loop()
  if (revolutions >= 20) { 
    //Update RPM every 20 counts, increase this for better RPM resolution,
    //decrease for faster update

    // calculate the revolutions per milli(second)
    **rpmilli =  revolutions/(millis()-timeold)

    timeold = millis();
    *  revolutions = 0;

    // WHEELCIRC = 2 * PI * radius (in meters)
    // speed = rpmilli * WHEELCIRC * "milliseconds per hour" / "meters per kilometer"

    // simplify the equation to reduce the number of floating point operations
    // speed = rpmilli * WHEELCIRC * 3600000 / 1000
    // speed = rpmilli * WHEELCIRC * 3600

    speed = rpmilli * WHEELCIRC * 3600;

    Serial.print(rpmilli * 60000,DEC);
    Serial.print(" Speed:");
    Serial.println(" kph");

void rpm_fun()

There is little that is actually correct in your code.

For example: on the right side of the line below, you are dividing a byte value by an unsigned long value. What do you expect the result to be?

What do you think the left side does?

    **rpmilli =  revolutions/(millis()-timeold)

Hii Jerimgton,

It will provide me an float value.

It will provide me an float value.

The result of the right hand side will almost always be zero, never any floating point value.

The program can't be compiled, but even if so, the result would be stored in some arbitrary place in memory, perhaps crashing your program.