millis coding explained (?)

Hello… i got arduino coding that use to count RPM, here the code:

#include <Wire.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 5); // RX, TX
const int sensor = 2;
//const int hijau = 9;
//const int kuning = 10;
//const int merah = 11;
//const int buzz = 8;
int sensorstate = LOW;
int laststate = LOW;
volatile byte rpmcount;
unsigned int rpm;
int kmph;
float kroda;
float a; //Satuan cm/menit ke km/h
unsigned int r;
unsigned long timeold;
int kondisi;

void rpm_fun()
{
  rpmcount++;
}

void setup() {
  attachInterrupt(0, rpm_fun, FALLING);
//  pinMode(sensor, INPUT);
//  pinMode(hijau, OUTPUT);
//  pinMode(kuning, OUTPUT);
//  pinMode(merah, OUTPUT);
//  pinMode(buzz, OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9900);

  rpmcount = 0;
  rpm = 0;
  timeold = 0;
  a = 0.0006;
  //====== INPUT UKURAN JARI-JARI RODA ==========//
  r = 23;
  //============================================//

}

void loop() {
  sensorstate = digitalRead(sensor);

  if (sensorstate != laststate) {
    if (sensorstate == HIGH) {
      detachInterrupt(0);
      //=========== FORMULA =================//
      rpm = 30 * 1000 / (millis() - timeold) * rpmcount;
      timeold = millis();
      rpmcount = 0;
      //kroda=2*3.14*r;
      kmph = rpm * (2 * 3.14 * 23) * a; //*0,0006;
    }

    {
      //KMPH DIBAWAH 50
      if (kmph <= 50)
      {
        kondisi = 1;
      }

      //KMPH DIANTARA 50 - 80
      if (kmph >= 50 && kmph <= 80)
      {
        kondisi = 2;
      }

      //KMPh DIATAS 80
      if (kmph >= 80)
      {
        kondisi = 3;
      }
      mySerial.write(kondisi);

      Serial.print("RPM =  ");
      Serial.print(rpm);
      Serial.print("\t");
      Serial.print(" | SPEED =  ");
      Serial.print(kmph);
      Serial.print("\t");
      Serial.print("KM/h");
      Serial.print("\t");
      Serial.print("KONDISI = ");
      Serial.println(kondisi);
    }

    laststate = sensorstate;
    attachInterrupt(0, rpm_fun, FALLING);
  }
}

but, im kinda confused with formula:
rpm = 30 * 1000 / (millis() - timeold) * rpmcount;

can someone explain the formula, im confused about it. Thanks

(millis() - timeold) / (60 * 1000) gives you the time difference in minutes. Then you calculate the number of pulses per minute (rpmcount / time difference). Finally you divide it by two, for some reason, maybe because there are two falling edges per rotation.

Pieter

PieterP:
(millis() - timeold) / (60 * 1000) gives you the time difference in minutes. Then you calculate the number of pulses per minute (rpmcount / time difference). Finally you divide it by two, for some reason, maybe because there are two falling edges per rotation.

Pieter

so, 601000 is the time (1 minute)
if i use 2 falling per rotation, i use 30
1000
if i use 5 falling per rotation, i use 12*1000?

is that right?

I much prefer to measure rotational speed by measuring the time (in micros() ) for each revolution. Have a look at this example snippet

...R

You might also want to check this:

mySerial.begin(9900);

Selamat success.

so, 60*1000 is the time (1 minute)

Yes

if i use 2 falling per rotation, i use 30*1000

No, 60*1000 is still the time in ms for 1 minute.

"60*1000" is very dangerous, since it overflows the maximum value of an integer on Arduino (16bits.)
You should use "60*1000UL" so the calculation is done with 32bit integers instead.