What will be the approximate output of the micros() function after a delay(1) call?
unsigned long start = micros();
delay(1);
unsigned long end = micros();
Serial.println(end - start);Remember, delay(1) pauses for 1 millisecond, and micros() counts microseconds.
The delay(1) pauses the program for 1 millisecond, which is 1000 microseconds. So the difference between end and start will be about 1000.
What happens to the value returned by micros() when it reaches its maximum value?
Think about how unsigned integers behave in Arduino.
micros() returns an unsigned long that overflows and wraps back to zero after about 70 minutes.
Consider this code snippet:
unsigned long start = micros();
// some code
unsigned long duration = micros() - start;
if (duration < 1000) {
Serial.println("Fast");
} else {
Serial.println("Slow");
}Sometimes it prints "Slow" even when the code runs quickly. Why?
Think about how unsigned subtraction works in Arduino.
Unsigned subtraction handles overflow correctly, so the duration is always correct. The issue is likely the code is actually slow or the timing condition is wrong.
Which option contains a syntax error when using micros() to measure elapsed time?
Look carefully for missing punctuation.
Option A is missing a semicolon after unsigned long start = micros(), causing a syntax error.
You want to measure how many times a button is pressed per second using micros(). Which code snippet correctly calculates the frequency?
Think about when to update the count and the lastPress time.
Option C increments count on each press, then checks if 1 second passed since last print. It prints count, resets it, and updates lastPress correctly. This gives presses per second.
