From 28247cf74ab63c681dfc1491ebe2e993ace48b8a Mon Sep 17 00:00:00 2001 From: Simon Couball Date: Tue, 11 Aug 2020 23:51:44 +0200 Subject: [PATCH] Control LED with period --- src/main.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 199b335..95d4c8e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -73,28 +73,37 @@ void loop() uint32_t period = 0; uint16_t rotations = 0; + uint16_t led_period = (uint16_t)-1; + uint16_t led_counter = led_period; + while (1) { iterator++; uint8_t tacho_current = digitalRead(7); + /* Get number of loop iterations per fan revolution */ if (!tacho_last && tacho_current) { if (tacho_counter == 0) { tacho_counter = 1; - digitalWrite(2, LOW); } else { tacho_counter = 0; - digitalWrite(2, HIGH); period = iterator; + led_period = period / 3; iterator = 0; rotations++; } + } + tacho_last = tacho_current; + + /* Emit strobe for LED */ + led_counter--; + if (led_counter < 12) { + digitalWrite(2, HIGH); + if (!led_counter) { + led_counter = led_period; + } } else { digitalWrite(2, LOW); } - - tacho_last = tacho_current; - - delayMicroseconds(100); } }