Go Down

Topic: Read this before posting a programming question ... (Read 76691 times) previous topic - next topic

Nick Gammon

Mar 19, 2012, 11:41 pm Last Edit: Oct 12, 2014, 10:25 pm by Nick Gammon Reason: 1
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".

Although the Arduino main page states that "the microcontroller on the board is programmed using the Arduino programming language (based on Wiring)" this should not be taken to mean that the Arduino is programmed in some sort of cut-down or "similar to C" variant.

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.




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:

Code: [Select]
#include "WProgram.h"

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

Code: [Select]
#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:

http://arduino.cc/it/Reference/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:

http://arduino.cc/en/Reference/HomePage




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:

Code: [Select]
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.

Code: [Select]
 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:

Code: [Select]
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:

Code: [Select]

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


Use:

Code: [Select]

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:  

http://mattgemmell.com/2008/12/08/what-have-you-tried/

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:

http://www.gammon.com.au/tips




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:

http://www.gammon.com.au/forum/?id=12625




(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"
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

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.

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

fkeel

http://embodimentlabs.tumblr.com/
http://paulstrohmeier.info/

el_supremo

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

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Morris Dovey

There's always a better way!

WizenedEE

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.

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. Maybe we need an Arduino picture book...you know, bright colors, shapes, connect the dots....

(channeling a little Grumpy Mike today)
Unique RGB LED Modules and Arduino shields: http://www.macetech.com/store

Coding Badly

(channeling a little Grumpy Mike today)


Indeed.

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


I agree.

Quote
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.

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


XD

Nick Gammon


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.

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


I can work on that bit.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

dxw00d

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.

cjdelphi

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.

Nick Gammon

Yeah I hope so too ... but I am just a Pawn in the Game of Life ...
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

mromani

Excellent!

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

Go Up