Help understanding code

Dear All

I start working with equipment that uses arduino to measure speed in km/h

The car tire has a 18 teeth wheel and proximity sensor that count pulses.

This is the code

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
volatile byte rpmcount;
 double freq;
 double timeold;
 double velocidade_k;

void setup()
    pinMode(2, INPUT);
  attachInterrupt(0, velo, RISING); 'falling change';
  lcd.begin(8, 2); 
  lcd.setCursor(0, 0);
  lcd.setCursor(0, 1);
     rpmcount = 0;
     freq = 0;
     timeold = 0;}

 void loop()
 if (rpmcount >= 10 )
 {   freq = 1000/((millis()-timeold)/rpmcount);
     timeold = millis();
     rpmcount = 0;
    velocidade_k = (freq * 0.407); 
    lcd.setCursor(3, 1);
    lcd.print(velocidade_k, 1);
    Serial.println(velocidade_k, 2);
void velo()

My question is this formula

velocidade_k = (freq * 0.407)

The 0.407 should be related to the wheel diameter or Perimeter?????
The car has aprox 1.96m perimeter wheel.

Could the formula be Velocidade(speed)=freq* Perimeter/18pulses * 3.6(m/s to km/h)???

There is a relation between frequency and perimeter? I have some doubts.
The code was not made by me.

I appreciate some help


Does that even compile?

Yes, it runs in an arduino uno with a lcd

Could the formula be Velocidade(speed)=freq* Perimeter/18pulses * 3.6(m/s to km/h)???

If frequency is counts per second and there are 18 counts per revolution then you are correct. freq*Perimeter/18 is linear development in meters per second.

In the coding, Perimeter should be declared as a float . I would bracket the computation this way way and make the constant 18.0 for the floating point calculations.

float perimeter = 1.96; //meters
((freq/18.0)*perimeter) *3.6 = km/hr

yes on AVR (just a warning about a statement that has no effect likely) whereas it will fail for an ESP (too many characters for the underlying char type).

comments at the end of a line starts with a // in C++ (or use /* .... */)

Velo = 7.056 / (millis()-timeold);

1.96 / 18 * 3.6 * 1000 = 7.056

Many thaks cattledog

Hi Kolaha

Can you explain your formula? 1000 ms?

we calculate speed in m/s but millis are in ms, so we need m/ms, or km/s

I need km/h

1.96m is wheel by complete rotation.
messured time is shorter by 18 times.
millis() are 1000 more as 1 second.
3.6 multiplyer for m/s to km/h conversion.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.