Wind Speed calculator without Blocking Delay

Hello Blynk community, I want to integrate an Anemometer/Wind Speed Sensor into my Blynk project, but I have the Following Problem.

My Anemometer uses this Code to work:


const int RecordTime = 3; //Define Measuring Time (Seconds)
const int SensorPin = 3;  //Define Interrupt Pin (2 or 3 @ Arduino Uno)

int InterruptCounter;
float WindSpeed;

void meassure() {
  InterruptCounter = 0;
  attachInterrupt(digitalPinToInterrupt(SensorPin), countup, RISING);
  delay(1000 * RecordTime);
  detachInterrupt(digitalPinToInterrupt(SensorPin));
  WindSpeed = (float)InterruptCounter / (float)RecordTime * 2.4;
}

void countup() {
  InterruptCounter++;
}

But i obviously can’t use the Delay function in Blynk, so I tried solving it this way:


const int RecordTime = 3; //Define Measuring Time (Seconds)
const int SensorPin = 3;  //Define Interrupt Pin (2 or 3 @ Arduino Uno)

int InterruptCounter;
float WindSpeed;

void Wind() {
  InterruptCounter = 0;
  attachInterrupt(digitalPinToInterrupt(Pwind), countup, RISING);
  timer.setTimeout((1000 * RecordTime), Wind2);
}

void Wind2() {
  detachInterrupt(digitalPinToInterrupt(Pwind));
  Wspeed = (float)InterruptCounter / (float)RecordTime * 2.4;
}

void countup() {
  InterruptCounter++;
}

But as I found out that doesn’t work, because the attachInterrupt apparently needs to be interrupted for a set time and I haven’t been able to figure out how I can do that with the Blynk Timer.

Your help would be greatly apprechiated.

It might be worth you reading this…

Pete.

Thank you Pete, i actually figured it out and it works (i think), because i cant really test if its accurate, but i wrote this code:


void Wind() {
  while (blocked == false) {
  InterruptCounter = 0;
  attachInterrupt(digitalPinToInterrupt(Pwind), countup, RISING);
  timer.setTimeout((1000 * RecordTime), Wind2);
  blocked = true;
  }
}

void Wind2() {
  
  detachInterrupt(digitalPinToInterrupt(Pwind));
  Wspeed = (float)InterruptCounter / (float)RecordTime * 2.4;
  blocked = false;

}

void countup() {
  InterruptCounter++;
}

Can you tell if something is very wrong or might cause inaccurate results? Or is my Code ok?

I don’t really understand why you’re constantly attaching and detaching the interrupts.
Also, it’s never a good idea to use while with Blynk in my experience.

Pete.