# Timer0\_mills error

**URL:** <https://forum.arduino.cc/t/timer0-mills-error/1118362>\
**Category:** Programming\
**Created:** [April 22, 2023, 5:13pm UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362 "2023-04-22T17:13:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![say2k](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@say2k](https://forum.arduino.cc/u/say2k)\
**Post date:** [April 22, 2023, 5:13pm UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/1 "2023-04-22T17:13:13Z")

</div>

Hi,

I have a problem with my sketch and can't find the error.

I am using an Arduino Uno Wifi Rev2. Firmware etc. are all up to date.

In my sketch I make a motor keep turning after certain conditions. However, when I press a button, I want the motor to move back to position 0, but after a predefined waiting time, it moves back to the previous state.

I have tried this with the timer0\_millis function. Unfortunately, I get an error when I compliate it and I can't find the error. Can you help?

```plaintext

#include <AccelStepper.h>

unsigned long start = 0;
const long interval = 60000;
extern volatile unsigned long timer0_millis;
unsigned long milliwind = 0;

#define dirPin 2

#define stepPin 4

#define motorInterfaceType 1

#define PIN_BUTTON 7                     

AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

 

void setup() {

  stepper.setMaxSpeed(600);

  stepper.setAcceleration(400);
  pinMode(7,INPUT_PULLUP);
  Serial.begin(9600);
  delay (1000);
  
}

void loop(){
  unsigned long currentMillis = millis();

  if ((currentMillis >= interval) & (currentMillis <= interval+100))

  

  stepper.moveTo(-7000);

  stepper.runToPosition();
  currentMillis = millis();
  if ((currentMillis >= 5*60*interval) & (currentMillis <= 5*60*interval+100))
  {
    

  stepper.moveTo(-3200);
  stepper.runToPosition();
  }

currentMillis = millis();
  if ((currentMillis >= 6*60*interval) & (currentMillis <= 6*60*interval+100))
  {
    

  stepper.moveTo(0);

  stepper.runToPosition();
  
  }

currentMillis = millis();
  if ((currentMillis >= 7*60*interval)& (currentMillis <= 7*60*interval+100))
  {
    

  stepper.moveTo(3200);

  stepper.runToPosition();

  }

  currentMillis = millis();
  if ((currentMillis >= 8*60*interval)& (currentMillis < 10*60*interval))
  {
    
  stepper.moveTo(6400);

  stepper.runToPosition();

  }

   currentMillis = millis();
  if ((currentMillis >= 11*60*interval)& (currentMillis <= 11*60*interval+100))
  {
    

  stepper.moveTo(0);

  stepper.runToPosition();

  }
//////////////////////////////////////////////////////////////

  if (digitalRead(7) == 1) {  
  stepper.moveTo(0);
  stepper.runToPosition();

  milliwind = millis();

  timer0_millis = 0;
  delay (120*interval);
   //delay (14UL*60*60*1000);
   timer0_millis = milliwind+7200000;       
  } 
}

```

Edit:

I forgot the error code:

```plaintext
/var/folders/6l/xjdrdhjs54x4c1_yxd7ltlz00000gn/T//ccwUxPPH.ltrans0.ltrans.o: In function `loop':

moni/v1_280223.ino:114: undefined reference to `timer0_millis'
moni/v1_280223.ino:114: undefined reference to `timer0_millis'
moni/v1_280223.ino:114: undefined reference to `timer0_millis'
moni/v1_280223.ino:114: undefined reference to `timer0_millis'
moni/v1_280223.ino:117: undefined reference to `timer0_millis'
/var/folders/6l/xjdrdhjs54x4c1_yxd7ltlz00000gn/T//ccwUxPPH.ltrans0.ltrans.o:/moni/v1_280223.ino:117: more undefined references to `timer0_millis' follow
collect2: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 22, 2023, 5:15pm UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/2 "2023-04-22T17:15:59Z")

</div>

Your topic was **MOVED** to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [April 22, 2023, 8:55pm UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/3 "2023-04-22T20:55:29Z")

</div>

It looks like what you are doing is:

After 1 minute, move to -7000  
After 5 hours, move to -3200  
After 6 hours, move to 0  
After 7 hours, move to 3200  
After 8 hours, move to 6400  
After 11 hours, move to 0  
If the button is pressed, move to 0 and do nothing for 2 hours.

Is that what you want to do? If so, you don't need "timer0\_millis", whatever that is.

---

