From 1f1b379cc2339767f235c00a26c346fdf8be4f95 Mon Sep 17 00:00:00 2001 From: "Tanner Collin (aider)" Date: Thu, 5 Jun 2025 11:16:49 -0600 Subject: [PATCH] refactor: Improve RPM calc using actual elapsed time --- tach/tach.ino | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tach/tach.ino b/tach/tach.ino index ffffc55..e120caa 100644 --- a/tach/tach.ino +++ b/tach/tach.ino @@ -64,15 +64,25 @@ void loop() { // Store the current total snapshot for the next interval's calculation g_main_last_pulse_count_snapshot = current_total_snapshot; + unsigned long actual_elapsed_time = current_time - last_measurement_time; + // Calculate RPM - // RPM = (pulses / pulses_per_revolution) / (measurement_period_ms / 1000 / 60) - // RPM = (pulses / pulses_per_revolution) * (60000 / measurement_period_ms) + // RPM = (pulses / pulses_per_revolution) / (actual_elapsed_time_ms / 1000 / 60) + // RPM = (pulses / pulses_per_revolution) * (60000 / actual_elapsed_time_ms) // Ensure floating point division for accuracy before converting to int for display - float rpm_float = ( (float)collected_pulses / PULSES_PER_REVOLUTION ) * (60000.0 / MEASUREMENT_PERIOD_MS); + float rpm_float = 0.0; + if (actual_elapsed_time > 0) { // Avoid division by zero + rpm_float = ( (float)collected_pulses / PULSES_PER_REVOLUTION ) * (60000.0 / actual_elapsed_time); + } int rpm = (int)rpm_float; show(rpm); + // Debugging output + Serial.print("Pulses: "); Serial.print(collected_pulses); + Serial.print("\tElapsed ms: "); Serial.print(actual_elapsed_time); + Serial.print("\tRPM: "); Serial.println(rpm); + last_measurement_time = current_time; } }