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

73 questions
13
votes
1 answer

_delay_ms() is much slower than expected (by a factor of 6) on TinyAVR 0/1 (ATTiny1604)

I designed a custom AVR development board using the Atmel ATTiny1604 microcontroller. It belongs to the new Tiny-0 family, due to its recentness, some required changes to relevant toolchains have yet to be included into the stable version (I prefer…
比尔盖子
  • 439
  • 2
  • 12
9
votes
3 answers

delay(time); vs if(millis()-previous>time); and drift

Going through an old project, I had code on two Arduino Due that looked like this void loop() { foo(); delay(time); } taking to heart the majority of literature on using delay(); I recoded this as void loop() { static unsigned long PrevTime; …
ATE-ENGE
  • 941
  • 3
  • 19
  • 32
8
votes
2 answers

How precise is the timing of pulseIn()?

I've been using the pulseIn() function for processing PWM-based binary data encoding. It works well for distinguishing pulses which are significantly different lengths, e.g. 500us vs. 1500us. That makes it more than sufficient for handling typical…
Peter Bloomfield
  • 10,982
  • 9
  • 48
  • 87
6
votes
1 answer

Syncing/taking external clock for SPI on Arduino Due

I have an Arduino Due with a SAM3X8E chip and am trying to read SPI data from an external source that is communicating in SPI. I was able to get data by identifying specific bytes that are identifiers but realized that the clock for my board is not…
wallenut
  • 63
  • 4
4
votes
4 answers

Use timer0 without affecting millis() and micros()

I'm writing a library which needs an ISR to turn off an LED some time after it was turned on. Since it's all about turning an LED on and off it doesn't need to be very precise. On the other hand I would like to use this library in a program where…
noearchimede
  • 482
  • 2
  • 7
  • 19
3
votes
1 answer

How do I run 4 LEDs sequentially based off of a push button input?

I am trying to create a program that runs 4 LEDs sequentially while also being able to do other things with different inputs and outputs. Because of this, I am using the millis function and not the delay. I have the code working but not exactly how…
Myles
  • 73
  • 7
3
votes
2 answers

Switched from 3ft to 10ft connection wires. My setup doesnt run correctly now. Timing issues?

Running a DC motor(6.5A max input), motor driver (MD10C 7Amp peak output), and Arduino Mega. I am stabilizing an object with an accelerometer as my sensor. I switched to longer wires so I can have my board next to me and the stabilized object far…
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
3
votes
3 answers

Ensuring specific frequency at specific time?

I, at the moment am trying to come up with a simple way where I can program an Arduino to provide a step signal with a given frequency, at a specific time. The frequency has to be ramped up, and within a time duration of 10 seconds shall the…
Carlton Banks
  • 227
  • 3
  • 11
2
votes
2 answers

Arduino Uno Serial.write() how many bits are actually transmitted at once by UART and effect of baudrate on other interrupts

With Arduino Uno Rev3, I am trying to maintain a delicate timing while handling data transmission. I want to send 6 bytes at a time, which takes around ~44 us when I time Serial.write(). The time it takes from Arduino Serial to computer is…
gunakkoc
  • 123
  • 5
2
votes
0 answers

I goofed: I used a digital output rather than an analog output

OK, I have a little hardware issue I need to fix in software for the time being. I say, "For the time being," because I already have the PC Boards printed, and I need to make do for this run of boards. (So sue me for not bread boarding first.) I…
2
votes
2 answers

How to wait outside the loop without delay()?

Is it possible to delay a task inside a function that is called only once (meaning, it's not inside the loop) without using delay()? This library I'm using, for reading sensor measurements via serial has a delay() in it. I want to get rid of it,…
kslstn
  • 123
  • 4
2
votes
2 answers

How can I read the Timer direction in phase correct PWM?

I have had to use Timer0 with phase correct PWM and a prescaler of 1. This results in a 32khz PWM frequency and obviously impacts millis, delay and micros. (Timer1 and Timer 2 are also in use so changing to those is not an option) millis() is…
Simm
  • 51
  • 5
2
votes
1 answer

Obtaining angular velocity from quaternion data (BNO055)

I'm able to get quaternion data from the BNO055 sensor, and would like to convert these to angular velocity for my project. I know you can pull the gyroscope directly for the data, but from what I understand using quaternions and converting those is…
Zhelyazko Grudov
  • 367
  • 2
  • 15
2
votes
1 answer

Relays just switch ON and OFF on delay(), not on millis()

I use fuzzy logic for the running time of relays, but the relays just switch ON and OFF at the interval of the delay() I put at the end of loop. This is my code: void loop () { ph = -180/100.0; hasilec = -1.2; unsigned long now = millis(); …
daffa faiz
  • 91
  • 6
1
2 3 4 5