Time

From robotics

Time can be used with 4 different arduino functions

millis();

micros();

delay(milliseconds);

delayMicroseconds(microseconds);

Unsigned long systime = millis(); 

This line of code will return the time in milliseconds from when the board was powered on or reset, to the variable systime. This function will restart from zero after 50 days.

Unsigned long systime = micros();

This line of code will return the time in microseconds from when the board was powered on or reset, to the variable systime. This function will restart from zero after 70 minutes.

The time functions are most commonly used to time events. An example of this is used in the function below below.

void autonomous(long time) { 
unsigned long startTime=millis(); // assign startTime to millis
while (millis()-startTime<time) { // run until time is up
// autonomous code
}
}


delay(10); 

will stop the code except for interrupts for 10 milliseconds. There is some overhead so the delay is sometimes off by upto 1ms

delayMicroseconds(10);

will stop the code except for interupts for 10 microseconds. There is overhead to this function and it is inaccurate for times less than 5 microseconds.