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:

#include "WProgram.h"

If you get errors try locating such places and changing that line to:

#include "Arduino.h"

* IDE = Integrated Development Environment


3. Libraries

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:


5. Examples

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.

7. Debugging

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 [20] [50];

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:

Instead of:

Serial.println ("Welcome to my program!");

Use:

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!

Read this:

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: avr-libc: Modules

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 .


(edits)

  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.
8 Likes

I like it, but there are only two stickies on the whole forum - both in the Website forum. I don't expect that this will make sticky status.

Another one of mine got stickied (not by me):

http://arduino.cc/forum/index.php/topic,84190.0.html

I'm trying to save everyone some time by helping people out to help themselves here.

1 Like

+1
think is useful.

In "Getting Help on the Forum" I would make something like this the first point:

  • Use a descriptive Subject for your post. A Subject like "Noob needs help" or "Guidance needed" will not induce people to read your message.

I, for one, tend to ignore any post which doesn't have some useful info in the Subject or which is ALL IN CAPS or which has the word "urgent" in it.

Pete

Modified to follow your suggestion, thanks.

You could include a link to the venerable "How to Ask Questions the Smart Way" at How To Ask Questions The Smart Way

My thought it that it may be helpful to organize it a little better, such that someone completely clueless to programming wouldn't have to wade through "Serial data arrives asynchronously" to get to "Post your code!" Put the important and easily understandable things at the top.

I don't think the problem is lack of available things to read. It's the fact that a lot of people don't want to read at all. Maybe we need an Arduino picture book...you know, bright colors, shapes, connect the dots....

(channeling a little Grumpy Mike today)

1 Like

macegr:
(channeling a little Grumpy Mike today)

Indeed.

I don't think the problem is lack of available things to read.

I agree.

It's the fact that a lot of people don't want to read at all.

That is a problem but my impression is that most folks new to Arduino have a difficult time knowing how or where to get the information they need. They come to the forum because they suffer from 3rd Order Ignorance.

For example, I can distinctly remember when I started that it was a few weeks before I realized there was a "Playground" and a few months before I realized what the "Playground" was. I assumed, with the title "Playground", it was a place for advanced users to exchange project details so I simply didn't bother to visit.

Maybe we need an Arduino picture book...you know, bright colors, shapes, connect the dots....

XD

macegr:
I don't think the problem is lack of available things to read. It's the fact that a lot of people don't want to read at all.

Yes, sure. But my experience with other forums is that if there is a sticky at the very top where I am about to post "der, how do I get X to work?" and it runs through a lot of the preliminaries, then there is a chance I will read it.

My thought it that it may be helpful to organize it a little better ...

I can work on that bit.

Thumbs up from me.

If you are going to suggest that people use 115200 baud for serial comms, it might be worth mentioning that they will need to set the serial monitor to match. It's not necessarily obvious for beginners, and could save on 'My serial output is just weird characters' problems.

1 Like

I think this was very useful, 2 questions answered (of mine) - I was wondering about char arrays and sram, since most boards (the cheaper ones not the mega boards) only have 2k of sram, this post would save someone a LOT of time lol.... cheers - i hope it stays sticky.

Yeah I hope so too ... but I am just a Pawn in the Game of Life ...

Excellent!

The "what have you tried" link alone doubles the usefulness of the post. :smiley:

When posting code, rather than posting a complex sketch, aim to post the simplest sketch that shows the problem. As a courtesy to the people trying to help you, make sure the sketch actually compiles and demonstrates the problem before you post it, and format the code.

Nice post Nick! My suggestion: Add number to each bold title so I can tell a noob to say read number 6 on the sticky thread about "getting help" and follow the "posting code" suggestion. Or simply issue a 6-5.

PeterH:
When posting code, rather than posting a complex sketch, aim to post the simplest sketch that shows the problem.

Added this suggestion to the "how to post" list.

liudr:
My suggestion: Add number to each bold title so I can tell a noob to say read number 6 on the sticky thread about "getting help" and follow the "posting code" suggestion.

Now numbered each section.

Jeremy Blum's tutorial series on Youtube is the single most helpful resource for anyone just getting started using the Arduino.

I watched all 14 videos twice before I even received my Arduino in the mail. I think everyone should be required to watch those videos before they post a thing:

2 Likes

Here's my contribution, a collection of common beginner trouble spots: 8 Common Programming Mistakes - Cprogramming.com

1 Like