# 1024hz to milliseconds

**URL:** <https://forum.arduino.cc/t/1024hz-to-milliseconds/255169>\
**Category:** Programming\
**Created:** [September 1, 2014, 9:42am UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169 "2014-09-01T09:42:51Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcarruthers](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@jcarruthers](https://forum.arduino.cc/u/jcarruthers)\
**Post date:** [September 1, 2014, 9:42am UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/1 "2014-09-01T09:42:51Z")

</div>

I need accurate time for my project — specifically milliseconds.

I plan to use a DS3231 RTC and it's 1024hz output.

I can keep count of the output but of course I am stuck with a "millisecond" of 1024 every second.

What's the most elegant way of converting a range of 0 to 1024 to 0 to 1000?

My plan is to keep a count (that rolls from 1023 to 0) and then whenever I request a millisecond do the maths to convert it from 0-999

I can either do the obvious "hzCount \* (hz/1000)" or I could keep a look up table in memory and just do "msLUT[hzCount]"

The truth is that I'll probably be calling a getMillis() every loop.

Anything I'm missing? I'm really keen to learn about how to do these things as efficiently as possible.

James

---

<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 1, 2014, 10:53am UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/2 "2014-09-01T10:53:51Z")

</div>

Have you looked at the way Arduino tackles this problem?

---

<div class="post-metadata">

