Debugging Code Templates
A coding template provides example code that utilizes the Arduino Debugger to help find the bug(s) in your code. The codint templates are broken up into 3 categories - Conditional Logic, Loops, and Methods.
Loops
Key Breakpoints
When working with any type of loop, there are four key areas you’ll want to place your breakpoints:
- Before the loop
This breakpoint will help you see the current state of your variables & pins BEFORE any part of your loop is executed.
- Last line of your loop
This breakpoint helps to check that your ENDING your loop with correct values.
Bug Tips & Things to Check
- Counter Update
- Is your counter updating by the correct amount? Try writing out all of the values your counter should reach; is your code reaching all of those values?
- End Condition
- Is your loop ending on the correct value? A common mistake here is using "<" or ">" when you ended to use "<=" or ">=". For example, if you want your loop to count from 0 to 10, then the last value of the counter should be 11.
For Loop Logic
byte i = 0; //initialize for loop counter
debugger.add(&i, BYTE, "i");//add loop counter to variable watch
//Add other variables to watch here
debugger.breakpoint("Before For Loop");
for(i; i < end_condition; i++){
//Your code here
debugger.breakpoint("End For Loop");
}
While Loop Logic
byte i = 0; //initialize for loop counter
debugger.add(&i, BYTE, "i");//add loop counter to variable watch
//Add other variables to watch here
debugger.breakpoint("Before While Loop");
while(i < end_condition)
//Your code here
i++;
debugger.breakpoint("End While Loop");
}
Do While Loop Logic
byte i = 0; //initialize for loop counter
debugger.add(&i, BYTE, "i");//add loop counter to variable watch
//Add other variables to watch here
debugger.breakpoint("Before Do While Loop");
do{
//Your code here
i++;
debugger.breakpoint("End Do While Loop");
}while(i < end_condition);