Help with rpm counter coding

So in my school we had to make a project that consisted in a rpm counter that inputs the results into a lcd screen like this

So I took this concept: http://duino4projects.com/wp-content/uploads/2013/05/Arduino-RPM-Counter.gif

and used this code :

/*
 * Optical Tachometer
 *
 * Uses an IR LED and IR phototransistor to implement an optical tachometer.
 * The IR LED is connected to pin 13 and ran continually.
 * Pin 2 (interrupt 0) is connected across the IR detector.
 *
 */

int ledPin = 13;                // IR LED connected to digital pin 13
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;

// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void rpm_fun()
 {
   
      rpmcount++;
 
 }

void setup()
 {
   lcd.begin(16, 2);  // intialise the LCD

   //Interrupt 0 is digital pin 2, so that is where the IR detector is connected
   //Triggers on FALLING (change from HIGH to LOW)
   attachInterrupt(0, rpm_fun, RISING);

   //Turn on IR LED
   pinMode(ledPin, OUTPUT);
     digitalWrite(ledPin, HIGH);

   rpmcount = 0;
   rpm = 0;
   timeold = 0;
 }

 void loop()
 {
   //Update RPM every second
   delay(1000);
   //Don't process interrupts during calculations
   detachInterrupt(0);
   
   rpm = 60*1000/(millis() - timeold)*rpmcount;
   timeold = millis();
   rpmcount = 0;

 
   lcd.clear();
   lcd.print("RPM=");
   lcd.print(rpm);
  
   //Restart the interrupt processing
   attachInterrupt(0, rpm_fun, RISING);
  }

The only thing I don't understand is the math used.
In other words, I don't understand this line: rpm = 60*1000/(millis() - timeold)*rpmcount;

Can someone explain to me the math being used? And if possible, can someone show me a way to improve the code in order to switch the display from rpm's to km/h ? That would be awesome thanks a lot ! :sweat_smile:

rpm is rotations per minute, thus mathematically it is rotation count divided by elapsed time (in minutes). In your formula mills()-timeold is the elapsed time in milliseconds and rpmcount is the rotation count. If you divide these you get rotations per millisecond. Since there are 60,000 milliseconds in a minute, the 60*1000 represents the conversion factor to convert rotations per millisecond to rotations per minute.

The equation in the code is equivalent to this ...

rpm = 60 * 1000 * rpmcount / (millis() - timeold);

Ray

To get km/ h, convert rpm to rph by multiplying by 60. Then multiply by the circumference of the wheel.