Thursday, June 5, 2014
Arduino IDE - Time Function
Time Function millis()
millis() - is a time function that measures time since the microcontroller starts. It reads the time lapse every time the function was executed.
For example, the statement timeNow=millis(); will read the time lapse since microcontroller start in milliseconds and store it in the variable timeNow.
void setup()
{Serial.begin(9600);}
void loop()
{
int timeNow=millis();
Serial.println(timeNow);
}
Another example, we want to measure how fast a microcontroller could loop.
int timeNow=0, timePrevious=0, timeLapse=0;
void setup()
{Serial.begin(9600);}
void loop()
{
timeNow=millis();
timeLapse=timeNow-timePrevious;
Serial.print(timePrevious);Serial.print("\t");Serial.print(timeNow); Serial.print("\t");Serial.println(timeLapse);
timePrevious=timeNow;
}
Here's how it works......
On first loop,
statement 1: timeNow is read and recorded timeNow=millis(); say timeNow=5;
statement 2: Time lapse is computed i.e. timeLapse=timeNow - timePrevious ;
where initially declared timePrevious =0. then timeLapse=5-0=5ms
statement 3: Print or display the time for viewing.
statement 4: value of timeNow is passed on to timePrevious by timePrevious=timeNow;
This is to record the time a loop was last executed.
Labels:
Arduino Sketch
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment