Debugging Arduino code
Common errors with Arduino programs.
Arduino code is written in a mixture of C,C++.
When software is written the error messages returned are usually because the compiler doesn't like what you have written.
Some things to look out for:
----------------------------------------------------------------------------------------------------------------------------------------------------
// variables created by the build process when compiling the sketch
extern int __bss_end;
extern void *__brkval;
int memoryFree()
{
int freeValue;
if((int)__brkval == 0)
freeValue = ((int)&freeValue) - ((int)&__bss_end);
else
freeValue = ((int)&freeValue) - ((int)__brkval);
return freeValue;}
Cut and paste it into your sketch, it works very well. Print the memoryFree() result
----------------------------------------------------------------------------------------------------------------------------------------------------
Serial.println(A);
If B and C are of type int there will be a problem when multiplying B by 10,000 when B is > 6, and C when > 65. The maximum value for an unsigned int is 65535. A and B need to be of type long (or something greater than int) to accommodate the result.
----------------------------------------------------------------------------------------------------------------------------------------------------
When software is written the error messages returned are usually because the compiler doesn't like what you have written.
Some things to look out for:
----------------------------------------------------------------------------------------------------------------------------------------------------
- confusing = with == in an if statement or other comparison statement. Late at night this can be difficult to see! But is easily done. = assigns a value from one variable to another whereas == is used to compare.
- forgetting a ; at the end of a line - causes interesting error messages from the compiler depending on where you have forgotten to put ;
- Running out of memory, this can cause very obscure errors. Here is a small program to include in code and run occasionally to ensure that there is plenty of memory available. The code is:
// variables created by the build process when compiling the sketch
extern int __bss_end;
extern void *__brkval;
int memoryFree()
{
int freeValue;
if((int)__brkval == 0)
freeValue = ((int)&freeValue) - ((int)&__bss_end);
else
freeValue = ((int)&freeValue) - ((int)__brkval);
return freeValue;}
Cut and paste it into your sketch, it works very well. Print the memoryFree() result
----------------------------------------------------------------------------------------------------------------------------------------------------
- Maths can cause all sorts of strange errors, especially if you are using a mixture of int, float, unsigned int, long, unsigned long. A useful thing is to put Serial.println(result_of _math) within your code to see that the result is what you hope it is.
Serial.println(A);
If B and C are of type int there will be a problem when multiplying B by 10,000 when B is > 6, and C when > 65. The maximum value for an unsigned int is 65535. A and B need to be of type long (or something greater than int) to accommodate the result.
----------------------------------------------------------------------------------------------------------------------------------------------------