Questions tagged [timing]

This tag is for questions about timing code execution, or external events.

Timing with millis/micros

You can, generally-speaking, time code execution by calling millis() or micros() before and after some event of interest. For example:

unsigned long startTime = millis();  // note start time
someLengthyThing();                  // do the thing we want to time
unsigned long endTime = millis();    // note end time
unsigned long elapedTime = endTime - startTime;  // calculate elapsed time

For more precise measurements use micros() (returns elapsed time in microseconds) rather than millis() (returns elapsed time in milliseconds).


Timing with digitalWrite

If calling millis/micros is not practical (you may have no way of seeing what the results are) you can also time by turning on and off a digital pin. For example:

digitalWrite (13, HIGH);  // we are about to time something - turn D13 on
someLengthyThing();       // do the thing we want to time
digitalWrite (13, LOW);   // we are done - turn D13 off

You can put an oscilloscope or logic analyser on pin 13 (or whatever pin you choose, which naturally should be set to OUTPUT mode) and then see how long the signal is high for.


Timing with writing to a port

Unfortunately, digitalWrite itself takes about 4 µs to execute, so for more accurate timing, do direct port access. For example on the Uno:

PORTB = bit (5);     // we are about to time something - turn D13 on
someLengthyThing();  // do the thing we want to time
PORTB &= ~bit (5);   // we are done - turn D13 off

These port accesses only take 250 ns which is a much lower overhead.


Also see

69 questions
3
votes
3 answers

Control time without delay() Arduino

For the last couple of day I've been trying and failed many times just to not use the delay() funtion in Arduino. I have to admitted that my coding skill are extremely bad. What i trying to do is: If button is pressed: Motor A and B move for 1…
zuzu
  • 55
  • 1
  • 1
  • 5
1
vote
2 answers

How can I execute two functions at the same time?

#include Servo myservo; int pos = 0; int servo_pin = 3; int led_pin = 4; void setup() { myservo.attach(servo_pin); Serial.begin(9600); pinMode(led_pin,OUTPUT); } void loop() { if (Serial.available()) { char serial =…
1
vote
1 answer

How do I set a variable so that after a condition is met the loop ignores input?

I have a analog inputs to my Uno that I am cleaning up and using to keep a digital pin high until the output drops to zero. At this point I want to stop reading that input because it may come back up after being low for a short time. Is there any…
1
vote
1 answer

Atmega328P Prescaler to Microseconds

For a bitbanging project I'm working on, I need to measure microseconds as accurately as possible. The function micros() won't work for me here because of the unpredictable times at which it might roll over. I have been using this project by Louis…
Bo Thompson
  • 261
  • 1
  • 2
  • 12
0
votes
2 answers

Buffering output stream

I'm looking to toggle a pin on and off in a fast and timing-sensitive manner, and I'm wondering if the Arduino Uno has any built in "buffers" that would be able to hold a set of values and send them sequentially to the output pin at high speed,…
user31061
-1
votes
1 answer

delayMIcroseconds and micros execution time not explainable

i just experimented with the timing accuracy of my Arduino Uno R3 using this code: unsigned long beginTime; unsigned long endTime; void setup() { Serial.begin(9600); // open a serial port } void loop() { beginTime = micros(); …
Michael S
  • 101
  • 2
-1
votes
1 answer

Issue using micros() instead of millis()

I have two function implementations that I expected to work the same: #define HEAD_STEP_DELAY 1000 unsigned long HeadLastMicros; void HeadUpdate() { if ((micros()>=HeadLastMicros) && ((micros()-HeadLastMicros)
smbaker
  • 19
  • 5