I am trying to make a speedometer cum odometer. I'm using a PIC based MCU. The speed I'm getting is as an analog input. (at 0 V, I get 0 km/h and at 18 V, I get 25 km/h). I can easily measure the speed using the ADC conversion. The issue I'm facing is how do I measure the distance here. Does anyone have any ideas for the same?
One thing I can think of is having a delay of 100 ms in the code and multiplying the instantaneous speed by 100 ms. Since all other function execution should take a maximum of 1-5 ms (including ADC conversion and sending bits to LCD displays etc), I can neglect that time and consider the distance here. But this method is not very exact. Does anyone have a better solution?
PFA the basic code
currentSpeedVoltage = ADC_Read(9);
currentSpeedVoltage = (currentSpeedVoltage * 0.001220703125);
//(0.001220703125 = 5/4096) (12 bit resolution for ADC).
currentSpeed = (currentSpeedVoltage*25)/18;
// assumed here that at 18 Volts, we show the speed as 25 km/hr.
distance += (currentSpeedx100x5*(pow(10,-5))/18)/1000;