# A timer in a function

**URL:** <https://forum.arduino.cc/t/a-timer-in-a-function/1236076>\
**Category:** Programming\
**Created:** [March 15, 2024, 7:35pm UTC](https://forum.arduino.cc/t/a-timer-in-a-function/1236076 "2024-03-15T19:35:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rainman](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@rainman](https://forum.arduino.cc/u/rainman)\
**Post date:** [March 15, 2024, 7:35pm UTC](https://forum.arduino.cc/t/a-timer-in-a-function/1236076/1 "2024-03-15T19:35:49Z")

</div>

I am making a function with multiple results and in this function a need a timer.

I can't get it to work so I stipt my function to just a blinker. Even this I can't get it to work

Can someone help me?

With regards,  
Edwin

```arduino
******************************************************************************************
#define PinLed 6

bool xBlinkOn;

void Test();

void setup() {
  pinMode(PinLed, OUTPUT);
}

void loop() {
  Test(millis(), &xBlinkOn);

  digitalWrite(PinLed, xBlinkOn);
}

void Test(unsigned long time , bool* xOutput){
  unsigned long timeBlink;
  bool xInit; 

  if (xInit== 0)
  {
    timeBlink = time;
    xInit = 1;
  }

  if ( time - timeBlink >= 500) 
  {
    *xOutput = !*xOutput;
    timeBlink = time;
  }
} //Test

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [March 15, 2024, 7:39pm UTC](https://forum.arduino.cc/t/a-timer-in-a-function/1236076/2 "2024-03-15T19:39:08Z")

</div>

You should initialize local variables.

```arduino
bool xInit;

if (xInit== 0)

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [March 15, 2024, 7:43pm UTC](https://forum.arduino.cc/t/a-timer-in-a-function/1236076/3 "2024-03-15T19:43:25Z")

</div>

Do you understand how this TIMER snippet works ?

```arduino

  // ********************************************** h e a r t b e a t T I M E R
  //is it time to toggle the heartbeatLED ?
  if (millis() - heartbeatTime >= 500ul)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle LED
    digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
  }

```

---

<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:** [March 15, 2024, 7:48pm UTC](https://forum.arduino.cc/t/a-timer-in-a-function/1236076/5 "2024-03-15T19:48:04Z")

</div>

> [@rainman](#):
>
> ```arduino
> if (xInit== 0)
> {
> timeBlink = time;
> xInit = 1;
> 
> ```

won't this turn the LED off (or on) immediately after it is turned on (or off)

try this

```arduino
const byte PinLed = LED_BUILTIN;

bool state;
unsigned long msec0;

void loop ()
{
    unsigned long msec = millis ();
    if (msec - msec0 >= 500) {
        msec0 = msec;
        state = !state;
        digitalWrite (PinLed, state);
    }
}

void setup ()
{
    pinMode (PinLed, OUTPUT);
}

```

---

<div class="post-metadata">

**Author:** ![bojan\_jurca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bojan_jurca/32/1199504_2.png) [@bojan\_jurca](https://forum.arduino.cc/u/bojan_jurca)\
**Post date:** [March 16, 2024, 6:54pm UTC](https://forum.arduino.cc/t/a-timer-in-a-function/1236076/6 "2024-03-16T18:54:20Z")

</div>

> [@rainman](#):
>
> ```arduino
> #define PinLed 6
> 
> bool xBlinkOn;
> 
> void Test();
> 
> void setup() {
> pinMode(PinLed, OUTPUT);
> }
> 
> void loop() {
> Test(millis(), &xBlinkOn);
> 
> digitalWrite(PinLed, xBlinkOn);
> }
> 
> void Test(unsigned long time , bool* xOutput){
> unsigned long timeBlink;
> bool xInit; 
> 
> if (xInit== 0)
> {
> timeBlink = time;
> xInit = 1;
> }
> 
> if ( time - timeBlink >= 500) 
> {
> *xOutput = !*xOutput;
> timeBlink = time;
> }
> } //Test
> 
> ```

Use 'static' variable to initialize and preserve its value through separate function calls.

```arduino
#define PinLed 21

bool xBlinkOn;

void Test();

void setup() {
  Serial.begin (115200);
  pinMode(PinLed, OUTPUT | INPUT);
  xBlinkOn = digitalRead(PinLed); // or 0 or 1
}

void loop() {
  Test(millis(), &xBlinkOn);

  digitalWrite(PinLed, xBlinkOn);
}

void Test(unsigned long time , bool* xOutput){
  static unsigned long lastTimeBlink = 0;

  if ( time - lastTimeBlink >= 500) 
  {
    *xOutput = !*xOutput; 
    Serial.println (*xOutput);
    lastTimeBlink = time;
  }
} //Test

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 16, 2024, 9:28pm UTC](https://forum.arduino.cc/t/a-timer-in-a-function/1236076/7 "2024-03-16T21:28:34Z")

</div>

> [@bojan\_jurca](#):
>
> Use 'static' variable

Yes. But @rainman has used a pointer to bool in the parameter list for the function, as if she intends to use the function one day for multiple blinking \<_whatevers_\>, which won't work because

```arduino
void Test(unsigned long time , bool* xOutput){
  static unsigned long lastTimeBlink = 0;

```

There is but one place to store the time of the last blink.

Or maybe that's what is desired, seems odd.

A solution would be to add a parameter for an _unsigned_ _long_ call by reference variable that would play a roll unique to the _bool_ being time toggled.

a7

---

<div class="post-metadata">

**Author:** ![bojan\_jurca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bojan_jurca/32/1199504_2.png) [@bojan\_jurca](https://forum.arduino.cc/u/bojan_jurca)\
**Post date:** [March 16, 2024, 10:05pm UTC](https://forum.arduino.cc/t/a-timer-in-a-function/1236076/8 "2024-03-16T22:05:26Z")

</div>

> [@rainman](#):
>
> ```arduino
> bool xInit; 
> 
> if (xInit== 0)
> {
> timeBlink = time;
> xInit = 1;
> }
> 
> ```

Of course. I just wanted to simplify the code a little. There is absolutely no reason why xIniti could not be static as well.

```arduino
#define PinLed 21

bool xBlinkOn;

void Test();

void setup() {
  Serial.begin (115200);
  pinMode(PinLed, OUTPUT | INPUT);
  xBlinkOn = digitalRead(PinLed); // or 0 or 1
}

void loop() {
  Test(millis(), &xBlinkOn);

  digitalWrite(PinLed, xBlinkOn);
}

void Test(unsigned long time , bool* xOutput){
  static unsigned long lastTimeBlink = 0;
  static bool xInit = false; 

  if (xInit == false)
  {
    lastTimeBlink = time;
    xInit = true;
  }  

  if ( time - lastTimeBlink >= 500) 
  {
    *xOutput = !*xOutput; 
    Serial.println (*xOutput);
    lastTimeBlink = time;
  }
} //Test

```

I'm not quite sure if this is what your question was.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 17, 2024, 12:28pm UTC](https://forum.arduino.cc/t/a-timer-in-a-function/1236076/9 "2024-03-17T12:28:12Z")

</div>

> [@bojan\_jurca](#):
>
> `static bool xInit = false; `

Here you don't need to say _false_, static variables get initialised.

But this

```arduino
bool xInit; 

  if (xInit== 0)
  {

```

is very asking for trouble. It invokes undefined behaviour, which goes way beyond what you might think, that the variable would necessarily have one, or the other, value.

How I know - learned the hard way

> [@Odd code behaviour, just FYA](https://forum.arduino.cc/t/odd-code-behaviour-just-fya/1153935):
>
> Please enjoy a war story and have a laugh on me. The error in the code below kept me busier for longer than I will ever say. I have solved the problem. Many of you will solve it just by reading it, others will find the compiler warning useful. TBC I have the compiler warnings cranked up to the max. For some reason, I got no warning from the full code. Only late in the game of boiling this down for your consideration did the compiler start warning me. Of how I had gone wrong all by myself. Th…

  
a7

---

<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:** [September 13, 2024, 12:28pm UTC](https://forum.arduino.cc/t/a-timer-in-a-function/1236076/10 "2024-09-13T12:28:51Z")

</div>

Closed on September 13, 2024, 12:28pm UTC

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