Breakpoints are a great way to see how the compiler runs to your code. Now my question is, is there a possibility to use breakpoints when you debug your code?
As noted in Majenko's answer, the Arduino IDE doesn't provide a breakpoint mechanism but Atmel Studio does support breakpoints.[*]
However, if you have a switch and an LED, you can track the progress of your program in a way that provides some of the benefits of breakpoints. You would add a subroutine, say
BPReport(), that via serial output or an LCD reports values of critical variables, then lights the LED and waits until the switch has been pressed and released, with debounce. Call your
BPReport() routine whereever you want an unconditional breakpoint. For conditional breakpoints, you can have a routine
BPReportIf(cond) which calls
cond is true. If you don't want to output via serial, you might use several LEDs or an LCD, and you might use several switches if you want external break controls (for example,
cond could be a test of one of the extra switches).
[*] Some hardware debuggers modify downloaded code each time breakpoints are added, changed, or removed. That usage will wear out flash memory more rapidly than just occasionally downloading to it. If a chip has been heavily used for such debugging, don't use that chip in a production system.
Though Majenko his answer is correct there are some other options.
As to the real hardware debugging as stated by majenko I would say:
- Install and use a real IDE, such as Atmel Studio or the arduino eclipse plugin called sloeber (I'm author), and
- Use a full hardware debugger or hardware that has it on board like the Arduio zero or hardware using other debugging technology like the ESP8266 that allows USB debugging
An other debugging option from a completely different category is to organize your code so that decision logic (hardware independent) and action (hardware dependent ) are completely separated.
Then compile your sketch as a local program and debug the "decision logic" on your local machine. This method does not allow for "hardware debugging". This method also enables unit testing.
Note that your local machine is probably a 32 or 64 bitter and most Arduino's are 8 bitters which will result in differences in data types which is an extra attention point when using this method.
The Arduino-Debug library provides a simple on-target debugger for Arduino sketches. Debug command are added directly to the sketch. A debugger command shell is started on break-points and assertions.
The screen-shot above shows the example sketch run on an Arduino Mega with Serial output monitor used by application and Serial1 used for the debugger shell.
Sketch Debug Commands
- ASSERT(cond) Check assert condition. If false the debug shell is called. The sketch cannot continue.
- BREAKPOINT() The debug shell is called.
- BREAK_IF(cond) The debug shell is called if the condition is true.
- CHECK_STACK(room) Check that there is room (bytes) on the stack. If false the debug shell is called.
- DEBUG_STREAM(dev) Use the given stream device for debug session. Typically Serial.
- OBSERVE(expr) Print the expression to the debug stream.
- OBSERVE_IF(cond,expr) Print the expression to the debug stream if the condition is true.
- REGISTER(var) Register a variable for access from the debug shell. Debug Shell Commands
Debug Shell Commands
- ?VARIABLE Print variable address and value.
- @VARIABLE Print pointer variable address and reference value.
- backtrace Print simple call-stack.
- commands Print list of commands (see also help).
- data Print contents of data area, i.e. global variables.
- go Leave debug shell and continue execution of sketch.
- heap Print contents of heap, i.e. dynamic allocated data.
- help Print list of commands.
- memory Print memory status.
- quit Stop sketch.
- stack Print contents of stack, i.e. call frames, arguments, return addresses.
- variables Print list of registered variables.
- where Print source code file and line where debug shell was called.
All debug shell commands can be abbreviated to single character commands. Please see the README for further details; installation details, example sketch and benchmarking.