Starting new timer when first timer end

Hey pretty people
I have created a timer using the millis function and now i want to start a new timer right when the first one is completed.

This will be used for holding a "door" open for a certain amount of time

I hope my question makes sense

Wow. I must be in the wrong place.

I see no question.

I see no code either.

I think OP was talking to me.

Please include your sketch. Please describe what you want your program to do.

well, I would just start the next timer when the first one has triggered.

#include <Noiasca_timer.h>

// here you create your timer object
MiniTimer miniTimerA {2000};
MiniTimer miniTimerB {1000};

void setup() {
  Serial.begin(115200);
  Serial.println(F("\nStart"));
  miniTimerA.start();
}

void loop() {
  if (miniTimerA.hasTriggered())  // if the defined time is over, the timer will execude the followin lines
  { 
    Serial.println(F("timerA has triggered"));
    miniTimerA.stop();
    miniTimerB.start(); 
  }
  if (miniTimerB.hasTriggered())  // if the defined time is over, the timer will execude the followin lines
  { 
    Serial.println(F("timerB has triggered"));
    miniTimerB.stop();
    miniTimerA.start(); 
  }
}

I hope my answer makes sense for you.

I cant seem to find this library or "minitimer" function you are using


const unsigned long vandInterval = 5000;
const unsigned long vand2Interval = 3000;
unsigned long foerTid1 = 0;
unsigned long foerTid2 = 0;


void setup() {

  Serial.begin(9600);
  Serial.println("Program start");

}

void loop() {
  unsigned long nuTid = millis();
  // nuTid opdateres konstant med værdien fra millis funktion
  // foerTid ændres kun når if statementet kører
  if ( nuTid - foerTid1 >= vandInterval) {

    Serial.println("åben for vand");
    /*Åbner for vandet ved hjælp af servo*/
    
    // opdaterer værdien af nuTid
    foerTid1 = nuTid;
    
    
  }

}

I wish to use these timers to control a servo that i will be using to open/close a tiny metal door in an upcoming project

I need it to able to open say every 7 days and stay open for maybe 1 hour max



const unsigned long vandInterval = 5000;
const unsigned long vand2Interval = 3000;
unsigned long foerTid1 = 0;
unsigned long foerTid2 = 0;


void setup() {

  Serial.begin(9600);
  Serial.println("Program start");

}

void loop() {
  unsigned long nuTid = millis();
  // nuTid opdateres konstant med værdien fra millis funktion
  // foerTid ændres kun når if statementet kører
  if ( nuTid - foerTid1 >= vandInterval) {

    Serial.println("åben for vand");
    /*Åbner for vandet ved hjælp af servo*/
    
    // opdaterer værdien af nuTid
    foerTid1 = nuTid;
    
    
  }

}

I wish to use these timers to control a servo that i will be using to open/close a tiny metal door in an upcoming project

I need it to able to open say every 7 days and stay open for maybe 1-hour max

So how do a make that when the door opens, it stays open for a desired amount of time

Now you understand how important it is to see full code. Otherwise we can't help.

this reads like you have exact two states:
a 7 day periode
a 1 hour periode

is this really all you need?
because if this would be true, you only need a "Blink Without Delay" - with different on and off times.


//constexpr uint32_t onInterval = 60*60UL; // 1 h
//constexpr uint32_t offInterval= (7 * 24 * 60 * 60UL) - onInterval; // nearly a week

// for testing
constexpr uint32_t onInterval = 1; // short period in seconds
constexpr uint32_t offInterval= 5; // long period in seconds

void open()
{
  Serial.println(F("open"));
  //do something with the servo
}

void close()
{
  Serial.println(F("close"));
  // do something with the servo
}

void doFSM()
{
  static uint32_t previousMillis = 0;
  static byte state = 0;
  uint32_t currentMillis = millis();
  if (state == 0 && currentMillis - previousMillis > offInterval*1000)
  {
    previousMillis = currentMillis;
    open();     // do something
    state = 1;  // go to next state
  }
  else if (state == 1 && currentMillis - previousMillis > onInterval*1000)
  {
    previousMillis = currentMillis;
    close();   // do something
    state = 0; // go to next state
  }
  
}

void setup() {
  Serial.begin(115200);
  Serial.println(F("\nStart"));
}

void loop() {
  doFSM();
}

to state a few framing conditions about timing.

Whenever you use the function millis() the variables for the timing must be of type unsigned long.

The if-condition to check the time must be coded

if (currentTime - snapShotOfStart >= timePeriod) {
  // time for timed action
}

If the if-condition is coded this way the rollover from max to zero is calculated that even on rollover the timing works as expected

best regards Stefan

Hello
I have found a similar sketch in my box for used sketches:

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336
*/
#define ProjectName "Starting new timer when first timer end"
// VARIABLE DECLARATION AND DEFINITION
enum {Timer_A, Timer_B};
enum {Off, On};
struct TIMER {              // has the following members
  String name_;             // timer name
  int   next;               // timer fired next
  unsigned long duration;   // memory for interval time
  unsigned long stamp;      // memory for actual time
  bool onOff;               // control for start/stop
};
TIMER timers[] {
  {"Timer A", Timer_B, 1000, 0, On},
  {"Timer B", Timer_A, 2000, 0, Off},
};
// -------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
}
void loop () {
  unsigned long currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  for (auto &timer : timers) {
    if (currentTime - timer.stamp >= timer.duration && timer.onOff) {
      Serial.print(timer.name_), Serial.println(F(" fired"));
      timer.onOff = Off;
      timers[timer.next].stamp = currentTime;
      timers[timer.next].onOff = On;
    }
  }
}

Try it.
Have a nice day and enjoy coding in C++.