Pages: [1]   Go Down
Author Topic: newbie time problem  (Read 798 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi all,
I'm trying to evaluate the time necessary to run a function but seem to have an issue with units. Here's the code :
Code:
  unsigned long timeNecessary1 = 0;
  unsigned long timeNecessary2 = 0;
  timeNecessary1 = micros();
  IRscan(); // this is my function
  timeNecessary2 = micros();
  Serial.begin(9600);
  Serial.println("");
  Serial.println(timeNecessary1,DEC);
  Serial.println(timeNecessary2,DEC);

Then i substract one from the other. The problem is the numbers don't make any sense. The results are :
  • timeNecessary2 is 2483394992
  • timeNecessary1 is 23462246
When i substract one from the other i get 2.12*10^3 seconds wich does not make sense. Do you guys see any issues with the code?
Logged

Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
micros()
returns the microseconds. 1 micro (u) second = 1/1000000 of a second. So 2.12e3 or 2120 micros actually =s 2120 millionths of a second. This equals 2.12 thousandths of a second, or 2.12 milliseconds, which in turn equals .00212 seconds.

good luck!
Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12048
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well you need to check your arithmetic:  2483394992 - 23462246  = 2459932746 us = 2.46e3 seconds, not 2.12e3

Also how long does the function actually take?  Are you using timer0?
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for quick reply. I do not know how long my function takes, this is why I am doing this test. Is there a better way MarkT?
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 205
Posts: 12844
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The code you posted, does it run with interrupts disabled?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i do not know. What does it mean and how can i figure this out? I am not using any delay() if this is what you mean.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 205
Posts: 12844
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is all the code yours or did you obtain some of it from someone else?  If it is all yours then interrupts are not an issue.  If you have obtained some of it from another source then search for "sei" and "cli" calls.

Which libraries are you using?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is all the code yours or did you obtain some of it from someone else?  If it is all yours then interrupts are not an issue.  If you have obtained some of it from another source then search for "sei" and "cli" calls.

Which libraries are you using?


The code is all mine except for the SoftwareSerial library which I am also using to print the results.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 205
Posts: 12844
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Serial.begin(9600);

...is that call inside loop?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...is that call inside loop?

No everything is done only once in Setup. But i did find the problem and my units were wrong. Thanks guys for your support.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49007
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code is all mine except for the SoftwareSerial library which I am also using to print the results.
SoftwareSerial has been obsolete for a long time. It's time for you to upgrade to NewSoftSerial.
Logged

Pages: [1]   Go Up
Jump to: