Go Down

Topic: measuring time (Read 94479 times) previous topic - next topic

allanhurst

#15
Aug 19, 2016, 09:22 am Last Edit: Aug 19, 2016, 09:23 am by allanhurst
Some arduinos use a ceramic resonator, which could be +/-  3%%

some use crystals, which could be as bad as +/- 100ppm - ie  0.01%

if this isn't good enough, buy a 30ppm crystal  - 0.003%

if you want better than this is it gets more expensive

regards

Allan

MarkT

Call millis(), remember the result on each press, then you can subtract one from the other.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

dwightthinker

When I needed accurate time, I use a DS3231 board
that I got from ebay. I set it to do a one second interrupt.
In the interrupt, I take a micros() reading.
When the code is running, I watch for an update from the one
second interrupt, while doing other things that take less than a second.
I take the result of the interrupts time reading from the
uPs clock and calculate the error. I apply it to the current
one second time span.
Even though the uP is running on a resonator, it is relatively
constant over a one second time span.
The drift rate is mostly related to temperature changes.
The change in rate is a little higher for the first 15 minutes
after power up and then slows down a lot.
Dwight

it91

Hi everybody!

I'm almost new in Arduino and I'm trying to send a sample of an Ultrasonic (HC-SR04) sensor to PC over Bluetooth. The main problem is that time the pulseIn function I am using to work out the distance depends on the distance. I mean, the more distance, the more time it takes.

Any idea of how I can set the sampling time? I don't mind if it's not too quick because I will interpolate the signal in matlab.

I have tried using millis() but it doesn't work.

Thanks

AWOL

Quote
I have tried using millis() but it doesn't work
I can absolutely assure you that the millis function does work.

If the delay caused by increased range is a problem, you have at least two solutions:
A) reduce the range
B) increase the speed of sound - try an atmosphere of helium.

mu234

Code: [Select]

unsigned long StartTime = millis();

later...

unsigned long CurrentTime = millis();
unsigned long ElapsedTime = CurrentTime - StartTime;


That will give you the elapsed time in milliseconds, up to about 40 days.  If you need more precise measurement you can use 'micros()' instead of ''millis()' to get microseconds, up to a couple of hours, I think.

The Arduino clock isn't very accurate so your timing may be off by minutes a day.
Hello cheers for posting this!

I am using this code, but when I printout the elapsedTime sometimes I get the number which is not "useful" e.g.

StartTime: 313239   CurrentTime: 313247   ElapsedTime: 4294967288

Any idea what this is, and how to avoid this, if I want to use the elapsedTime in further code. For example, I would like to make a decision with regard to ElapsedTime, namely, if it is above certain time, below, etc.

Any ideas are welcome!

Best.

 




Wawa

#21
Sep 20, 2018, 08:54 am Last Edit: Sep 20, 2018, 08:57 am by Wawa
It just calculates and displays the ElapsedTime in milliseconds.
Up to you to convert/print that in a human readable time format.
Look at this thread how I did that for a laptime sketch.

If you want to make a decision, e.g for 10 minutes,
10 minutes = 10 * 60 * 1000 = 600000 milliseconds.
Then use an if/else statement to compare that to the ElapsedTime you got.
Leo..

odometer

#22
Sep 20, 2018, 02:38 pm Last Edit: Sep 20, 2018, 02:39 pm by odometer Reason: grammar
I am using this code, but when I printout the elapsedTime sometimes I get the number which is not "useful" e.g.

StartTime: 313239 CurrentTime: 313247 ElapsedTime: 4294967288

Any idea what this is, and how to avoid this, if I want to use the elapsedTime in further code.

Sure, I have an idea what it is.

You did the subtraction the wrong way around. What you did was StartTime minus CurrentTime. What you should have done would have been CurrentTime minus StartTime.

MarkT

FWIW, once you get ready to finalize the code, try to compress all the math calcs in this example into a constant float or long up front. The reason being, there is no benefit to recalculating "2.0*pi* 3600 * 1000" over and over, it's 22619467.11... Ideally, Define R in that function as well and you may not even need to use a float.

Less calculations for Arduino to do = better responsiveness by Arduino.
The compiler does constant folding for you, its not the 1970's any more!!
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up