Go Down

### Topic: newbie time problem (Read 1 time)previous topic - next topic

#### matejj

##### Mar 22, 2011, 02:17 am
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: [Select]
`  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?

#### baum

#1
##### Mar 22, 2011, 02:25 am
Code: [Select]
`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!

#### MarkT

#2
##### Mar 22, 2011, 02:29 am
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?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### matejj

#3
##### Mar 22, 2011, 02:36 am
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?

#4
##### Mar 22, 2011, 02:44 am
The code you posted, does it run with interrupts disabled?

#### matejj

#5
##### Mar 22, 2011, 03:04 am
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.

#6
##### Mar 22, 2011, 03:44 am
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?

#### matejj

#7
##### Mar 22, 2011, 03:52 am

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.

#8
##### Mar 22, 2011, 04:04 am
Code: [Select]
`Serial.begin(9600);`

...is that call inside loop?

#### matejj

#9
##### Mar 22, 2011, 04:09 am

...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.

#### PaulS

#10
##### Mar 22, 2011, 11:10 am

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.