Hi, hoping for some guidance on a non-blocking method for calculating fan RPM. I have the below running using millis using Blynk Timers, how-ever this essentially is an alternative method to delay()? which I’m guessing can cause the same issue with blynk server connections?
Code compiles/runs and I’m seeing correct FAN RPMs being reported in serial monitor every 5 seconds.
The issue is I need exactly 1 second delay between the below two lines for it to calculate th number of reotations. Anyone have an example of how to do this which is more friendly to blynk, or is Millis fine to use in this case?
attachInterrupt(digitalPinToInterrupt(fanrpmsensor), countup, RISING);
delay here for 1 second is needed.
detachInterrupt(digitalPinToInterrupt(fanrpmsensor));
thanks heaps!
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "REMOVED"
#define BLYNK_DEVICE_NAME "REMOVED"
char auth[] = "REMOVED";
char ssid[] = "REMOVED";
char pass[] = "REMOVED";
#include <Wire.h>
#include <SPI.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer1;
//FAN RPM Code
const int fanrpmsensor = 14;
int InterruptCounter, rpm;
int period = 1000;
unsigned long time_now = 0;
int fanrpm = 0;
void setup()
{
Serial.begin(9600);
pinMode(fanrpmsensor, INPUT);
timer1.setInterval(5000L, fanrpmmeasure);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
timer1.run();
time_now = millis();
}
void fanrpmmeasure() {
InterruptCounter = 0;
attachInterrupt(digitalPinToInterrupt(fanrpmsensor), countup, RISING);
while (millis() < time_now + period) {
//wait approx. [period] ms
}
detachInterrupt(digitalPinToInterrupt(fanrpmsensor));
rpm = (InterruptCounter / 2) * 60;
display_rpm();
}
void countup() {
InterruptCounter++;
}
void display_rpm() {
Serial.print(" RPM: ");
Serial.println(rpm);
if (rpm >= 6000) {
Serial.println("I am more than 6000 RPM");
}
}