# Software Timer stops working

**URL:** <https://community.particle.io/t/software-timer-stops-working/25987>\
**Category:** Troubleshooting\
**Created:** [September 21, 2016, 4:13pm UTC](https://community.particle.io/t/software-timer-stops-working/25987 "2016-09-21T16:13:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexgrauer](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@alexgrauer](https://community.particle.io/u/alexgrauer)\
**Post date:** [September 21, 2016, 4:13pm UTC](https://community.particle.io/t/software-timer-stops-working/25987/1 "2016-09-21T16:13:24Z")

</div>

Hi all,

So im trying the following code. The idea is just to turn on a led when an external interrupt appears (which im simulating with a button fro now), and then automatically turn it off using a timer, After i press the button a few times, the led turns on but never goes off again. So i guess the timer stops working.

Any ideas?

Thanks 😄

```auto
SYSTEM_THREAD(ENABLED)

int out = 3;
int in = 2;

Timer counter1(20, sendSignal);

void sendSignal(){
    digitalWrite(D7, LOW);
    counter1.stop();
}

void zero_cross(void);

void setup() {
    pinMode(D7, OUTPUT);
    digitalWrite(D7, LOW);
    pinMode(D2, INPUT_PULLDOWN);
    attachInterrupt(D2, zero_cross, RISING);
}

void loop() {
}

void zero_cross(){
    counter1.start();
    digitalWrite(D7, HIGH);
}

```

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [September 21, 2016, 4:33pm UTC](https://community.particle.io/t/software-timer-stops-working/25987/2 "2016-09-21T16:33:53Z")

</div>

@alexgrauer, since you starting the timer from the ISR, you need to use `counter1.startFromISR()` instead of the regular start command as per the [documentation](https://docs.particle.io/reference/firmware/photon/#stopfromisr-). 😉

---

<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:** [September 21, 2016, 4:43pm UTC](https://community.particle.io/t/software-timer-stops-working/25987/3 "2016-09-21T16:43:37Z")

</div>

BTW, there is also the option for a one-shot timer (in the same docs), so no need to call `counter1.stop()`

---

<div class="post-metadata">

**Author:** ![alexgrauer](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@alexgrauer](https://community.particle.io/u/alexgrauer)\
**Post date:** [September 21, 2016, 5:22pm UTC](https://community.particle.io/t/software-timer-stops-working/25987/4 "2016-09-21T17:22:40Z")

</div>

Thanks both for the quick response.  
I changed the code but still same thing happens! ☹

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [September 21, 2016, 5:53pm UTC](https://community.particle.io/t/software-timer-stops-working/25987/5 "2016-09-21T17:53:59Z")

</div>

@alexgrauer, what is the rate of zero\_cross interrupts? If it is, as I assume, 50Hz, this would explain the 20ms timer. However, the software timer has a 1ms jitter, meaning it takes up to 1ms to start or stop since the minimum resolution is 1ms. It could be that the calling `counter1.startFromISR() in zero\_cross() is re-triggering the timer before it gets a chance to expire. This would give the appearance that the counter is not working.

---

<div class="post-metadata">

**Author:** ![KyleG](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/kyleg/32/11077_2.png) [@KyleG](https://community.particle.io/u/KyleG)\
**Post date:** [September 23, 2016, 8:52pm UTC](https://community.particle.io/t/software-timer-stops-working/25987/6 "2016-09-23T20:52:07Z")

</div>

@alexgrauer have you been able to fix this issue yet? Let us know if there is anything else we can do to help you.

Thanks @peekay123 and @ScruffR for providing your thoughts 🙂
