# Software Timers period value question?

**URL:** <https://community.particle.io/t/software-timers-period-value-question/29376>\
**Category:** Firmware\
**Created:** [February 17, 2017, 9:59pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376 "2017-02-17T21:59:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sheng](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sheng](https://community.particle.io/u/sheng)\
**Post date:** [February 17, 2017, 9:59pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/1 "2017-02-17T21:59:36Z")

</div>

Hi,

I am trying to setup a software timer with the period as an unsigned long variable as below:

```
Timer sTimer(testTime, loadSTimer, true);

```

I want to confirm if this is doable or not since its not working for me. But if I used the below code, it would work:

```
 Timer sTimer(3000, loadSTimer, true);

```

I look at the document examples, the period value is define with milliseconds value not with define variable. Could variable be used in the period field with software timer? Other ways around this? Thanks

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [February 17, 2017, 10:44pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/2 "2017-02-17T22:44:17Z")

</div>

> [@sheng](#):
>
> Could variable be used in the period field with software timer?

It works for me. Where are you declaring your variable **testTime**? It needs to be before the creation of your timer. However, I'm not sure you want to do this anyway, if you're using the variable to change the timer's period after it's already created. If you need to change the period of the timer, you should use the changePeriod() function, and you can pass a variable to it.

---

<div class="post-metadata">

**Author:** ![sheng](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sheng](https://community.particle.io/u/sheng)\
**Post date:** [February 21, 2017, 4:56pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/3 "2017-02-21T16:56:48Z")

</div>

@Ric, look like I am declaring the testTime variable after the creation of the timer. Below is the flow for the timer. ChangePeriod() seem like a better solution then changing the period with a variable. I will change my test case and see how it goes. Thanks for the help

```
unsigned long testTime;
void setup()
{
...
}

Timer sTimer(testTime, loadSTimer, true);

void loadSTimer()
{
  Serial.println ("loadSTimer" );

}

void loop()
{
...
schedTimeEvent ();
...
}

void schedTimeEvent ()
{
testTime = 3000;
sTimer.start();
}
```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [February 21, 2017, 5:50pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/4 "2017-02-21T17:50:42Z")

</div>

The periode of the timer is set **_when_** this line is executed

```cpp
Timer sTimer(testTime, loadSTimer, true);

```

and not when the value of `testTime` changes.  
Moreover, since constructors are not executed in the order they are placed in the source but in the order the linker decides to have them executed, you won’t even have `setup()` executed at the time your `sTimer` is constructed.

Hence, you should initialise `testTime` for the construction and when you want to change the periode call `sTimer.changePeriod()` as @Ric already said.

BTW, calling `schedTimeEvent()` in `loop()` doesn’t seem to be the way to go.  
Software Timers are free running, so once started, you won’t have to call that function anymore.

---

<div class="post-metadata">

**Author:** ![sheng](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sheng](https://community.particle.io/u/sheng)\
**Post date:** [February 21, 2017, 7:23pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/5 "2017-02-21T19:23:39Z")

</div>

@ScruffR, at first, I thought the timer will only start and get the period when sTimer.start(); is called. Now, I know its not the case as you mention above.

By using sTimer.changePeriod() as @Ric suggested, I was able to get the result that I wanted.

> [@ScruffR](#):
>
> BTW, calling schedTimeEvent() in loop() doesn't seem to be the way to go.Software Timers are free running, so once started, you won't have to call that function anymore.

I am using a one shot timer in my case since I am trying to launch a schedule event once the timer is reached. I have 8 entries in the schedule that why I place it in the loop and need to be able to change the timer period base on the schedule time. I just need the timer to execute once per entries and different time period. Please correct me if I am wrong, from the document if I add `true` to the third field of the timer like `Timer sTimer(testTime, loadSTimer, true);` it become one shot and it only run once.

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [February 21, 2017, 10:06pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/6 "2017-02-21T22:06:44Z")

</div>

> [@sheng](#):
>
> if I add true to the third field of the timer like Timer sTimer(testTime, loadSTimer, true); it become one shot and it only run once.

That is true. You need to be careful how you start or change the period of your timer. If you try to call start() in loop, you may just keep resetting the timer, and it will never fire. For instance, with this code below:

```cpp
Timer t(2000, timerHandler, true);

void setup() {
  	Serial.begin(9600);
  	delay(3000);
	Serial.println("Starting");
}

void loop() {
	delay(1000);
	t.start();
}

void timerHandler() {
	Serial.print("Fired ");
}

```

This timer will never fire, because it has a 2 second period, but it is being reset (with the start() function) in loop every second. Calling changePeriod() also starts a stopped timer, so there's no need to call start() if you're using that function. It would be helpful if you could give us more information about what you're trying to accomplish. You say you have 8 entries, does that mean you want 8 timers? Once they all fire, is that it, to do you then change the periods, and fire them again? Are we talking about second type timeframes, minutes, hours?

---

<div class="post-metadata">

**Author:** ![sheng](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sheng](https://community.particle.io/u/sheng)\
**Post date:** [February 21, 2017, 11:10pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/7 "2017-02-21T23:10:04Z")

</div>

@Ric, I want to use one timer with the ability to modify the period in milliseconds. The 8 entries is 15 min apart. I am taking the schedule time minus the current time as the period to launch the timer. Example: schedule time is 1:15pm and current time is 1:00pm. The difference is 15 minute, so the period for the timer is 15\*60000 =900000 milliseconds. After triggering the timer at 900000 milliseconds, move on to next entries. etc… until 8 entries is done.

I would like to limit one timer instead of 8 timer flying around. This might cause confusion or problems. I am using one shot timer to have the timer fire once for each entry only. All the entries does the same thing, just at different time. I think a reusable timer will be a good fit in this case.

Will the changePeriod() function start a timer with the new period even if I never use timer.start() anywhere in my code? Right now, I am using changePeriod() then timer.start() after. It seem like its giving me the result that I want.

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [February 21, 2017, 11:18pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/8 "2017-02-21T23:18:50Z")

</div>

> [@sheng](#):
>
> Will the changePeriod() function start a timer with the new period even if I never use timer.start() anywhere in my code?

Yes, changePeriod() will start the timer without ever calling start(). I just tested that to be sure.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [February 22, 2017, 6:14am UTC](https://community.particle.io/t/software-timers-period-value-question/29376/9 "2017-02-22T06:14:10Z")

</div>

If each of your periodes is 15min then why not have one multi-shot timer that just stops after 8 events?

```cpp
void timerCallback() {
  static int count = 0;
  // do whatever
  if (++count >= 8) {
    t.stop();
    count = 0; // for next 8-part run
  }
}

```

---

<div class="post-metadata">

**Author:** ![sheng](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@sheng](https://community.particle.io/u/sheng)\
**Post date:** [February 23, 2017, 11:27pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/10 "2017-02-23T23:27:49Z")

</div>

@ScruffR, I should said the 8 entries is over 24 hour, 15min apart so its could be 1:15am , 3:30pm, 5:00pm… etc. I was able to get it working by using the changePeriod() with a variable Period in a loop. Thanks all for the help.

---

<div class="post-metadata">

**Author:** ![mohitagnihotri](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mohitagnihotri/32/13462_2.png) [@mohitagnihotri](https://community.particle.io/u/mohitagnihotri)\
**Post date:** [December 10, 2018, 11:54am UTC](https://community.particle.io/t/software-timers-period-value-question/29376/11 "2018-12-10T11:54:51Z")

</div>

Hi @ScruffR & @sheng

I tried to implement software timer but not working.

In my test code, I send the timer value like ‘5000’ through Particle.function() to turn the led D7 HIGH after 5 seconds but it doesn’t work.  
If I direct assign the value=5000; then it works. Why?

```auto
int value;
int status(String command);

void setup()
{   
    pinMode(D7, OUTPUT);
    
 // Particle.variable("value", value); to check value of value variable in console
    Particle.function("status", status);
    
}

int status(String command) //enter value like 2000 through console to set 2 sec timer
{   
    int value = command.toInt(); //convert string to int for timer
    Timer t(value, action);
    return 0;
}

    void action()
{
    digitalWrite(D7, HIGH);
}

void loop() {

}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 10, 2018, 11:59am UTC](https://community.particle.io/t/software-timers-period-value-question/29376/12 "2018-12-10T11:59:58Z")

</div>

Your timer object gets destroyed as soon you leave the function where it is created.  
You need a global timer if you want to do it that way.

---

<div class="post-metadata">

**Author:** ![mohitagnihotri](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mohitagnihotri/32/13462_2.png) [@mohitagnihotri](https://community.particle.io/u/mohitagnihotri)\
**Post date:** [December 10, 2018, 12:06pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/13 "2018-12-10T12:06:46Z")

</div>

Oh!

I did some changes again and tested but not working even now.  
Please edit to the code to make it workable.

```auto
int value;
Timer t(value, action);
int status(String command);

void setup()
{   
    pinMode(D7, OUTPUT);
    
 // Particle.variable("value", value); to check value of value variable in console
    Particle.function("status", status);
    
}

int status(String command) //enter value like 2000 through console to set 2 sec timer
{   
    value = command.toInt(); //convert string to int for timer
    t.start();
    return 0;
}

    void action()
{
    digitalWrite(D7, HIGH);
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 10, 2018, 12:09pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/14 "2018-12-10T12:09:14Z")

</div>

If you want to change the previously set periode of the SoftwareTimer you should use the respective function 😉  
[https://docs.particle.io/reference/device-os/firmware/argon/#changeperiod-](https://docs.particle.io/reference/device-os/firmware/argon/#changeperiod-)

> [@mohitagnihotri](#):
>
> Please edit to the code to make it workable.

Uhm, no - we'd rather have people use the docs.

Explanation why this isn't working either:  
When you create (instantiate) the global timer `t`, your global `value` variable is initialised to 0 and that value is taken for the timer periode, which in turn means that the timer is not supposed to ever run.  
Changing the value of the variable that _was_ used when you create the timer has no relevance for the timer anymore since `value` was only passed by value and not by reference.

---

<div class="post-metadata">

**Author:** ![mohitagnihotri](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mohitagnihotri/32/13462_2.png) [@mohitagnihotri](https://community.particle.io/u/mohitagnihotri)\
**Post date:** [December 10, 2018, 12:22pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/15 "2018-12-10T12:22:08Z")

</div>

I tried a lot and read documentation several times and change the code accordingly.  
I spent whole day for this really but not working even now.

Is it correct way to convert “String command” to int by this line of code? And it should define as int or what?

```auto
int value = command.toInt();

```

I changed the code again

```auto
int value;
Timer t(value, action);
int status(String command);

void setup()
{   
    pinMode(D7, OUTPUT);
    
    Particle.variable("value", value); 
    Particle.function("status", status);
    
}

int status(String command) //enter value like 2000 through console to set 2 sec timer
{   
    value = command.toInt(); //convert string to int for timer
    t.changePeriod(value);
    return 0;
}

    void action()
{
    digitalWrite(D7, HIGH);
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 10, 2018, 12:27pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/16 "2018-12-10T12:27:24Z")

</div>

You almost got it, but while you now have the preparation of your timer correct, you never start it.

So try this.

```cpp
int status(String command) //enter value like 2000 through console to set 2 sec timer
{   
    value = command.toInt(); //convert string to int for timer
    t.changePeriod(value);
    t.start();
    return value; // <-- returning a useful value helps more that unconditionally return 0
}

```

---

<div class="post-metadata">

**Author:** ![mohitagnihotri](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mohitagnihotri/32/13462_2.png) [@mohitagnihotri](https://community.particle.io/u/mohitagnihotri)\
**Post date:** [December 10, 2018, 12:37pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/17 "2018-12-10T12:37:18Z")

</div>

😄  
Yup

I changed what you suggest but not worked.

```auto
int value;
Timer t(value, action);
int status(String command);

void setup()
{   
    pinMode(D7, OUTPUT);
   // t.start();
    Particle.variable("value", value); 
    Particle.function("status", status);
    
}

int status(String command) //enter value like 2000 through console to set 2 sec timer
{   
    value = command.toInt(); //convert string to int for timer
    t.changePeriod(value);
    t.start(); //As you suggested and also tried with declare t.start() in setup() too
    return value;
}

    void action()
{
    digitalWrite(D7, HIGH);
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 10, 2018, 12:43pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/18 "2018-12-10T12:43:12Z")

</div>

I probably won’t get around trying it myself 😉  
Meanwhile you could try to initialise `value` like this before your timer declaration.

```cpp
int value = 5000;

```

BTW, what device and device OS version are you targeting?

---

<div class="post-metadata">

**Author:** ![mohitagnihotri](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mohitagnihotri/32/13462_2.png) [@mohitagnihotri](https://community.particle.io/u/mohitagnihotri)\
**Post date:** [December 10, 2018, 12:51pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/19 "2018-12-10T12:51:16Z")

</div>

Now, it is working as per time I passed through console when I declared int value = 5000;  
Why it is so?

I am executing on Photon and v0.7.0

Thank you so much to assist me.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 10, 2018, 1:01pm UTC](https://community.particle.io/t/software-timers-period-value-question/29376/20 "2018-12-10T13:01:08Z")

</div>

> [@mohitagnihotri](#):
>
> Now, it is working as per time I passed through console when I declared int value = 5000;
> 
> Why it is so?

Probably because the timer won't even be fully instantiated when the inital period is set to 0.  
On the one hand that's not too surprising as a timer without periode makes no sense and before the introduction of `setPeriod()` there was no point in having such a timer at all.  
But when `setPeriod()` was introduced later on that previous reasoning would have become invalid. However, as it seems, the implementation of the constructor was not adapted for that.

One other note. Since you only want to use the timer to switch the LED on once, you should rather use a one-shot timer

```cpp
Timer t(value, action, true);

```

[Next page](https://community.particle.io/t/software-timers-period-value-question/29376.md?page=2)
