- 1. Troubleshooting if Arduino applications don't work
- 2. Make a copy of the preference.txt file.
- 3. Troubleshooting with installing new Arduino software versions
- 4. Arduino.h and WProgram.h
- 5. Compiler differences between Arduino software versions
- 6. How to Install Arduino libraries
- 7. General Arduino tips
- 8. Change the editor font
- 9. Arduino C++ anomalies
- 10. Arduino peculiarities
- 11. Saving ATmega program memory and RAM
- 12. Easy printing with the overload stream operator <<
- 13. Using inline functions
- 14. My Arduino wish list
1. Troubleshooting if Arduino applications don't work
My Arduino applications and libraries are tested with the Arduino software version used at the time of development. After installation of a new Arduino software version, problems may arise with existing projects. I don't have time to regularly test all my software on newer Arduino versions, so let me know if problems occur.
To solve a problem, read this page, and first check if the Arduino works well with some simple sample projects. Normally, by building up the application step by step, you will find out what the problem is.
Don't forget to select the right board: Tools > Board > and the serial port: Tools > Serial Port.
2. Make a copy of the preference.txt file.
Sometimes the Arduino software doesn't work anymore, at least on Windows XP; the preference.txt file may be corrupt. Put the old preference.txt file back and try again. If this doesn't work, the computer has to be start up again. For Windows XP c:\Documents and Settings\\Application Data\Arduino\preferences.txt.
3. Troubleshooting with installing new Arduino software versions
After the installation of a new Arduino software version, problems arise often with existing projects.
Compiler differences and missing or incompatible libraries may cause compiler errors like these:
'blabla' was not declared in this scope
'blabla' does not name a type
expected ',' or '...' before 'blabla'
ISO C++ forbids declaration of 'blabla' with no type
See here what to do:
- Libraries can't be found
Installed additional libraries are not moved to the new Arduino environment, they have to be installed again.
- Incompatible libraries
Older additional libraries may be not compatible with newer Arduino software versions, so update the libraries too.
- Compiler differences
4. Arduino.h and WProgram.h
At the Arduino version 1.0, the name of WProgram.h is changed to Arduino.h. This was not handy because old software doesn't work anymore. From now on, in each Arduino program, the following code has to be included:
I want to get rid from this. This inconvenience can be avoided by including such code in a new version of Arduino.h, so we can use just:
5. Compiler differences between Arduino software versions
After the installation of a new Arduino software version, problems arise often due to compiler differences. See here what to do:
- Tabsheet ino extention
Tab sheets should have the extension *.ino now, otherwise global variables from other tab sheets are not recognized.
- Inline public member functions
Public member functions can't be inline anymore.
6. How to Install Arduino libraries
Look at a library in the folder \libraries\ and see that a library is just a folder with the c++ code file (.cpp) and a header (.h) file. Other files are not needed. A library will be included in a project with #include <libraryBlaBla.h>, if the library is included in the project itself, in a tab, than use #include "libraryBlaBla.h". To convert an Arduino tab into a library file, change the extension from .ino to .cpp.
7. General Arduino tips
- One c++ code file name (.ino) must be equal to the project folder name. A copy of a project shall not run anymore because the project map name is changed to "Copy of ...". To avoid this, place the project map into another map, this can further be copied without problems.
- Older Atmega boards, before 2010, may have a slower baudrate of 19200 instead of 57600 baud for recent boards. During uploading an error may occur like: avrdude: stk500_getsync(): not in sync: resp=0x00. To fix upload problems, edit the file hardware\arduino\boards.txt:
Change atmega328.upload.speed=57600 to atmega328.upload.speed=19200
- Turn on the internal 20k pull up resistors: pinMode(pinBlabla, INPUT); digitalWrite(pinBlabla, HIGH);
8. Change the editor font
Make the code more readable by changing this in the preference.txt file:
9. Arduino C++ anomalies
Although Arduino is programmable in C++, there are a few exceptions.
- Instead of a mandatory function main(), the Arduino need to have these two functions:
void setup(void) and void loop(void).
- The Arduino uses for a c++ code file the extension .ino, instead of .cpp. The extension .cpp may be used too, but at least one file must have the extension .ino.
- Dynamic memory allocation (new en delete) is not allowed.
- Library inline functions must be placed in the header file. Inline member functions can be placed in the cpp file.
- #include "blabla.h" can be used instead of #include <blabla.h>, this is not according c++ rules.
- At the Arduino, we can use explicit only at declarations of constructors.
- Function templates must be placed in a header instead of a cpp file ( template <class T> ).
10. Arduino peculiarities
While I was reducing program size, weird things came to light. See the library Simple Arduino library for debouncing switches and buttons.
- Variables of type bool instead of byte, increases the program size with 6 byte.
- Initialization of member variables with parenthesis in a constructor, consumes 6 bytes more when using for instance pin(pin) instead of pin(_pin).
- Reading a public member variable directly (classBlabla.x) consumes 54 bytes more than using a getter (classBlabla.read()).
- Initialization of member variables to 0 costs 2 bytes, although they are set to 0 by default.
11. Saving ATmega program memory and RAM
- In contrast to the usual practice, don't initialize variables; this takes extra program space. Variables will be initialized to zero automatically.
- Use the Flash library, see below.
- Place constant data in program memory instead of RAM:
const PROGMEM char str = "blabla";
- Comment out unused and debug code with // or /* */. Unused member functions however, take no extra code.
- Use byte instead of int if possible:
for(byte i=0; i<3; i++) instead of int saves about 12 byte program memory.
- Avoid floating point operations; the large floating point library uses a lot of program memory.
- Saving ATmega memory with the Flash library:
Serial.println("blabla") uses 7 byte RAM and 7 byte program memory. Avoid this by using the Flash library:
Serial.println(F("blabla")) uses only 7 byte program memory. Or use:
Serial << F("blabla").
- Don't use endl, << endl; takes 60 byte more program space than << "\n";
12. Easy printing with the overload stream operator <<
- Using the Streaming.h library makes printing to the PC or LCD simpler. It generates no extra program memory:
Serial << F("blabla") << data << F("blabla");
Streaming is such an important c++ feature that it should be integrated standardly in the Arduino environment. However, this is not the case; download the latest streaming library from Mikal Hart here.
13. Using inline functions
- About 8 byte program memory can be saved when functions are made inline. It depends on the number of times the function is called and on the size of the function, whether it makes sense. By using refactoring, code is split into small member functions, which is part of Extreme Programming. When these functions are inline, no extra code will be generated.
- Note that member functions defined inside their class are implicit inline. At the Arduino, we can use explicit only at declarations of constructors.
14. My Arduino wish list
- Remember the window size.
- The serial monitor needs a stop button.
- Solve incompatibility, using just #include "Arduino.h".