 
 // Shows the current delta on the display (using current units)
 void show_delta( unsigned long delta, int units ) {
-  if( delta==0 ) { 
-    switch( units ) { // Font is patched to have -. at 0x1A
+  if( delta==0 ) { // No delta available, show dashes
+    switch( units ) { // Font is patched to have '-.' at 0x1A
       case 0: dsp_set("-"); break;
       case 1: dsp_set("\x1A-"); break;
       case 2: dsp_set("\x1A--"); break;
     }
-  } else {
+  } else { // delta is rotation time in us
+    // Compute derived values - the additions below are for rounding
     int rpm= (60*1000*1000+delta/2)/delta;  // units: rotations per minute
-    int ms10= (delta+50)/100;               // units: 1/10 microseconds
-    int hz100= (100*1000*1000+delta)/delta; // units: 1/100 herz
+    int ms10= (delta+50)/100;               // units: 1/10 milliseconds
+    int hz100= (100*1000*1000+delta/2)/delta; // units: 1/100 hertz
     switch( units ) {
-      case 0: dsp_set(tostr(rpm,0)); break;
-      case 1: dsp_set(tostr(ms10,1)); break;
+      case 0: dsp_set(tostr(rpm  ,0)); break;
+      case 1: dsp_set(tostr(ms10 ,1)); break;
       case 2: dsp_set(tostr(hz100,2)); break;
     }
   }

// Arduino entry points
// ============================================================

#define TOGGLE 1000    // Time (in ms) between displaying units and delta (when not rotating)
int units;             // With the 'flash' buttons, the units can be chosen. This variable hold the current mode.
int olddelta;          // Last valid measurement
unsigned long oldtime; // Last time the display was refreshed (when not rotating)

void setup() {
  // Init serial port (over USB), matrix and sensor
  Serial.begin(115200);
  dsp_init();
  sen_init();
  // Init program variables
  units= 0; // 0=rpm, 1=ms, 2=Hz
  olddelta= 0;
  // Welcome messages
  Serial.printf("\n\nWelcome at mRPM\n");
  dsp_set("mRPM");
  delay(3000);
}

void loop(){

// RPM measurement tool for Jan Ridders
//     2017 July 16 by Maarten Pennings  v2  Removed 'String', fixed spelling errors, fixed error in hz100, added but_init(), added #defines
//     2017 July 10 by Maarten Pennings  v1  Created
// Hardware used:
//   - RobotDyn NodeMCU V3 ESP8266 32M flash USB-serial CH340G
//     https://www.aliexpress.com/item/NodeMCU-WIFI-module-integration-of-ESP8266-extra-memory-32M-flash-USB-serial-CH340G/32739832131.html?