<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:** [April 23, 2023, 5:38am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/5 "2023-04-23T05:38:56Z")

</div>

> [@johnwasser](#):
>
> If so, you don't need "timer0\_millis", whatever that is.

I guess that is a approach to reset the timer used by the millis() function.  
That is not needed.

---

<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:** [April 23, 2023, 7:09am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/6 "2023-04-23T07:09:43Z")

</div>

Hi @say2k

indeed you should confirm or correct what @johnwasser was asking

> [@johnwasser](#):
>
> It looks like what you are doing is:
> 
> After 1 minute, move to -7000  
> After 5 hours, move to -3200  
> After 6 hours, move to 0  
> After 7 hours, move to 3200  
> After 8 hours, move to 6400  
> After 11 hours, move to 0  
> If the button is pressed, move to 0 and do nothing for 2 hours.

is this a correct description of what you want the code to do?  
confirm or correct.

If it is **not** correct post a corrected description in the style like johnwasser has written it.  
the character of how johnwasser described it is  
avoiding **all** programming terms  
just use  
time and step-positions

You are trying to reset function millis() to zero.  
This works for an arduino uno just fine.  
The

> [@say2k](#):
>
> Arduino Uno Wifi Rev2

this board uses the **ATmega4809** and a different core.  
Core here means the basic code that "translates" the C++-functions like pinMode(), digitalWrite() etc. to adress the **real** hardware of the ATmega4809.

The people who have written this core seem to have decided to not use the core-variable  
**timer0\_millis** anymore.

You should'nt mess around with core-variables anyway. If you manipulate core-variables you are in the danger of screwing up things.  
Your code does some timing things which can be quite easy done **without** resetting the function millis().

You seem to not yet fully understand how function millis() works and how to use it.

I have written a tutorial that shall explain timing how you are using it.  
This tutorial is dedicated to beginners and shall be explicitly easy to understand.  
To improve the tutorial questions are always welcome.

> [@Example-code for timing based on millis() easier to understand through the use of example-numbers / avoiding delay()](https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017):
>
> non-blocking timing. Execute code only from time to time. There are a lot of different ways to learn programming. This thread wants to add another approach that is different to the yet existing ones. UPDATE 06.01.2023 if you are mainly interested in applying non-blocking timing you can do a quick read of this short tutorial / demonstration If you are intersted in understanding the details how it works go on reading here. I use an everyday analogon in this post to explain the basic principle …

best regards Stefan

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 23, 2023, 7:15am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/7 "2023-04-23T07:15:58Z")

</div>

> [@StefanL38](#):
>
> You are trying to reset function millis() to zero.  
> This works for an arduino uno just fine.

But is completely unnecessary

---

<div class="post-metadata">

**Author:** ![say2k](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@say2k](https://forum.arduino.cc/u/say2k)\
**Post date:** [April 23, 2023, 7:50am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/8 "2023-04-23T07:50:02Z")

</div>

Hello and thank you for your answers.  
They have already helped a little.

@Johnwasser?  
I wanted to implement a second counter with the timer0\_millis function. The function should contain that when the button is pressed, the motor is moved to position 0, pauses there for 2 hours and then moves to the position it should have.

Example:  
7 hours after starting, the motor goes to position 3200. When I then press the button, the motor sets itself to position 0. It waits there for two hours. After the two hours, however, it should move to the position it should have after 9 hours. In this case, the 7 hours before + the 2 hours waiting time.

In the example, it would then be at position 6400 (after 8 hours).  
Do you understand what I mean?

---

<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:** [April 23, 2023, 7:53am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/9 "2023-04-23T07:53:21Z")

</div>

Hello [say2k](https://forum.arduino.cc/u/say2k)

Take a view into BlinkWithOutDelay example of the IDE how to learn to design timer modules as for project needed.

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

---

<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:** [April 23, 2023, 7:58am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/10 "2023-04-23T07:58:46Z")

</div>

> [@say2k](#):
>
> 7 hours after starting, the motor goes to position 3200. When I then press the button, the motor sets itself to position 0. It waits there for two hours. After the two hours, however, it should move to the position it should have after 9 hours. In this case, the 7 hours before + the 2 hours waiting time.
> 
> In the example, it would then be at position 6400 (after 8 hours).  
> Do you understand what I mean?

I re-phrase in my own words:

You want to be able to press the button at **any** time ???

If the button has been pressed:

- move motor to position 0
- stay at position 0 for 2 hours
- after 2 hours have passed by move motor to the same position that the motor would have if the button would had not been pressed

This means you have certain time-windows and there is a reference-point in time.

The windows are  
right from **START** until 1 minute  
or to say it different

between **START** and 1 minute past **START**  
between 1 minute and 5 hours past **START**  
between 5 hours and 6 hours past **START**  
between 6 hours and 7 hours past **START**  
between 7 hours and 8 hours past **START**  
between 8 hours and 11 hours past **START**

whenever the button is pressed move to position **zero**  
then wait for two hours relative to the time when the button was pressed  
after 2 hours have passed by move motor to position according to the description above

is this a correct description of the wanted behaviour?

---

<div class="post-metadata">

**Author:** ![say2k](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@say2k](https://forum.arduino.cc/u/say2k)\
**Post date:** [April 23, 2023, 8:07am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/11 "2023-04-23T08:07:02Z")

</div>

> [@StefanL38](#):
>
> I re-phrase in my own words:
> 
> You want to be able to press the button at **any** time ???
> 
> If the button has been pressed:
> 
> - move motor to position 0
> - stay at position 0 for 2 hours
> - after 2 hours have passed by move motor to the same position that the motor would have if the button would had not been pressed

That is exactly what I want to achieve. Absolutely right.

> [@](#):
>
> This means you have certain time-windows and there is a reference-point in time.
> 
> The windows are  
> right from **START** until 1 minute  
> or to say it different
> 
> between **START** and 1 minute past **START**  
> between 1 minute and 5 hours past **START**  
> between 5 hours and 6 hours past **START**  
> between 6 hours and 7 hours past **START**  
> between 7 hours and 8 hours past **START**  
> between 8 hours and 11 hours past **START**
> 
> whenever the button is pressed movoe to position zero  
> then wait for two hours relative to the time when the button was pressed  
> after 2 hours have passed by move motor to position according to the description above
> 
> is this a correct description of the wanted behaviour?

These are the current time slots, yes.

Of course, the time slots can still change. But then they will be fixed in the code again.

The time always runs from the system start.

---

<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:** [April 23, 2023, 8:27am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/12 "2023-04-23T08:27:00Z")

</div>

> [@say2k](#):
>
> These are the current time slots, yes.

How arrucate these time slots should be?

You might take a RTC into account too.

---

<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:** [April 23, 2023, 8:49am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/13 "2023-04-23T08:49:51Z")

</div>

here is a code-version that should do what the description above is.  
This code keeps the repetetive character of your original code.

The code can be much shorter by using arrays. Then you would store the times and the position into these arrays and then just iterate through the elements of the array

This code is prepared to let run the microcontroller all the time  
if the 11 hours have passed by no new position will be set  
But you could add code that pressing a different button that your "go to pos 0"-button  
would start a new cycle without the need to powercycle the microcontroller

the fact that timer millis() is counting up more and more is handled correct by the code  
even if millis() has a rollover.

This is done by calculating time- **differencies** with **un** signed integers.

I recommend that you define constants for your positions.  
Then you have only one place to change the numbers of the positions

```plaintext
#include <AccelStepper.h>

const long OneMinuteIn_ms = 60000;

#define dirPin 2
#define stepPin 4
#define motorInterfaceType 1

#define PIN_BUTTON 7

unsigned long oneMinute_ms = 60 * 1000UL;
unsigned long twoHours_ms = oneMinute_ms * 60 * 2;

unsigned long fiveHours_ms = oneMinute_ms * 60 * 5;
unsigned long sixHours_ms = oneMinute_ms * 60 * 6;
unsigned long sevenHours_ms = oneMinute_ms * 60 * 7;
unsigned long eightHours_ms = oneMinute_ms * 60 * 8;
unsigned long elevenHours_ms = oneMinute_ms * 60 * 11;

unsigned long StartOfCycle;
unsigned long timeSinceStart;

unsigned long StartOfWaitAtPos0;
boolean waitAtPos0_active = false;

AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  stepper.setMaxSpeed(600);
  stepper.setAcceleration(400);
  pinMode(PIN_BUTTON, INPUT_PULLUP);
  Serial.begin(9600);
  delay (1000);
  StartOfCycle = millis();
}

void loop() {

  // check if button is pressed
  if (digitalRead(PIN_BUTTON) == HIGH) {
    // if flag-varaiable is not yet set
    if (!waitAtPos0_active) {
      waitAtPos0_active = true; // set flag-variable
      // store snap-shot of time right in the moment the button is pressed
      StartOfWaitAtPos0 = millis(); 
      stepper.moveTo(0);
      stepper.runToPosition();
    }
  }

  // check if two hours have passed by since button has been pressed
  if (millis() - StartOfWaitAtPos0 >= twoHours_ms) {
    waitAtPos0_active = false;
  }

  // if waiting at position 0 is NOT active
  if (!waitAtPos0_active) {
    managePositions();
  }
}

void managePositions() {

  // calculate how much time has passed by since cylce started
  timeSinceStart = millis() - StartOfCycle;

  // according to the time-slot 
  if (timeSinceStart > 0 && timeSinceStart <= oneMinute_ms) {
    // if target-position is not yet reached
    if (stepper.currentPosition() != -7000) {
      stepper.moveTo(-7000); // set position
      stepper.runToPosition();
    }
  }

  if (timeSinceStart > oneMinute_ms && timeSinceStart <= fiveHours_ms) {
    if (stepper.currentPosition() != -3200) {
      stepper.moveTo(-3200);
      stepper.runToPosition();
    }
  }

  if (timeSinceStart > fiveHours_ms && timeSinceStart <= sixHours_ms) {
    if (stepper.currentPosition() != 0) {
      stepper.moveTo(0);
      stepper.runToPosition();
    }
  }

  if (timeSinceStart > sixHours_ms && timeSinceStart <= sevenHours_ms) {
    if (stepper.currentPosition() != 3200) {
      stepper.moveTo(3200);
      stepper.runToPosition();
    }
  }

  if (timeSinceStart > sevenHours_ms && timeSinceStart <= eightHours_ms) {
    if (stepper.currentPosition() != 6400) {
      stepper.moveTo(6400);
      stepper.runToPosition();
    }
  }

  if (timeSinceStart > eightHours_ms && timeSinceStart <= elevenHours_ms) {
    if (stepper.currentPosition() != 0) {
      stepper.moveTo(0);
      stepper.runToPosition();
    }
  }
}
```

best regards Stefan

---

<div class="post-metadata">

**Author:** ![say2k](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@say2k](https://forum.arduino.cc/u/say2k)\
**Post date:** [April 23, 2023, 10:48am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/14 "2023-04-23T10:48:04Z")

</div>

Hi @StefanL38 ,

thanks for the code. An interesting and yet simple solution. I will try it out in live operation later. It sounds logical to me at first. But I will take a closer look to understand the code.

Defining the positions with a constant makes sense and I will implement it directly. Thanks for the tip.

The arrays are a good idea. I would also like to implement it, as it makes things much easier. However, I would still need a lot of support here to learn.

I bought my first Arduino a week ago and have only just started with programming.

If you still have time and want to, maybe you can help me with the arrays here. As a learning example, which I can use to continue learning.

Thanks a lo

Thank you and everyone else for the support so far! 🙂

---

<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:** [April 23, 2023, 11:13am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/15 "2023-04-23T11:13:23Z")

</div>

> [@say2k](#):
>
> I bought my first Arduino a week ago and have only just started with programming.

Then I suggest that you work through this tutorial:

[Arduino Programming Course](https://startingelectronics.org/software/arduino/learn-to-program-course/)

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

arrays are explained in paragraph 17.  
From the fact that arrays are in §17 you can conclude that arrays are a bit advanced.  
If you at least have understood what variables are you can jump ahead trying to understand arrays

best regards Stefan

---

<div class="post-metadata">

**Author:** ![say2k](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@say2k](https://forum.arduino.cc/u/say2k)\
**Post date:** [April 25, 2023, 7:17am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/16 "2023-04-25T07:17:41Z")

</div>

@StefanL38

Thank you for the link. I'll take my time and work through it. But I need time for that. The first pages were already well structured.

But I still have one question about the code. Maybe you can answer it for me.

If I wanted to add a condition that the "waitAtPos0" is always reset when it is triggered, how could I incorporate that into the code?

Example: The switch was confirmed and the motor turns to position 0, where it waits for two hours. After one hour, the button is pressed again. Then the waiting time should start again at zero.

How could I incorporate this into the if loop? Or do I need an extra counter for this?

---

<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:** [April 25, 2023, 10:23am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/17 "2023-04-25T10:23:09Z")

</div>

> [@say2k](#):
>
> Example: The switch was confirmed and the motor turns to position 0, where it waits for two hours. After one hour, the button is pressed again. Then the waiting time should start again at zero.
> 
> How could I incorporate this into the if loop? Or do I need an extra counter for this?

no extra counter needed

```arduino
// check if button is pressed
  1. if (digitalRead(PIN_BUTTON) == HIGH) {
    // if flag-varaiable is not yet set
    2. if (!waitAtPos0_active) {
      3. waitAtPos0_active = true; // set flag-variable
      // store snap-shot of time right in the moment the button is pressed
      4. StartOfWaitAtPos0 = millis(); 
      5. stepper.moveTo(0);
      6. stepper.runToPosition();
    }
  }

```

explain in your **own words** what the lines numbered with  
1.  
2.  
3.  
4.  
5.  
6.  
are doing.  
The important thing is: find your **own words**.

---

<div class="post-metadata">

**Author:** ![say2k](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@say2k](https://forum.arduino.cc/u/say2k)\
**Post date:** [April 25, 2023, 11:11am UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/18 "2023-04-25T11:11:39Z")

</div>

What do you mean with "your own words"?  
Sounds like a learning task at school 😅

1. If the button is pressed or not set to "high"...
2. Query whether "waitAtPos0" is already active (defined as false at the beginning)
3. Set "waitAtPos0" to true
4. Start the counter in milliseconds
5. Set position to 0
6. Let the motor go to position of point 5

My thoughts on this.

Since I have stored the if query in the loop, it is always executed when the button is confirmed. This also means that the timer is always reset and starts again at 0.

Correct? 😉

---

<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:** [April 25, 2023, 12:40pm UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/19 "2023-04-25T12:40:57Z")

</div>

> [@say2k](#):
>
> Correct?

almost.  
There is a tiny tiny but very important detail that reverses the logic:  
This tiny tiny detail is the **not** -operator.  
Huh where does the **not** -operator hide?

The **not** -operator is the attention-mark

## !

the not-operator simply **inverts** the logic value

not false = true  
not true = false  
in programming the **not** -operator is the attention-mark  
 **!** false = true  
 **!** true = false

So take a very very close look again at the line of code

```arduino
if (!waitAtPos0_active) {

```

there is an attention-mark ! before the variable  
if ( **!** waitAtPos0\_active) {

this means initially / normally the boolean variable **waitAtPos0\_active** has value false  
this means the if-condition is

if ( **!** false) {

which in result means

if (true) {  
// do what is inside the curly braces of the if-condition  
}

what is done inside the if-condition is to set variable **waitAtPos0\_active** to true  
so the next time the if-condition is executed means

if ( **!** true) {

which is

if (false) {

the if-condition is not executed because **!** waitAtPos0\_active evaluates to **false**

This means the if-condition

```arduino
if (!Variable) {
  Variable = true;

```

is some kind of a self-locking mechanism  
that has the effect of  
a new buttonpress does not reset the wait\_at\_position\_0\_timer.  
if you **remove** this if-condition the wait\_at\_position\_0\_timer is reset to zero

For testing you can do two things:

1. reduce the times from hours to seconds
2. add serial printing that makes visible what line of code is already executed

best regards Stefan

---

<div class="post-metadata">

**Author:** ![DaveX](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davex/32/751443_2.png) [@DaveX](https://forum.arduino.cc/u/DaveX)\
**Post date:** [April 25, 2023, 1:20pm UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/20 "2023-04-25T13:20:09Z")

</div>

> [@say2k](#):
>
> 1. Start the counter in milliseconds

I might consider this as "reset the timer" or "update the waiting time start", since the millis() counter part of the timer is always running, and how use use millis() is to compare its count to a starting time.

Then from:

> [@say2k](#):
>
> After one hour, the button is pressed again. Then the waiting time should start again at zero.

... it might be clear that to "update the waiting time start" with each button press, you need to move the line from the "Query whether "2. waitAtPos0" is already active..." conditional and up to the "button is pressed again" conditional.

---

<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:** [April 25, 2023, 1:30pm UTC](https://forum.arduino.cc/t/timer0-mills-error/1118362/21 "2023-04-25T13:30:24Z")

</div>

I am a friend of a well readable code, so you can save yourself an additional comment.

For example:

```plaintext
if (variable == true) doSometing();

```

or

```plaintext
if (variable == HIGH) doSometing();

```

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

[Next page](https://forum.arduino.cc/t/timer0-mills-error/1118362.md?page=2)
