Control LED with period

This commit is contained in:
Simon Couball
2020-08-11 23:51:44 +02:00
parent 72c1c5c9d7
commit 28247cf74a

View File

@@ -73,28 +73,37 @@ void loop()
uint32_t period = 0; uint32_t period = 0;
uint16_t rotations = 0; uint16_t rotations = 0;
uint16_t led_period = (uint16_t)-1;
uint16_t led_counter = led_period;
while (1) { while (1) {
iterator++; iterator++;
uint8_t tacho_current = digitalRead(7); uint8_t tacho_current = digitalRead(7);
/* Get number of loop iterations per fan revolution */
if (!tacho_last && tacho_current) { if (!tacho_last && tacho_current) {
if (tacho_counter == 0) { if (tacho_counter == 0) {
tacho_counter = 1; tacho_counter = 1;
digitalWrite(2, LOW);
} else { } else {
tacho_counter = 0; tacho_counter = 0;
digitalWrite(2, HIGH);
period = iterator; period = iterator;
led_period = period / 3;
iterator = 0; iterator = 0;
rotations++; 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 { } else {
digitalWrite(2, LOW); digitalWrite(2, LOW);
} }
tacho_last = tacho_current;
delayMicroseconds(100);
} }
} }