# LED speedometer at rest

hi!
I ve been building a LED bar speedometer on a model car, calculating the velocity with an infrared RPM sensor. It works, except for one thing: once the car brakes and comes to a stop, the velocity wont become 0.
This is because we used the attachinterrupt function and it wont update the velocity until the RPM sensor gets HIGH again. Does anyone of you have any idea as to how we can force a function to update the velocity anyway?
Here is some of the code, the attachinterrupt:

``````attachInterrupt(5, interruption, FALLING);
``````

the function it calls:

``````void interruption(){Â  Â
Â
Â   interval = (millis()-time_last);
Â   time_last = millis();

Â
}
``````

interval is then used to update the velocity:

``````void velocityCalc(){

Â  rpsec = (1000/interval)/2;
Â  Â  velocity = (0.376991*rpsec*60*3.6)/60;

}
``````
``````if (millis() - time_last > something)
{
velocity = 0;
}
``````

I have already tried that, i have put it in the interrupt function. But that still doesnt work. It only goes back to zero after you start to drive again, so during the standstill, it still displays a couple of of km/h,
and once you provide throttle again, it quickly goes back to zero and then displays the new speed.

i have put it in the interrupt function

Why when you know that the ISR will not be triggered ?
Put it or something similar in the loop() function and set time_last to millis() as part of the ISR or outside the ISR by setting a flag variable inside the ISR.

The problem is that you're only triggering an update when you get a pulse from the speed sensor.

instead of measuring how long it's been since the last pulse, what you really need to do is measure how many pulses in a given time period.

``````const unsigned long UpdateInterval = 500; // update speed twice per second.
const unsigned long SpeedConstant = (whatever it needs to be for pulsecounts/interval);
unsigned long LastUpdate = millis(); // When was the Speed last updated?
unsigned long PulseCount = 0l;
unsigned int CurrentSpeed = 0;
...

void Interruption() {
Â  PulseCount++;
}

void CalculateSpeed() {
CurrentSpeed = PulseCount / (millis() - LastUpdate) * SpeedConstant;
}

...

void main() {
if (millis() - LastUpdate > UpdateInterval) {
Â  CalculateSpeed();
Â  DisplaySpeed(); // whatever code you want to use to display the result of the previous calculation.
Â  PulseCount=0;
Â  LastUpdate=millis();
}
// whatever other code you want to keep running
}
``````

Something like that will update the speed at consistent intervals which will avoid the display flashing between 2 similar values many times per second. In this case, it's every 1/2 second, but you could use longer values for more meaningful results to a human being.