From 9b7e119259ef80e23ea6ce3e9e1bc4a2abab5e50 Mon Sep 17 00:00:00 2001 From: JanHBade Date: Sun, 3 Nov 2024 13:04:15 +0100 Subject: [PATCH 1/2] Reduce measure errors --- src/HCSR04.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/HCSR04.cpp b/src/HCSR04.cpp index afe483c..8e43fc8 100644 --- a/src/HCSR04.cpp +++ b/src/HCSR04.cpp @@ -40,7 +40,9 @@ float UltraSonicDistanceSensor::measureDistanceCm(float temperature) { } // Measure the length of echo signal, which is equal to the time needed for sound to go there and back. + noInterrupts(); unsigned long durationMicroSec = pulseIn(echoPin, HIGH, maxDistanceDurationMicroSec); // can't measure beyond max distance + interrupts(); float distanceCm = durationMicroSec / 2.0 * speedOfSoundInCmPerMicroSec; if (distanceCm == 0 || distanceCm > maxDistanceCm) { @@ -48,4 +50,4 @@ float UltraSonicDistanceSensor::measureDistanceCm(float temperature) { } else { return distanceCm; } -} \ No newline at end of file +} From 178ffc7f825c1b728d6e38ddc623a789c3b6e954 Mon Sep 17 00:00:00 2001 From: JanHBade Date: Thu, 5 Dec 2024 13:55:43 +0100 Subject: [PATCH 2/2] fix indent --- src/HCSR04.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HCSR04.cpp b/src/HCSR04.cpp index 8e43fc8..8782d13 100644 --- a/src/HCSR04.cpp +++ b/src/HCSR04.cpp @@ -40,9 +40,9 @@ float UltraSonicDistanceSensor::measureDistanceCm(float temperature) { } // Measure the length of echo signal, which is equal to the time needed for sound to go there and back. - noInterrupts(); + noInterrupts(); unsigned long durationMicroSec = pulseIn(echoPin, HIGH, maxDistanceDurationMicroSec); // can't measure beyond max distance - interrupts(); + interrupts(); float distanceCm = durationMicroSec / 2.0 * speedOfSoundInCmPerMicroSec; if (distanceCm == 0 || distanceCm > maxDistanceCm) {