9.8 Example: showTime

An Arduino script that displays the elapsed time, in milliseconds (ms) since the last reset of the Arduino is shown. This code uses the serial monitor of the Arduino IDE to display values each time the instruction passes through the loop.

A screen shot of the serial monitor output is shown below:

 

 

The text for this script

/* showTime This script will print the time elapsed, in ms, since the 
last reset of the Arduino. The output will be sent to the serial monitor 
of the Arduino IDE via the Serial.print command. Note the the 
Serial.begin(9600) command is used within the setup() function to 
initialize the serial port and set its baud rate to 9600 bits per 
second. Be sure to set the baud rate of the serial monitor to 9600 bps also. 
*/ 

unsigned long time; 

void setup(){ 
   Serial.begin(9600); 
} 

void loop(){ 
   Serial.print("Time: "); 
   time = millis(); 
   Serial.print(time); 
   Serial.println(" ms"); 
   delay(1000); 
}

 

License

Icon for the Creative Commons Attribution 4.0 International License

Applied Electrical Engineering Fundamentals by David J. McLaughlin is licensed under a Creative Commons Attribution 4.0 International License, except where otherwise noted.

Share This Book