refactor: Simplify interrupt handling
This commit is contained in:
+2
-8
@@ -50,12 +50,10 @@ void loop() {
|
|||||||
unsigned long current_time = millis();
|
unsigned long current_time = millis();
|
||||||
|
|
||||||
if (current_time - last_measurement_time >= MEASUREMENT_PERIOD_MS) {
|
if (current_time - last_measurement_time >= MEASUREMENT_PERIOD_MS) {
|
||||||
detachInterrupt(digitalPinToInterrupt(TACH_INTERRUPT_PIN));
|
//detachInterrupt(digitalPinToInterrupt(TACH_INTERRUPT_PIN));
|
||||||
unsigned long collected_pulses = pulse_count;
|
unsigned long collected_pulses = pulse_count;
|
||||||
pulse_count = 0;
|
pulse_count = 0;
|
||||||
// It's important to re-attach the interrupt as soon as possible
|
//attachInterrupt(digitalPinToInterrupt(TACH_INTERRUPT_PIN), count_pulse, RISING);
|
||||||
// to minimize missed pulses.
|
|
||||||
attachInterrupt(digitalPinToInterrupt(TACH_INTERRUPT_PIN), count_pulse, RISING);
|
|
||||||
|
|
||||||
// Calculate RPM
|
// Calculate RPM
|
||||||
// RPM = (pulses / pulses_per_revolution) / (measurement_period_ms / 1000 / 60)
|
// RPM = (pulses / pulses_per_revolution) / (measurement_period_ms / 1000 / 60)
|
||||||
@@ -65,10 +63,6 @@ void loop() {
|
|||||||
int rpm = (int)rpm_float;
|
int rpm = (int)rpm_float;
|
||||||
|
|
||||||
show(rpm);
|
show(rpm);
|
||||||
Serial.print("Pulses: ");
|
|
||||||
Serial.print(collected_pulses);
|
|
||||||
Serial.print(", RPM: ");
|
|
||||||
Serial.println(rpm);
|
|
||||||
|
|
||||||
last_measurement_time = current_time;
|
last_measurement_time = current_time;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user