Advice for timing the duration of an event Arduino

Hi,

First time posting.

Fairly new to Arduino and I am after some advice on the best way to time the duration of an event using Arduino.
My idea is to use a Davis anemometer to measure wind speed at a given time then to have a timer start when the wind speed increases above 20KM/h then stop when it falls below 20KM/H.
I am wanting for the timer to start and stop each time the conditions change to give me a total amount of time the condition was met over a 24hr period.
My intent is to gain data to estimate the effectiveness of a wind generator based on the time the wind speed is high enough for it to work

I have the anemometer part coded and working to give me a reliable wind speed output as well as the data logger but I am not sure how to code the timer part.

I have some basic understanding using an RTC and the Millis function however I cant figure out how to make do it what I want.

I figure it should be simple and I am missing something obvious.

Thanks in advance for any advice

Paulreic:
My intent is to gain data to estimate the effectiveness of a wind generator based on the time the wind speed is high enough for it to work

For that purpose I suspect it would be sufficiently accurate to use millis() to keep track of the duration and just use the RTC to identify midnight so you can calculate the duration for a day

To use millis() you need to save the value when the speed changes from below 20 to above 20 and again when it falls from above 20 to below 20. And after a drop in speed you calculate the duration for that period and add it to the total. Something like this

if (prevWindSpeed <= 19 and currentWindSpeed >= 20) {
   startMillis = millis();
}
if (prevWindSpeed >= 20 and currentWindSpeed <= 19) {
   endMillis = millis();
   durationThisPeriod = endMillis - startMillis;
   totalDuration += durationThisPeriod;
}

If you want to use the RTC for all the timing the approach will be same

…R

This might give you something to work from.

// globals
unsigned long totalTime = 0;
unsigned long timeMark = 0;

void beginTiming() {
  timeMark = millis()
}

void endTiming() {
  if (timeMark) {
    totalTime += millis() - timeMark;
    timeMark = 0;
  }
}

Thanks for your replies

I had come up with similar to the one Robin2 has suggested however I only had one condition in my If statement and it didn’t work. Makes perfect sense and is indeed obvious

I will give this a go

FireTimer.h is a super easy to use lib that does this timing for you. You can install it via the Arduino IDE's Libraries Manager and comes with the following example:

#include "FireTimer.h"

FireTimer msTimer;
FireTimer usTimer;

void setup()
{
  Serial.begin(115200);

  msTimer.begin(1000);
  usTimer.begin(1000000, MICRO_SECONDS);
}

void loop()
{
  if(msTimer.fire())
    Serial.println("ms");

  if(usTimer.fire())
    Serial.println("us");
}