Delay after a fixed time

I'm using NodeMcu along with pulse sensor to monitor the values.
Currently I'm using this simple code

Serialprint(analogRead(A0));
delay(5000);

This code monitors and provides the values after giving a delay of 5 seconds after each value.

I want the modify the code in such a way that the NodeMcu will continuously monitor for 10 mins without any delay. After 10 mins, a delay of 5 seconds will occur. Again the NodeMcu will monitor for 10 mins and this way it will continue. I can't find a way to do that. Is there any way to do this ??

Hello
Well, I think you could desigend a time manager using the IDE example "BlinkWithOutDelay".

How to write Timers and Delays in Arduino covers replacing delay with a millis based timer.

//https://forum.arduino.cc/t/delay-after-a-fixed-time/884660

// download SafeString V4.1.5+ library from the Arduino Library manager or from
// https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
// for the millisDelay class
#include "millisDelay.h"

millisDelay tenMinTimer;
const unsigned long TEN_MIN_MS = 10ul * 60 * 1000; // not the ul
millisDelay fiveSecDelay;
const unsigned long FIVE_SEC_MS = 5ul * 1000;

void setup() {
  Serial.begin(115200); //for debug
  for (int i = 10; i > 0; i--) {
    delay(500);
    Serial.print(i); Serial.print(' ');
  }
  Serial.println("started");
  tenMinTimer.start(TEN_MIN_MS);
}

void loop() {
  if (tenMinTimer.isRunning()) { // only while timer is running
    Serial.println(analogRead(A0));
  }
  if (tenMinTimer.justFinished()) { // after 10mins
    fiveSecDelay.start(FIVE_SEC_MS);
  }
  if (fiveSecDelay.justFinished()) {
    tenMinTimer.restart(); // start reading again
  }
}

Okay sir

Thank your sir, I'll try it

how about

    Serialprint(analogRead(A0));
    if (millis() > (10 * 60 * 1000);
        delay(5000);
void setup()
{
  Serial.begin(115200);
}

void loop()
{
  static unsigned long startTimer = 0;
  Serial.print(analogRead(A0));
  // Add a 5-second delay every 10 minutes:
  if (millis() - startTimer > 10ul * 60ul * 1000ul)
  {
    startTimer = millis();
    delay(5000);
  }
}

Okay sir

Thanks sir, it helped me !

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.