Blynk Non Blocking for FAN RPM

Delay(x) effectively causes all code execution to halt for x milliseconds. This creates problems with Blynk, as the Blynk library needs to be talking to the Blynk server all the time.
Delay() would also cause you problems with your code, as it would stop the interrupts and the counter increments as well.

BlynkTimer and SimpleTimer are non-blocking, so don’t cause these problems. In effect, they both use a millis comparison process. Every time timer.run (or timer1.run in your case) is executed, it causes the Blynk/SimpleTimer library to check if any of the scheduled timer tasks need to be executed yet.

In your case, you’ve constructed a combination of timer useage and millis comparison, but you’ve done it incorrectly as far as I can see.

There are two ways to tackle this in my opinion, one uses the timer/millis combo, the other uses a timeout timer in place of the millis comparison.

Option 1 - timer plus millis…

Timer calls a function (every 5 seconds in your case)
Function captures the current millis reading, zeros the interrupt counter and enables interrupts
Interrupts occur and are counted
In your void loop you do a millis comparison - has 1 second elapsed since the millis value that was captured above?
If yes then you call a second function which disables interrupts, calculates the fan speed, prints the results, sends the results to Blynk etc.

Option 2 - Timer plus Timeout timer

Basically the same as above, except that instead of doing the millis comparison, a timeout timer is used instead…

Timer calls a function (every 5 seconds in your case)
Function initiates a timeout timer of 1000ms, zeros the interrupt counter and enables interrupts
The timeout timer will call a ‘calculate_results’ function when it times-out
Interrupts occur and are counted
When the timeout timer times-out then the ‘calculate results’ function is called which disables interrupts, calculates the fan speed, prints the results, sends the results to Blynk etc.

Option 2 would be my preferred method, as it is cleaner because only one set of millis comparisons is being done (inside the BlynkTimer library) rather than two.

More info on timeout timers towards the end of this tutorial…

Pete.