Read this before posting a programming question …
1. Arduino programming language
The Arduino basically uses the industry-standard C++ language, implemented by the GNU g++ compiler.
For information on basic things like loops, strings, functions, data types, and so on try consulting one of the many online C++ references. Hint: Google “C++ tutorial”.
The main sketch is C++, however the IDE does do a certain amount of pre-processing (in particular, generating function prototypes for you) before submitting the code to the main C++ compiler (see point 12 below).
2. Version 1.0 of the IDE*
A few things were changed in the 1.0 release version of the IDE. In particular some older examples and libraries may have this at the start:
If you get errors try locating such places and changing that line to:
* IDE = Integrated Development Environment
Many useful features are implemented by libraries. These are basically xxx.cpp and xxx.h files collected together into a library folder. Some come with the IDE. See here for how to add additional libraries:
(It is easy enough to write your own, they are basically just C++ source code).
4. Reference page
Many functions specific to the Arduino (like activating pins, reading analog input etc.) are documented on the reference page:
Before rushing on to the forum saying you don’t understand how to make something work, try working your way through the examples (File Menu -> Examples in the IDE). A few examples under the belt make everything else much easier.
6. Getting help on the forum
There are quite a few experienced people on the forum anxious to help you, and help you get as much as you can out of your Arduino. You can help them do that by making helpful posts:
- Make an informative subject description, not “help me, I’m a noob”, nor something in all capitals. Try to avoid saying “urgent”. That’s your problem, not ours.
- Describe your problem in detail.
- If it relates to an electronics part (chip or board), give the exact part number and preferably a link to the data sheet.
- Describe how you have connected things like switches. Are they wired to ground? Or +5V? Are there pull-up or pull-down resistors? Post a circuit if there is doubt.
- Post your complete sketch (program code)! If you don’t you waste time while people ask you to do that.
- When you post your code put it between [code] … [/code] tags. You can do that by hitting the </> button above the posting area.
- If you get an error, post the error (copy and paste). Not just “I got an error”.
- With coding problems, if possible post a “minimal” sketch that demonstrates the problem – not hundreds of lines of code.
- If you have debugging information in your sketch, post your debugging displays.
- Describe what you expected to happen, and what actually happened. Not just “it doesn’t work”.
- If possible, describe what you are really trying to do, not what you think might work. For example “I am trying to turn on an aquarium heater and pump at the same time”, not “how do I break out of an interrupt?”.
- Don’t double-post (cross-post). Your question will be noticed. If you post it in multiple places you will just annoy people who might otherwise have answered.
If you have puzzling behaviour, it is frequently helpful to put “debugging prints” inside your code. For example, initialize the serial port inside the setup function:
void setup ()
Serial.begin (115200); // initialize serial comms at 115200 baud
// ... other setup here
} // end of setup
Later on you can print things of interest, eg.
Serial.println ("Button 5 pressed.");
(Check you have set your Serial Monitor to 115200 baud, or you may see gibberish characters).
8. Memory usage
Microcontrollers like the Arduino have much less memory than you would be used to on a Mac, Windows or Linux. For example the Atmega328 processor (used on the Uno, Fio, Nano, Duemilanove boards, and others) has 32 Kb of Program Memory (for your code), 2 Kb of SRAM* (for your variables), and 1 Kb of EEPROM (for saving data when powered off).
Typically programs that reset unexpectedly, or hang, have run out of SRAM. Note that the number reported by the compiler when you compile your program is program memory, not SRAM.
Even a simple array like the one below uses 2000 bytes of memory. This line alone would almost certainly make your program crash:
int myData  ;
Hint: The String class tends to gobble up memory. Try to avoid using that, especially in larger programs.
* SRAM = Static Random Access Memory
Warning: In versions of the Arduino IDE up to 1.0.1 (at least) there is a bug in dynamic memory allocation. This affects both malloc/free and new/delete. In particular it also affects the String class, which uses dynamic memory allocation. This is discussed in this forum thread. A work-around is provided in this thread (a replacement malloc.c file) until the inbuilt libraries are updated.
Hint: Static strings can quickly gobble up SRAM. A simple technique to avoid that is to use the F( ) macro, as shown here:
Serial.println ("Welcome to my program!");
Serial.println (F("Welcome to my program!"));
9. Reading serial data
Serial data arrives asynchronously, that is, a byte at a time at no particular rate between bytes. To read things longer than a byte it is helpful to collect incoming data into a suitable size buffer, and when all has arrived, process that. Typically you know it has all arrived when a special character, such a newline character, is received.
10. Try things!
You will get more help if you try something and describe what happened, rather than posting “I wonder if X will work”.
11. Traps, tips, style guide and more reading.
On the page below are various frequently reported programming traps, some tips for enhancing your coding, a brief style guide, and links for further reading:
12. Issues with the IDE pre-processor
Sometimes “valid C” (or “valid C++”) will not compile properly due to the way that the IDE pre-processor converts your “sketch” file into a C++ file. This is explained in detail here:
13. Standard libraries (libc)
The Arduino IDE includes (“links”) libc – a standard library of common building-blocks that C programs often need. This includes things such as type conversions and string manipulations.
The documentation is here: http://www.nongnu.org/avr-libc/user-manual/modules.html
C/C++ programmers should make themselves familiar with what kinds of things are already provided in this standard library. In particular, browse through the functions in stdlib.h and string.h and the types in stdint.h .
1. Added a paragraph about memory usage.
2. Added a line about making forum subject headings descriptive.
3. Added a line about describing or posting your circuit.
4. Re-organized the sequence of paragraphs.
5. Added note to make sure you have your serial port set to 115200 baud.
6. Added note about not cross-posting.
7. Added note about posting a “minimal” sketch.
8. Numbered sections.
9. Added warning about bug in dynamic memory allocation.
10. Added note in point 1 about the compiler being “real” C++.
11. Added link to “traps and tips” page.
12. Added link to “How to avoid the quirks of the IDE sketch file pre-preprocessing”
13. Added mention of the “libc” standard libraries.