Hardware Reference
In-Depth Information
for a specii ed period of time before reading a sensor, or slowing down a loop
that is running too fast.
delayMicroseconds()
delayMicrosecond() is similar to delay() , but instead of waiting for a specii ed
number of milliseconds, it waits for a specii c number of microseconds.
This function is accurate to a certain point; values above 16,383 produce
inaccurate results. If you need an accurate delay above 16,000 microseconds
(or 16 milliseconds), use a mix of delay() and delayMicroseconds() , like in
the following snippet of code, where the Arduino is asked to wait for 22.5 mil-
liseconds, or a total of 25,500 microseconds.
delay(25); // waits for 25 milliseconds
delayMicroseconds(500) waits for 500 microseconds
millis()
millis() returns the number of milliseconds that the sketch has been running,
returning the number as an unsigned long. This can be used to check how long
the current sketch has been running, but it can also be used to calculate how
long a function takes to run, by comparing the number of milliseconds before
and afterward.
unsigned long timeBefore;
unsigned long timeAfter;
timeBefore = millis(); //Get the time before running a function
aLongFunction(); //Run a function that could take some time
timeAfter = millis(); //And now get the time after running the function
This data is stored in a counter that will overl ow (go beyond the data capac-
ity and return to zero) after approximately 50 days.
micros()
micros() is almost identical to the millis() function, except it returns the
number of microseconds in an unsigned long. The counter overl ows far more
quickly than millis() ; roughly every 70 minutes.
unsigned long time;
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.print(“Time: “);
 
Search WWH ::




Custom Search