Share tips you have come across

If you have access to a Logic Analyzer or a Storage Oscilloscope, you can use them to time events when troubleshooting your code.
A write to pin D13 on the Arduino using the PINB command, toggles that pin’s state.
Toggle: if the pin was ON it goes OFF, if it was OFF it goes ON.
examples:
PINB = 0x20 // Toggle D13 ATMEGA 328
PINB = 0x80 // Toggle D13 ATMEGA 1284 or the ATMEGA 2560
Having two of these commands in a row gives a pulse of ~62.5ns (nano seconds) for 16Mhz clocks.
Creating a pulse on D13 can be used to mark the being of some code.
Adding a second pulse on D13 at the end of that code times how long that code took to execute.

  • The first image shows the timing relationship between the two PINB = 0x20; instructions, ~60ns.
  • The second image shows how long it takes to go from the end of loop() back to the beginning ~1.13us and it shows the time it takes to go between the two pulses ~100us.


See also post #968