This is a guide for writing clear Arduino examples that can be read by beginners and advanced users alike. You don't have to code this way, but it helps if you want your code to be clear to all levels of users. This is not a set of hard and fast rules, it's a set of guidelines. Some of these guidelines might even conflict with each other. Use your judgment on when they're best followed, and if you're not sure, ask someone who'll be learning from what you write what makes the most sense. You might also be interested in the Arduino Style Guide for Creating Libraries.
If you want to contribute with content for the Arduino Documentation website, please find instructions in the contribution-templates folder in the Arduino Documentation repository.
Most of this is borrowed from various editors over the years, and here is a list of guidelines that you can follow when writing code.
Efficiency is not paramount; readability is.
The most important users of Arduino are beginners and people who don't care about code, but about getting projects done.
Think generously about people who know less than you about code. Don't think they should understand some technical concept. They don't, and they're not stupid for not understanding. Your code should explain itself, or use comments to do the same. If it needs a complex concept like registers or interrupts or pointers, either explain it or skip it.
When forced to choose between technically simple and technically efficient, choose the former.
Introduce concepts only when they are useful and try to minimize the number of new concepts you introduce in each example. For example, at the very beginning, you can explain simple functions with no variable types other than int, nor for consts to define pin numbers. On the other hand, in an intermediate example, you might want to introduce peripheral concepts as they become useful. Concepts like using const ints to define pin numbers, choosing bytes over ints when you don't need more than 0 - 255, etc. are useful, but not central to getting started. So use them sparingly, and explain them when they're new to your lesson plan.
Put your
setup()
and your loop()
at the beginning of the program. They help beginners to get an overview of the program, since all other functions are called from those two.Use verbose if statements. For simplicity to the beginning reader, use the block format for everything, i.e. avoid this:
1if (distance > 10) moveCloser();
Instead, use this:
1if (distance > 10) {2 moveCloser();3}
Avoid pointers
Avoid
#defines
val
or pin
. Be more descriptive, like buttonState
or switchPin
.If you want to define pin names and other quantities which won't change, use const ints. They're less messy than #defines, yet still give you a way to teach the difference between a variable and a constant.
Use the wiring/Processing-style variable types, e.g. boolean,char,byte,int,unsigned int,long,unsigned long,float,double,string,array,void when possible, rather than uint8_t, etc. The former are explained in the documentation, and less terse names.
Avoid numbering schemes that confuse the user, e.g.:
1pin1 = 22pin2 = 3
If you need to renumber pins, consider using an array, like this:
1int myPins[] = { 2, 7, 6, 5, 4, 3 };
This allows you to refer to the new pin numbers using the array elements, like this:
1digitalWrite(myPins[1], HIGH); // turns on pin 7
It also allows you to turn all the pins on or off in the sequence you want, like this:
1for (int thisPin = 0; thisPin < 6; thisPin++) {2 digitalWrite(myPins[thisPin], HIGH);3 delay(500);4 digitalWrite(myPins[thisPin], LOW);5 delay(500);6}
Here's a good title block:
1/*2 Sketch title3
4 Describe what it does in layman's terms. Refer to the components5 attached to the various pins.6
7 The circuit:8 * list the components attached to each input9 * list the components attached to each output10
11 Created day month year12 By author's name13 Modified day month year14 By author's name15
16 http://url/of/online/tutorial.cc17
18*/
For digital input switches, the default is to use a pulldown resistor on the switch rather than a pullup. That way, the logic of a switch's interaction makes sense to the non-engineer.
Keep your circuits simple. For example, bypass capacitors are handy, but most simple inputs will work without them. If a component is incidental, explain it later.