# Starting new timer when first timer end

**URL:** <https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336>\
**Category:** Programming\
**Created:** [January 20, 2022, 4:29pm UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336 "2022-01-20T16:29:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![handrup](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@handrup](https://forum.arduino.cc/u/handrup)\
**Post date:** [January 20, 2022, 4:29pm UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/1 "2022-01-20T16:29:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [January 20, 2022, 4:32pm UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/2 "2022-01-20T16:32:20Z")

</div>

> [@handrup](#):
>
> Hey pretty people

Wow. I must be in the wrong place.

> [@handrup](#):
>
> I hope my question makes sense

I see no question.

I see no code either.

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [January 20, 2022, 4:38pm UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/3 "2022-01-20T16:38:59Z")

</div>

> [@anon73444976](#):
>
> > [@handrup](#):
> >
> > Hey pretty people
> 
> Wow. I must be in the wrong place.

I think OP was talking to me.

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [January 20, 2022, 4:39pm UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/4 "2022-01-20T16:39:57Z")

</div>

> [@handrup](#):
>
> 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

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

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [January 20, 2022, 4:58pm UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/5 "2022-01-20T16:58:50Z")

</div>

> [@handrup](#):
>
> 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.

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

```arduino
#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.

---

<div class="post-metadata">

**Author:** ![handrup](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@handrup](https://forum.arduino.cc/u/handrup)\
**Post date:** [January 20, 2022, 7:04pm UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/6 "2022-01-20T19:04:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![handrup](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@handrup](https://forum.arduino.cc/u/handrup)\
**Post date:** [January 20, 2022, 7:05pm UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/7 "2022-01-20T19:05:52Z")

</div>

```cpp

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

---

<div class="post-metadata">

**Author:** ![handrup](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@handrup](https://forum.arduino.cc/u/handrup)\
**Post date:** [January 20, 2022, 7:08pm UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/8 "2022-01-20T19:08:09Z")

</div>

```cpp

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

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [January 20, 2022, 7:20pm UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/9 "2022-01-20T19:20:51Z")

</div>

> [@handrup](#):
>
> I cant seem to find this library or "minitimer" function you are using

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

> [@](#):
>
> I need it to able to open say every 7 days and stay open for maybe 1-hour max

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.

```arduino

//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();
}

```

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 21, 2022, 6:58am UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/11 "2022-01-21T06:58:44Z")

</div>

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

```cpp
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

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [January 21, 2022, 9:07am UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/12 "2022-01-21T09:07:07Z")

</div>

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

```cpp
/* 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++.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 20, 2022, 9:07am UTC](https://forum.arduino.cc/t/starting-new-timer-when-first-timer-end/949336/13 "2022-07-20T09:07:27Z")

</div>

Closed on July 20, 2022, 9:07am UTC

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