Advanced Arduino Programming - Appendixes - Arduino: A Quick-Start Guide, Second Edition (2015)

Arduino: A Quick-Start Guide, Second Edition (2015)

Part III. Appendixes

Appendix 2. Advanced Arduino Programming

In reality, the Arduino programming language is nothing but C++, but it has some restrictions, and it uses a special tool suite. In this appendix, you’ll learn what your options are. Also, you’ll find a short section showing how bit operators work, because you need them often when working with sensors and other devices.

The Arduino Programming Language

The first sketches you’ll write for an Arduino might seem to be written in a special Arduino language, but they aren’t. To program the Arduino, you usually use plain old C/C++. Unfortunately, the Arduino doesn’t understand C or C++ code, so you have to compile the code on your PC or Mac into machine code suitable for the Arduino’s microcontroller. This process is called cross-compiling, and it’s the usual way of creating executable software for microcontrollers. You edit and compile the software on your PC, and then you transfer the machine code to the microcontroller.

In case of the Arduino, these microcontrollers are often part of the AVR family produced by a company named Atmel. To make software development for Atmel microcontrollers as easy as possible, Atmel has developed a whole tool chain based on the GNU compiler tools. All tools work like the originals, but they have been optimized for generating code for the Atmel microcontrollers.

For nearly all GNU development tools, such as gcc, ld, or as, there’s an AVR variant: avr-gcc, avr-ld, and so on. You can find them in the hardware/tools/avr/bin directory of the Arduino IDE.

The IDE is mainly a graphical wrapper that helps you avoid using the command-line tools directly. Whenever you compile or upload a program using the IDE, it delegates all work to the AVR tools. As a serious software developer, you should turn on a more verbose output, so you can see all command-line tool invocations. Enable verbose output for both compilation and upload in the Preferences menu, as described in Changing Preferences. Then load your blinking LED sketch and compile it. (We did this back at the start of our journey in Changing Preferences).

The command invocations look weird at first because of the names of the many temporary files that are created. You should still be able to identify all compile and link steps necessary to build even a simple sketch like our blinking LED example. That’s the most important thing the Arduino team did: they hid all these nasty details well behind the IDE, so even people with no software development experience can program the Arduino. For programmers, it’s a good idea to work in verbose mode, because the best way to learn about all the AVR tools is to see them in action.

Upload the program to the Arduino now to see avrdude in action. This tool is responsible for loading code into the Arduino and can be used for programming many other devices, too. Interestingly, the AVR tools even make it possible to use the Arduino IDE for non-Arduino projects.

There’s another difference between Arduino programming and regular C++ code. When programming for the Arduino, you don’t define main yourself, because it is already defined in the libraries provided by the Arduino developers. As you might have guessed, it calls setup first and then runs the loop function in a loop. Since Arduino 1.0, it also calls serialEvent at the end of the loop function.

Further restrictions when programming C++ on AVR microcontrollers include the following:[144]

· You cannot use the Standard Template Library (STL) because it’s way too big for the small AVR microcontrollers.

· Exception handling isn’t supported. That’s why you see the -fno-exceptions switch often when the avr-gcc compiler is invoked.

· Dynamic memory management using new and delete isn’t supported.

In addition to all that, you should keep an eye on performance. C++ automatically creates a lot of functions (copy constructors, assignment operators, and so on) in the background that are rarely needed on the Arduino. Even with these restrictions, the Arduino supports a powerful subset of the C++ programming language. So there’s no excuse for sloppy coding!

Bit Operations

In embedded computing, you often have to manipulate bits. You sometimes have to read single bits to get some sensor data. In other cases, you have to set bits to turn a device into a certain status or to make it perform some action.

For bit manipulation, you need only a few operations. The simplest is the not operation that inverts a bit. It turns a 0 into a 1 and vice versa. Most programming languages implement the binary not operation with a ~ operator:

int x = 42; // In binary this is 101010

int y = ~x; // y == 010101

In addition, you’ll find three binary operations named AND, OR, and XOR (eXclusive OR). Most programming languages call the corresponding operators &, |, and ^, and their definitions are as follows:

a

b

a AND b
a & b

a OR b
a | b

a XOR b
a ^ b

0

0

0

0

0

1

0

0

1

1

0

1

0

1

1

1

1

1

1

0

With these operators, it’s possible to mask bits in a number, so you can extract certain bits. If you’re interested only in the lower two bits of a number, you can do it as follows:

int x = 42; // In binary this is 101010

int y = x & 0x03; // y == 2 == B10

You can also set one or more bits in a number using the OR operation. The following code sets the fifth bit in x regardless of whether this bit is 0 or 1.

int x = 42; // In binary this is 101010

int y = x | 0x10; // y == 58 == B111010

The bit shift operators « and » let you move bits to a certain position before you work with them. The first one moves bits to the left, and the second moves them to the right:

int x = 42; // In binary this is 101010

int y = x << 1; // y == 84 == B1010100

int z = x >> 2; // z == 10 == B1010

Shifting operations might seem intuitive, but you have to be careful when shifting signed values.[145] Although they look similar, binary operators aren’t the same as Boolean operators. Boolean operators such as && and || don’t operate on the bit level. They implement the rules of Boolean algebra.[146]

Beginners are often afraid of bit operations, but there’s no reason to fear them. Microcontrollers operate on a bit level, so you have to be able to make the bits obey your will. It takes some training, but it’s not rocket science.

Footnotes

[144]

http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_cplusplus

[145]

http://en.wikipedia.org/wiki/Arithmetic_shift

[146]

http://en.wikipedia.org/wiki/Boolean_algebra_%28logic%29