# I want to add a Millis timer in place of a for loop

**URL:** <https://forum.arduino.cc/t/i-want-to-add-a-millis-timer-in-place-of-a-for-loop/1094375>\
**Category:** Programming\
**Created:** [February 24, 2023, 4:04pm UTC](https://forum.arduino.cc/t/i-want-to-add-a-millis-timer-in-place-of-a-for-loop/1094375 "2023-02-24T16:04:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmoneytv](https://avatars.discourse-cdn.com/v4/letter/c/dec6dc/32.png) [@cmoneytv](https://forum.arduino.cc/u/cmoneytv)\
**Post date:** [February 24, 2023, 4:04pm UTC](https://forum.arduino.cc/t/i-want-to-add-a-millis-timer-in-place-of-a-for-loop/1094375/1 "2023-02-24T16:04:47Z")

</div>

Hello, I am making a state machine in Arduino right now. One of the functions I have currently runs based on number of cycles with a for loop. I want it to run based on time (i.e. instead of running for 300 cycles, run for 30 seconds). I can't seem to get Millis to work. Please help. The specific function is below.

```cpp
void runStopLongFast ()
{
  enum class simSettings : uint8_t
  {
    IDLE,
    MANUAL,
    CONTINUOUS,
    RUNSTOP,
  };

  static simSettings currMode = simSettings :: RUNSTOP;
    for(int i = 0; i<300; I++)
    {  
    if (digitalRead(manualSwitchA) == HIGH && digitalRead(manualSwitchB) == LOW)
      {
        if (digitalRead(speedSwitch) == LOW && digitalRead(intervalSwitch) == LOW)
        {
          fastCycle();
          Serial.println("In Run-StopLongFast-Cycle");
        }
        else 
        {
          currMode = simSettings :: RUNSTOP;
        }
      }
      else if (digitalRead(manualSwitchA) == HIGH && digitalRead(manualSwitchB) == HIGH)
      {
        currMode = simSettings :: MANUAL;
      }
      else if (digitalRead(manualSwitchA) == LOW && digitalRead(manualSwitchB) == HIGH)
      {
        currMode = simSettings :: CONTINUOUS;
      }
    }      
    for(int i = 0; i<600; I++)
    {  
    if (digitalRead(manualSwitchA) == HIGH && digitalRead(manualSwitchB) == LOW)
      {
        if (digitalRead(speedSwitch) == LOW && digitalRead(intervalSwitch) == LOW)
        {
          digitalWrite(cycleOutput, LOW);    
          delay(50); 
          Serial.println("In Run-StopLongFast-IDLE");
        }
        else 
        {
          currMode = simSettings :: RUNSTOP;
        }
      }
      else if (digitalRead(manualSwitchA) == HIGH && digitalRead(manualSwitchB) == HIGH)
      {
        currMode = simSettings :: MANUAL;
      }
      else if (digitalRead(manualSwitchA) == LOW && digitalRead(manualSwitchB) == HIGH)
      {
        currMode = simSettings :: CONTINUOUS;
      }
    } 
    for(int i = 0; i<1; I++)
    {  
    if (digitalRead(manualSwitchA) == HIGH && digitalRead(manualSwitchB) == LOW)
      {
        if (digitalRead(ad1Switch) == LOW)
        {
          digitalWrite(cycleOutput, HIGH);    
          delay(50); 
          Serial.println("AD1");
        }
        else 
        {
          currMode = simSettings :: RUNSTOP;
        }
      }
      else if (digitalRead(manualSwitchA) == HIGH && digitalRead(manualSwitchB) == HIGH)
      {
        currMode = simSettings :: MANUAL;
      }
      else if (digitalRead(manualSwitchA) == LOW && digitalRead(manualSwitchB) == HIGH)
      {
        currMode = simSettings :: CONTINUOUS;
      }
    }      
    for(int i = 0; i<600; I++)
    {  
    if (digitalRead(manualSwitchA) == HIGH && digitalRead(manualSwitchB) == LOW)
      {
        if (digitalRead(ad1Switch) == LOW)
        {
          digitalWrite(autoDowntime1, HIGH);    
          delay(50); 
          Serial.println("AD1");
        }
        else 
        {
          currMode = simSettings :: RUNSTOP;
        }
      }
      else if (digitalRead(manualSwitchA) == HIGH && digitalRead(manualSwitchB) == HIGH)
      {
        currMode = simSettings :: MANUAL;
      }
      else if (digitalRead(manualSwitchA) == LOW && digitalRead(manualSwitchB) == HIGH)
      {
        currMode = simSettings :: CONTINUOUS;
      }
    }           
}

```

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [February 24, 2023, 4:15pm UTC](https://forum.arduino.cc/t/i-want-to-add-a-millis-timer-in-place-of-a-for-loop/1094375/2 "2023-02-24T16:15:15Z")

</div>

Welcome! Congratulations on properly posting your code first time.  
Wrong place in the forum (should be in Programming...), but that can be fixed by a moderator.

However, I can't tell you what's wrong with what you did with millis(), as I don't see it in your code. Please post that version, much easier to help you with it.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 24, 2023, 4:37pm UTC](https://forum.arduino.cc/t/i-want-to-add-a-millis-timer-in-place-of-a-for-loop/1094375/3 "2023-02-24T16:37:11Z")

</div>

it would also be worth to explain the circuit and context as the code is quite weird..  
the typical structure for a state machine would look like this

```cpp
void runStopLongFast () {
  enum simSettings : uint8_t {IDLE, MANUAL, CONTINUOUS, RUNSTOP};
  static simSettings currMode = RUNSTOP;
  
  switch (currMode) {
    case IDLE:
      ...
      break;

    case MANUAL:
      ...
      break;

    case CONTINUOUS:
      ...
      break;

    case RUNSTOP:
      ...
      break;
  }
}

void setup() {}
void loop() {}

```

---

<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:** [February 24, 2023, 6:49pm UTC](https://forum.arduino.cc/t/i-want-to-add-a-millis-timer-in-place-of-a-for-loop/1094375/4 "2023-02-24T18:49:33Z")

</div>

> [@cmoneytv](#):
>
> ` for(int i = 0; i<300; I++)`  
> ` for(int i = 0; i<600; I++)`  
> ` for(int i = 0; i<1; I++)`  
> ` for(int i = 0; i<600; I++)`

WHY do these loops use "I++" and not "i++"?!?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 24, 2023, 7:02pm UTC](https://forum.arduino.cc/t/i-want-to-add-a-millis-timer-in-place-of-a-for-loop/1094375/5 "2023-02-24T19:02:18Z")

</div>

> [@johnwasser](#):
>
> WHY do these loops use "I++" and not "i++"?!?

I would bet They were typed likely outside the IDE with a spell checker on which decided that in English you don’t write a small cap i and turned it into I automatically

That happens to me when typing code directly in the forum

For(int I=… damn autocorrect !

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [February 24, 2023, 7:05pm UTC](https://forum.arduino.cc/t/i-want-to-add-a-millis-timer-in-place-of-a-for-loop/1094375/6 "2023-02-24T19:05:22Z")

</div>

Given the distinction between i and I in C, it is a different variable. Which would imply that, if this code actually compiled, both i and I have been declared. Lethal.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 24, 2023, 8:29pm UTC](https://forum.arduino.cc/t/i-want-to-add-a-millis-timer-in-place-of-a-for-loop/1094375/7 "2023-02-24T20:29:28Z")

</div>

That’s why I think it’s an autocorrect mistake

It happens to me often when I type from my iPhone or iPad

---

<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:** [February 25, 2023, 1:35pm UTC](https://forum.arduino.cc/t/i-want-to-add-a-millis-timer-in-place-of-a-for-loop/1094375/8 "2023-02-25T13:35:18Z")

</div>

The standard blink without delay-example code makes it unnescessary **complicated** to understand non-blocking timing.

The main reason is that this example and other tutorials do **not** focus on the **fundamental difference** how non-blocking timing works.

You should learn the principle **prior** to looking into the code.  
**Knowing the basic principle will make it much easier to understand the code**

I recommend a version that puts the details about non-blocking timing into a function  
that can be used in a single if-condition that says what the function is doing

checking if **TimePeriodIsOver** ()

```arduino
if( TimePeriodIsOver(myTimingVariable,Period) ) {
  // do timed action
}

```

> [@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:** ![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:** [August 24, 2023, 1:36pm UTC](https://forum.arduino.cc/t/i-want-to-add-a-millis-timer-in-place-of-a-for-loop/1094375/9 "2023-08-24T13:36:17Z")

</div>

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