**Author:** ![AJITnayak](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@AJITnayak](https://forum.arduino.cc/u/AJITnayak)\
**Post date:** [September 1, 2014, 10:54am UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/3 "2014-09-01T10:54:38Z")

</div>

Why didn't used DS1307. You get accurate.problem need to use external crystal oscillator. I think it should work with library. Instead of conversion.

[GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks](https://github.com/JChristensen/DS3232RTC) library avialble here, Let me know if any problem in output. share your code and snapshot of the output window

---

<div class="post-metadata">

**Author:** ![jcarruthers](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@jcarruthers](https://forum.arduino.cc/u/jcarruthers)\
**Post date:** [September 1, 2014, 12:08pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/4 "2014-09-01T12:08:35Z")

</div>

So something like this:

```cpp
	ms += 1
	fracms += 1
	if (fracms == 42) {
		fracms = 0
		ms -= 1 }
	if (ms == 1000) {
		ms = 0 }

```

Every 42 beats it corrects itself.

Seems fair enough.

---

<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 1, 2014, 12:52pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/5 "2014-09-01T12:52:53Z")

</div>

> AMPS-N:  
> Why didn't used DS1307. You get accurate.problem need to use external crystal oscillator. I think it should work with library. Instead of conversion.
> 
> [GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks](https://github.com/JChristensen/DS3232RTC) library avialble here, Let me know if any problem in output. share your code and snapshot of the output window

I think you've misunderstood the question.  
The DS1307 doesn't have a 1.024kHz output, only 4.096, 8.192 and 32.768kHz and 1Hz.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [September 1, 2014, 1:06pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/6 "2014-09-01T13:06:11Z")

</div>

> JamesCarruthers:  
> I need accurate time for my project — specifically milliseconds.  
> ...SNIP....

Sorry, but I can't understand your post - even after having a quick look at the DS3231 datahsheet.  
I have no idea what you want to achieve or even how you think you might do it.

What is your project?  
What amount of error in timing is acceptable? ( +/- how many microseconds or parts-per-million )

...R

---

<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 1, 2014, 1:09pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/7 "2014-09-01T13:09:26Z")

</div>

I read it as "I want to be able to timestamp to (roughly) millisecond accuracy"

---

<div class="post-metadata">

**Author:** ![jcarruthers](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@jcarruthers](https://forum.arduino.cc/u/jcarruthers)\
**Post date:** [September 1, 2014, 1:48pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/8 "2014-09-01T13:48:59Z")

</div>

> AWOL:  
> I read it as "I want to be able to timestamp to (roughly) millisecond accuracy"

Yup, exactly.

I'm trying to work out if using a higher hz would give me a more accurate millisecond by using the same technique.

I guess it's a case of how accurate do I need...

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [September 1, 2014, 3:25pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/9 "2014-09-01T15:25:53Z")

</div>

> JamesCarruthers:
> 
> > AWOL:  
> > I read it as "I want to be able to timestamp to (roughly) millisecond accuracy"
> 
> Yup, exactly.

You are still not expressing your requirement clearly.

It sounds like you are content with an accuracy of +/- 1 msec, but you don't say how long that must be maintained for.

If you want accuracy over a longer period than the Arduino can naturally maintain I would read millis() and the RTC periodically and keep a correction factor variable that represents how much has to be added or subtracted to/from the value for millis() to stay in step with the RTC. In other words if I wanted to record the time of an event I would save it as

eventTime = millis() + correction;

Would it not be easier just to read the RTC when the event occurs and ignore millis() altogether?

...R

---

<div class="post-metadata">

**Author:** ![jcarruthers](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@jcarruthers](https://forum.arduino.cc/u/jcarruthers)\
**Post date:** [September 1, 2014, 4:23pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/10 "2014-09-01T16:23:32Z")

</div>

In the same way I can get seconds() I'd like to be able to get milliseconds()

Accurate as possible within reason.

I'm using them to drive a strip of LEDs — at the moment I'm using only 60 and slight variances are noticeable. It's surprising how the eye can pick up on tiny millisecond differences. So at the moment I only need 1/60 accuracy.

I'd like a solution that is scalable to a large number of LEDs

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [September 1, 2014, 6:09pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/11 "2014-09-01T18:09:26Z")

</div>

> JamesCarruthers:  
> I'm using them to drive a strip of LEDs

If you just want to flash LEDs in a pattern and you are driving all the LEDs from a single Arduino then just use millis().

If they are not staying in sync with each other the way you want that is because you are not using millis() properly - not because of a deficiency in millis().

This demo[several things at a time](http://forum.arduino.cc/index.php?topic=223286.0) illustrates how to use millis().  
...R

---

<div class="post-metadata">

**Author:** ![nilton61](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@nilton61](https://forum.arduino.cc/u/nilton61)\
**Post date:** [September 1, 2014, 8:03pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/12 "2014-09-01T20:03:42Z")

</div>

Why not implement a bresenham algrithm with a ratio of 125/128?

---

<div class="post-metadata">

**Author:** ![jcarruthers](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@jcarruthers](https://forum.arduino.cc/u/jcarruthers)\
**Post date:** [September 1, 2014, 8:09pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/13 "2014-09-01T20:09:42Z")

</div>

> AMPS-N:  
> Why didn't used DS1307. You get accurate.problem need to use external crystal oscillator. I think it should work with library. Instead of conversion.
> 
> [GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks](https://github.com/JChristensen/DS3232RTC) library avialble here, Let me know if any problem in output. share your code and snapshot of the output window

Hi Jack,

I have your library working — many thanks — saved me some time.

I have set my squarewave to 1024Hz — but am unsure how to test it's actually working. I have it attached to a pin and attachInterrupt running a counter function on RISING. But no luck.

---

<div class="post-metadata">

**Author:** ![Jiggy-Ninja](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@Jiggy-Ninja](https://forum.arduino.cc/u/Jiggy-Ninja)\
**Post date:** [September 1, 2014, 8:24pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/14 "2014-09-01T20:24:45Z")

</div>

> JamesCarruthers:  
> In the same way I can get seconds() I'd like to be able to get milliseconds()
> 
> Accurate as possible within reason.
> 
> I'm using them to drive a strip of LEDs — at the moment I'm using only 60 and slight variances are noticeable. It's surprising how the eye can pick up on tiny millisecond differences. So at the moment I only need 1/60 accuracy.
> 
> I'd like a solution that is scalable to a large number of LEDs

I'm with Robin on this, if your timing errors are noticeable to the human eye, you're probably _way_ beyond millisecond error. What kind of LED strips are these?

---

<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 5, 2021, 8:51pm UTC](https://forum.arduino.cc/t/1024hz-to-milliseconds/255169/15 "2021-05-05T20:51:05Z")

</div>


