# Millis timing by called function instead of function calling

**URL:** <https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441>\
**Category:** Programming\
**Created:** [February 23, 2020, 8:32pm UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441 "2020-02-23T20:32:40Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![moserroger](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@moserroger](https://forum.arduino.cc/u/moserroger)\
**Post date:** [February 23, 2020, 8:32pm UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/1 "2020-02-23T20:32:40Z")

</div>

Hello all

Is it possible to have the millis() timing done by the function being called?

I tried this which doesn't work. Only the first function call will be treated:

```cpp
const unsigned long interval = 1000;
unsigned long currentmillis;
unsigned long previousmillis;
int value1, value2, value3, value4;

void setup()
{
}

void loop()
{
currentmillis = mills();
otherfunction(value1, value2);
otherfunction(value3, value4);
}

void otherfunction(const int &a, const int &b)
{
if ( currentmillis - previousmillis > interval )
{
previousmillis = currentmillis;
do something;
}
}

```

I would prefere something similar to the first version. The following works understandably:

```cpp
const unsigned long interval = 1000;
unsigned long currentmillis;
unsigned long previousmillis;
int value1, value2, value3, value4;

void setup()
{
}

void loop()
{
currentmillis = mills();
if ( currentmillis - previousmillis > interval )
{
previousmillis = currentmillis;
otherfunction(value1, value2);
otherfunction(value3, value4);
}
}

void otherfunction(const int &a, const int &b)
{
do something;
}

```

Is there any way to make the first version work?

Thank you!

---

<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:** [February 23, 2020, 8:34pm UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/2 "2020-02-23T20:34:04Z")

</div>

What is "mills()"?  
Where is it defined?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [February 23, 2020, 8:41pm UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/3 "2020-02-23T20:41:17Z")

</div>

Yes you can do millis timing inside a function. But you still have to obey the rules of cooperative multitasking, which require that you do not dally around for long periods of time in the function - you have to check something quickly and return quickly.

---

<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:** [February 23, 2020, 8:41pm UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/4 "2020-02-23T20:41:51Z")

</div>

If you passed a unique call ID number to the function it cpuld be used by the function as an index to an array of variables such as previousMillis and interval which means that the timing for each will not get affected by other calls to the same function

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [February 23, 2020, 8:43pm UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/5 "2020-02-23T20:43:49Z")

</div>

in the first case your calling the same function twice and that function updates the value of previousmillis. so when it is called almost immediately after the first call, previousmillis is the same value as currentmillis

---

<div class="post-metadata">

**Author:** ![moserroger](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@moserroger](https://forum.arduino.cc/u/moserroger)\
**Post date:** [February 24, 2020, 5:36am UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/6 "2020-02-24T05:36:17Z")

</div>

> TheMemberFormerlyKnownAsAWOL:  
> What is "mills()"?  
> Where is it defined?

Typo... Sorry

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [February 24, 2020, 8:01am UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/7 "2020-02-24T08:01:58Z")

</div>

For example, see [This Delay without Delaying Hack](https://github.com/WestfW/Duino-hacks/blob/master/delay_without_delay/delay_without_delay.ino)

---

<div class="post-metadata">

**Author:** ![moserroger](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@moserroger](https://forum.arduino.cc/u/moserroger)\
**Post date:** [February 24, 2020, 9:37pm UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/8 "2020-02-24T21:37:13Z")

</div>

> westfw:  
> For example, see [This Delay without Delaying Hack](https://github.com/WestfW/Duino-hacks/blob/master/delay_without_delay/delay_without_delay.ino)

Thank you WestfW.

This is the kind of function I was thinking about but could come up with myself.

---

<div class="post-metadata">

**Author:** ![moserroger](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@moserroger](https://forum.arduino.cc/u/moserroger)\
**Post date:** [February 24, 2020, 9:39pm UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/9 "2020-02-24T21:39:57Z")

</div>

> UKHeliBob:  
> If you passed a unique call ID number to the function it cpuld be used by the function as an index to an array of variables such as previousMillis and interval which means that the timing for each will not get affected by other calls to the same function

Thank you UKHeliBob

This is also a very useful solution.

Would it be very memory consuming if I had, say, an array for 100 calls of previousmillis?

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [February 24, 2020, 11:51pm UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/10 "2020-02-24T23:51:49Z")

</div>

> Would it be very memory consuming if I had, say, an array for 100 calls of previousmillis?

Well, 400 bytes. That's a substantial fraction of the RAM available on a Uno, and 100 timed "tasks" is probably at the point where you have to be really careful not to let any of them get "slow."  
This is one of the simplest forms of multi-tasking.

---

<div class="post-metadata">

**Author:** ![moserroger](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@moserroger](https://forum.arduino.cc/u/moserroger)\
**Post date:** [February 25, 2020, 5:15am UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/11 "2020-02-25T05:15:07Z")

</div>

> westfw:  
> Well, 400 bytes. That's a substantial fraction of the RAM available on a Uno, and 100 timed "tasks" is probably at the point where you have to be really careful not to let any of them get "slow."  
> This is one of the simplest forms of multi-tasking.

Ok. Thank you for the code. I will try it these days...

---

<div class="post-metadata">

**Author:** ![moserroger](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@moserroger](https://forum.arduino.cc/u/moserroger)\
**Post date:** [February 25, 2020, 5:28pm UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/12 "2020-02-25T17:28:12Z")

</div>

> westfw:  
> For example, see [This Delay without Delaying Hack](https://github.com/WestfW/Duino-hacks/blob/master/delay_without_delay/delay_without_delay.ino)

I must have gotten it wrong. I tried the following but it will not deal with one function after the other. I guess it is still the same problem of calling the same function over and over again. How to get it more structured?

```arduino
int value1, value2, value3, value4;

void setup()
{
}

bool delay_without_delaying(){unsigned long time) {
  // return false if we're still "delaying", true if time ms has passed.
  // this should look a lot like "blink without delay"
  static unsigned long previousmillis = 0;
  unsigned long currentmillis = millis();
  if (currentmillis - previousmillis >= time) {
    previousmillis = currentmillis;
    return true;
  }
  return false;
}

void otherfunction(const int &a, const int &b)
{
do something with a;
do something with b;
}

void loop()
{
if ( delay_without_delaying(1000) )
     otherfunction(value1, value2);
if ( delay_without_delaying(1000) )
     otherfunction(value3, value4);
}

```

UPDATE: I did realize that I didn't add the function with &since. I haven't tried it yet but am quite sure this will do the trick.

But how is the value of &since updated being passed by reference? Each call will need memory for an unsigned long?

Can I use this one only if I call it once?

```arduino
boolean delay_without_delaying(unsigned long time)

```

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [February 28, 2020, 3:57am UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/13 "2020-02-28T03:57:10Z")

</div>

> Each call will need memory for an unsigned long?

Each "timer" will need storage for an unsigned long.  
It's pretty much exactly like UKHeliBob's example, except with individual ulongs for each purpose, instead of an indexed array of them.

---

<div class="post-metadata">

**Author:** ![moserroger](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@moserroger](https://forum.arduino.cc/u/moserroger)\
**Post date:** [March 2, 2020, 11:18am UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/14 "2020-03-02T11:18:48Z")

</div>

I think I found a way to successfully use an id to to control Serial output. I combined the input of ukhelibob and westfw's function.

I want to use the id to store the values of previousMillis in an array. But there must be something I am doing wrong as the previousMillis value doesn't seem to be updated:

```cpp
//#define S_U_LONG static unsigned long
#define INTERVAL 1000

float sensor = 15.5;
int sensor2 = 20;
bool delay_without_delaying(unsigned long);

void setup()
{
  Serial.begin(115200);
    while (!Serial);
}

bool delay_without_delaying(const byte &id, unsigned long time) {
  // return false if we're still "delaying", true if time ms has passed.
  // this should look a lot like "blink without delay"
  static byte idIndex = 0;
static unsigned long previousMillis[100];
if ( idIndex < id )
{
  idIndex = id;
  unsigned long currentmillis = millis();
  if (currentmillis - previousMillis[id] >= time)
  {
    previousMillis[id] = currentmillis;
    return true;
  }
  return false;
}
}

void loop()
{
  static byte id;
//currentMillis = millis();
if ( delay_without_delaying(id=0, INTERVAL) )
{
  Serial.print("HellOO: ");
  Serial.println(sensor);
  Serial.println(id);
}
if ( delay_without_delaying(id++, INTERVAL) )
{
  Serial.print("Hello2: ");
  Serial.println(sensor2);
  Serial.println(id);
}
if ( delay_without_delaying(id++, INTERVAL) )
  Serial.println("Test");
  Serial.println(id);
}

```

---

<div class="post-metadata">

**Author:** ![moserroger](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@moserroger](https://forum.arduino.cc/u/moserroger)\
**Post date:** [March 4, 2020, 5:12pm UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/15 "2020-03-04T17:12:45Z")

</div>

Up to now I used this as if condition which caused problems updating previousmillis:

```cpp
if ( idIndex < id )

```

I realized now that previousmillis is updated if I save id in idIndex and use this as index of the previousmillis array. Which will help in the functioning of the blink without delay function.

Now the big problem remaining is the selection of the correct id passed to the function. I tried to accomplish with a flag and some more detailed if conditions to better filter the id so they would only be permitted in increasing order until id is reset to zero. Up to now this doesnt work how ever.

Flag and if conditions:

```cpp
  static byte idIndex = 0;
  static bool idFlag = false;
if ( id == 0 )
    idFlag = true;
else if ( id > 0 && id > idIndex && id == idIndex+1 )
    idFlag == true;

if ( idFlag )
{
  idIndex = id;

```

The whole current code:

```cpp
#define INTERVAL 1000

float sensor = 15.5;
int sensor2 = 20;
bool delay_without_delaying(unsigned long);

void setup()
{
  Serial.begin(115200);
    while (!Serial);
}

bool delay_without_delaying(const byte &id, unsigned long time) {
  // return false if we're still "delaying", true if time ms has passed.
  // this should look a lot like "blink without delay"
  static unsigned long previousmillis[50];
  unsigned long currentmillis = millis();
  static byte idIndex = 0;
  static bool idFlag = false;
if ( id == 0 )
    idFlag = true;
else if ( id > 0 && id > idIndex && id == idIndex+1 )
    idFlag == true;

if ( idFlag )
{
  idIndex = id;
  if (currentmillis - previousmillis[idIndex] >= time) {
    previousmillis[idIndex] = currentmillis;
    return true;
  }
  return false;
idFlag = false;
}
}

void loop()
{
  static byte id;

if ( delay_without_delaying(id=0, INTERVAL) )
{
  Serial.print("HellOO: ");
  Serial.println(sensor);
  Serial.println(id);
}
if ( delay_without_delaying(id++, INTERVAL) )
{
  Serial.print("Hello2: ");
  Serial.println(sensor2);
  Serial.println(id);
}
if ( delay_without_delaying(id++, INTERVAL) )
{
  Serial.println("Test");
  Serial.println(id);
}
}

```

What would I have to change to only allow the id's in "correct" increasing order?

Thank you!

---

<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:** [May 6, 2021, 12:20am UTC](https://forum.arduino.cc/t/millis-timing-by-called-function-instead-of-function-calling/638441/16 "2021-05-06T00:20:18Z")

</div>


