Timer Conversion?

Hi Folks,

I’m looking for advice/guidance on converting/replacing the timer in the code below with the appropriate timer for Xiao SAMD21. The elapsedMillis is specific to the MCU I’ve been using. I’d like to bring this code to the Xiao if possible. ?

const int thresholdMin = 12;  // minimum reading, avoid "noise"
const int aftershockMillis = 60; // time of aftershocks & vibration

int state=0; // 0=idle, 1=looking for peak, 2=ignore aftershocks
int peak;    // remember the highest reading
elapsedMillis msec; // timer to end states 1 and 2

void setup() {
  Serial.begin(115200);
  //pinMode(A0, INPUT_DISABLE);
  while (!Serial && millis() < 2500) /* wait for serial monitor */ ;
  Serial.println("Piezo Peak Capture");
}

void loop() {
  int piezo = analogRead(A10);

  if (state == 0) {
    // IDLE state: if any reading is above a threshold, begin peak
    if (piezo > thresholdMin) {
      //Serial.println("begin state 1");
      state = 1;
      peak = piezo;
      msec = 0;
    }
  } else if (state == 1) {
    // Peak Tracking state: for 10 ms, capture largest reading
    if (piezo > peak) {
      peak = piezo;
    }
    if (msec >= 10) {
      Serial.print("peak = ");
      Serial.println(peak);
      //Serial.println("begin state 2");
      state = 2;
      msec = 0;
    }
  } else {
    // Ignore Aftershock state: wait for things to be quiet again
    if (piezo > thresholdMin) {
      msec = 0; // keep resetting timer if above threshold
    } else if (msec > 30) {
      //Serial.println("begin state 0");
      state = 0; // go back to idle after 30 ms below threshold
    }
  }
  
}

Hi there,
So why not just use the millis, according to Gman…

How to get elapsed time in Arduino?
You can use the millis() function of Arduino to measure the time. This function returns the number of milliseconds passed since your board started running the current program.

recode to use that function, ?
HTH
GL :slight_smile: PJ
:v:

Here is a thread that discusses it also

Hi PJ,

Thanks so much for responding. The millis() was the first option I looked into when I started. ( been researching for a few days). But it appeared that the millis() function was not at all possible on the SAMD21 ? And, that some other “type” of timer is needed to accomplish the “scanning for peak” I will certainly have at the link you provided.

Trying my best to use the errors as clues to get to a solution. I’m very new to this as you can probably guess. :slight_smile:

Hi there,
Ok so I see this :+1: the SleepyDog library works with the SAMD21: GitHub - adafruit/Adafruit_SleepyDog: Arduino library to use the watchdog timer for system reset and low power sleep.
I gleaned from this tread a while back looking over millis function, several other libs are out there to and it seems the op in this thread was able to use this other Timer lib with some success
https://forums.adafruit.com/viewtopic.php?t=206920
" I ended up going with the Timer.h library function, simpler to understand and dead precise without the mental gymnastics of millis()."
HTH
GL :slight_smile: PJ
:v:

the timers always throw folks into a spin… me too! :face_with_peeking_eye:

Thank you very much PJ. I really appreciate this !

1 Like