Offline
Newbie
Karma: 0
Posts: 3
|
 |
« on: March 28, 2012, 03:25:54 am » |
i have to measure time in arduino.I think i should use stopwatch library but i didn't include this library in my computer.Maybe you can help me.thanks
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 96
Posts: 6347
|
 |
« Reply #1 on: March 28, 2012, 10:58:03 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 87
Posts: 9371
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #2 on: March 28, 2012, 12:04:56 pm » |
get microseconds, up to a couple of hours, I think. micros() last for 2^32 micros = 4295 seconds = ~71.5 minutes so just more than one hour
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #3 on: March 28, 2012, 04:04:05 pm » |
thanks all your replies  i have another problem.I will make speedometer of te hydro car.Therefore i have to get time and measure the speed.i will use magnetic reed sensor.i use milis(); hiz =2*pi*r*3600/(elapsed time ); my measuring time code is this. eplased time is ElapsedTime = CurrentTime - StartTime; is it true approach?
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 87
Posts: 9371
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #4 on: March 28, 2012, 05:01:07 pm » |
you missed the factor 1000 as you measure in millis(). Just do the math for some known values and you see if a formula works. e.g 1 second, 10 seconds 3600 seconds. Partial code not tested unsigned long prev = 0;
void loop() { unsigned long now = millis(); float speed = 2.0*pi*r * 3600 * 1000 / (now- prev); speed in meters/hour prev = now; Serial.println(speed); }
|
|
|
|
|
Logged
|
|
|
|
|
Peoples Republic of Cantabrigia
Offline
God Member
Karma: 6
Posts: 634
Arduino happiness
|
 |
« Reply #5 on: March 30, 2012, 09:10:09 am » |
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.
If you want higher sampling speeds, look into the ways that are required to bypass Analog.read and have the ADC go directly to the pins in question. For example, you could set this up as a sub-routine with 100 samples taken, then return the speed based on the micro-seconds that have elapsed. One benefit of not using the analog.Read routines is that the ADC runs independently of the main CPU, you you can likely get some math done between each read (it's what I did with my energy logger - start sampling the voltage channel but work on the current data, then switch to measuring current while processing the just-sampled voltage data).
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Online
Brattain Member
Karma: 240
Posts: 16461
Available for Design & Build services
|
 |
« Reply #6 on: March 30, 2012, 11:39:15 am » |
"The Arduino clock isn't very accurate so your timing may be off by minutes a day. " I think that partly depends on whether the arduino uses a Crsytal vs a Resonator, and also how the seconds are tracked. I have done tests with my duemilanove, it has tracked the official US time with no losses over a day. http://www.time.gov/timezone.cgi?Eastern/d/-5/javaunsigned long previousTime = 0; byte seconds ; byte minutes ; bytes hours ; void setup(){ Serial.begin {9600); } // I think using microseconds is even more accurate if (millis() >= (previousTime) ) { previousTime = previousTime + 1000; // use 100000 for uS seconds = seconds +1; if (seconds == 60) { seconds = 0; minutes = minutes +1; } if (minutes == 60) { minutes = 0; hours = hours +1; } if (hours == 13) { hours = 1; } Serial.print (hours, DEC); Serial.print (":"); Serial.print (minutes,DEC); Serial.print (":"); Serial.println(seconds,DEC); } // end 1 second } // end loop
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 63
|
 |
« Reply #7 on: April 15, 2012, 05:18:43 pm » |
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.
In a way, it is good that he is using "float", because this way there is less chance of overflow. Also, I highly recommend using "micros()" here instead of "millis()". You will probably also want to check for switch bounce (that is, when the sensor registers twice when it should only register once). If the "elapsed time" for one revolution of the wheel is less than 5 milliseconds, then the measurement should be rejected. You will also need to check for speeds close to zero (that is, a long time without the wheel moving).
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 22
Posts: 1370
Now, More Than Ever
|
 |
« Reply #8 on: May 13, 2012, 01:16:10 am » |
Hi, CrossRoads There were a couple of typos, oversights in your code; it didn't compile. I took the liberty of fixing that. It works good! /* CrossRoads Time-clock */
unsigned long previousTime = 0; byte seconds ; byte minutes ; byte hours ; void setup() { Serial.begin (9600); }
void loop () { // I think using microseconds is even more accurate if (millis() >= (previousTime)) { previousTime = previousTime + 1000; // use 100000 for uS seconds = seconds +1; if (seconds == 60) { seconds = 0; minutes = minutes +1; } if (minutes == 60) { minutes = 0; hours = hours +1; } if (hours == 13) { hours = 1; } Serial.print (hours, DEC); Serial.print (":"); Serial.print (minutes,DEC); Serial.print (":"); Serial.println(seconds,DEC); } // end 1 second } // end loop
|
|
|
|
|
Logged
|
Don't Be Upset By The Results You Didn't Get With The Work You Didn't Do
|
|
|
|
Global Moderator
Boston area, metrowest
Online
Brattain Member
Karma: 240
Posts: 16461
Available for Design & Build services
|
 |
« Reply #9 on: May 13, 2012, 02:25:38 am » |
Well, making it from scratch at work while on a short break without a chance to compile will do that. Can't believe I left out void loop(){ !
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 87
Posts: 9371
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #10 on: May 13, 2012, 05:00:05 am » |
// use 100000 for uS 100000 ? ==> 1000000 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Online
Brattain Member
Karma: 240
Posts: 16461
Available for Design & Build services
|
 |
« Reply #11 on: May 13, 2012, 11:05:01 am » |
Yeah 5 zeroes, 6 zereos, whatever it takes 
|
|
|
|
|
Logged
|
|
|
|
|
|