Timing Arduino code
This is a quick and very helpful recipe. There are several time-sensitive applications on the Arduino, and sometimes we need to find the speed at which the Arduino executes various commands. Here we have a simple implementation that will tell us how much time it takes to set a digital pin at HIGH and LOW 10,000 times.
Getting ready
For this recipe, we require an Arduino board connected to a computer via USB.
How to do it…
We just need to write the following code:
// Variable to hold the passed time unsigned long time = 0; int pin = 3; // Declare a pin void setup(){ Serial.begin(115200); // High speed Serial pinMode(pin, OUTPUT); } void loop(){ // Get current time time = micros(); // Code to be tested for execution time for (int i = 0; i< 10000; i++){ digitalWrite(pin, HIGH); digitalWrite(pin, LOW); } // Find the passed time and print it Serial.println(micros() - time); }
How it works…
The micros()
function returns the number of microseconds...