From 85a312d7e10014dfed227ec7f4847c8c93969ce6 Mon Sep 17 00:00:00 2001 From: "jamesimmanuel.magsino@stud.h-da.de" <James Immanuel Magsino> Date: Tue, 3 Dec 2024 15:42:25 +0100 Subject: [PATCH] averaged the last three results --- src/main.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index b97ce69..ccca91d 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,10 @@ #include <unistd.h> #include <hardware/pwm.h> +double percentage_1 = 0; +double percentage_2 = 0; +bool flipflop = false; + const uint8_t init[4] = {0x21, 0xa0, 0xef, 0x81}; const uint8_t zeile[8] = {0x00, 0x02, 0x04, 0x06, 0x08, 0x10, 0x12, 0x14}; const uint8_t fill[17] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; @@ -144,7 +148,18 @@ void callback(uint gpio, uint32_t events) volatile int percentage = (distance / 10) * 100; - set_progress(percentage); + if (flipflop) + { + percentage_1 = percentage; + flipflop = false; + } + else + { + percentage_2 = percentage; + flipflop = true; + } + + set_progress((percentage + percentage_1 + percentage_2) / 3); } else if (events & GPIO_IRQ_EDGE_RISE) -- GitLab