# Background timer \[multithreading\]

**URL:** <https://community.particle.io/t/background-timer-multithreading/21308>\
**Category:** General\
**Created:** [March 21, 2016, 4:51pm UTC](https://community.particle.io/t/background-timer-multithreading/21308 "2016-03-21T16:51:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcodat](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@marcodat](https://community.particle.io/u/marcodat)\
**Post date:** [March 21, 2016, 4:51pm UTC](https://community.particle.io/t/background-timer-multithreading/21308/1 "2016-03-21T16:51:26Z")

</div>

I need to start a timer when my photon (acting as a clinet) receives a signal via TCP.

The photon should keep track of time _precisely_, read via I2C IMU data (new data incoming every 0.01s) and read analog values from a pressure pad.

Since the timer need to be accurate to the milisec I was thinking of running it on its own thread but I cannot find any documentation on how to achieve this (nor I’m sure whether this could actually be done).

I am open to any other suggestions that can help me tackling this problem.  
cheers

---

<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:** [March 21, 2016, 6:11pm UTC](https://community.particle.io/t/background-timer-multithreading/21308/2 "2016-03-21T18:11:35Z")

</div>

Just running on its own thread might not neccessarily give you the desired precission, but have a look at the `IntervalTimer` library which runs on timed interrupts.  
[https://build.particle.io/libs/568be2fb07d120fc16000662](https://build.particle.io/libs/568be2fb07d120fc16000662)  
[https://github.com/pkourany/SparkIntervalTimer](https://github.com/pkourany/SparkIntervalTimer)

---

<div class="post-metadata">

**Author:** ![marcodat](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@marcodat](https://community.particle.io/u/marcodat)\
**Post date:** [March 21, 2016, 7:14pm UTC](https://community.particle.io/t/background-timer-multithreading/21308/3 "2016-03-21T19:14:09Z")

</div>

Thanks a lot for the reply. You are really helping me out with my project!

My understanding of what you were saying is that I should use the SparkIntervalTimer in this way

```
IntervalTimer myTimer;
myTimer.begin(myfunction, 1000, hmSec);

```

This would call myfunction() after 0.5 sec.

I would like to store the time recorded from the timer in a variable. Is this possible?

---

<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:** [March 21, 2016, 7:24pm UTC](https://community.particle.io/t/background-timer-multithreading/21308/4 "2016-03-21T19:24:35Z")

</div>

@marcodat, given that SparkIntervalTimer will create a true interrupt, I would recommend against doing hardware resource specific calls within the ISR, specifically doing I2C calls. You could do the I2C sampling in loop() given that sampling is done at 100ms intervals.

I think before you begin, having a timing/state diagram of each step would be great for understanding what you are trying to achieve. 😉

---

<div class="post-metadata">

**Author:** ![marcodat](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@marcodat](https://community.particle.io/u/marcodat)\
**Post date:** [March 21, 2016, 7:58pm UTC](https://community.particle.io/t/background-timer-multithreading/21308/5 "2016-03-21T19:58:58Z")

</div>

thank for the reply. i implemented a pseudo-state diagram to explain what I would need to implement.  
 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/d/d2066f67affbb5b04ac51d26c96c26d26507f172.JPG)

---

<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:** [March 21, 2016, 8:20pm UTC](https://community.particle.io/t/background-timer-multithreading/21308/6 "2016-03-21T20:20:14Z")

</div>

@marcodat, from what I see, there are two key timing requirements. The first is to start a “reliable and accurate” millisecond timer that can started, stopped and read. The second is a 100ms timer which is used to trigger I2C data collection from the mpu6050.

The first timer can be created using SparkIntervalTimer with a simple counter in the ISR. The second is dependent on the timing accuracy needed. This could be as simple as a 100ms millis() non-blocking timer. Sending data to the server at that rate should not be a problem assuming it is straight TCP or UPD data. Can you elaborate?

---

<div class="post-metadata">

**Author:** ![marcodat](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@marcodat](https://community.particle.io/u/marcodat)\
**Post date:** [March 21, 2016, 10:38pm UTC](https://community.particle.io/t/background-timer-multithreading/21308/7 "2016-03-21T22:38:52Z")

</div>

I can, thank you a lot for your support!

Just a note for future reference: to set the second timing interval (ie the dmp’s sampling rate) `setRate()` can be used in the MPU6050\_6Axis\_MotionApps20.h  
This sets the sampling frequency of the MPU6050 and it’s better changing this parameter rather the interrupt frequency to avoid FIFO buffer overflow.

---

<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:** [March 21, 2016, 10:57pm UTC](https://community.particle.io/t/background-timer-multithreading/21308/8 "2016-03-21T22:57:03Z")

</div>

@marcodat if I understand correctly, the mpu6050 will generate it’s own interrupts or will you be polling it?

---

<div class="post-metadata">

**Author:** ![marcodat](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@marcodat](https://community.particle.io/u/marcodat)\
**Post date:** [March 22, 2016, 10:41am UTC](https://community.particle.io/t/background-timer-multithreading/21308/9 "2016-03-22T10:41:49Z")

</div>

@peekay123 that’s correct, the mpu6050 generates its own interrupts.
