The include directive
Electronic project-wise, I’m on a round-robin of nine different projects at home, and then there’s work too, where I’m managing another 14. Fortunately, many of these use the Arduino, so there is much commonality across these many projects. For one project in particular, I want to play sound through a speaker attached to an Arduino.
The Arduino’s open source nature means that often, someone has already done something similar and their project is available either to learn from, or use as a starting point or foundation for my own project. This is a great asset that makes the Arduino infinitely useable. My Arduino projects progress, sometimes slowly, but they seldom stop, because there are so many resources available on line to help find an answer to a problem. This wealth of information ensures that my time spent in the Arduino world is satisfying.
When looking into playing sounds the Arduino, I found this project in the Arduino Playground. It promises to be a simple demonstration of the Arduino playing digitized sound on pin 11. While the idea of playing sound on the Arduino will make an interesting blog post soon, today I want to focus in on a small topic about the Arduino Language Reference.
To run Michael Smith’s project, I had to cut and paste the source code from the Arduino Playground into two files. The main file is speaker_pcm.pde and it calls the sound data from a header file called sounddata.h. My limited C language knowledge tells me that the use of a header file to hold data is a bit unconventional, but I clearly understand the author’s intent.
I made my source files, put them in a sketch directory and compiled with Arduino-0021. Notice the include statement for the sounddata header file: #include "sounddata.h". Now notice the header files for the other stuff: #include <stdint.h> #include <avr/interrupt.h> #include <avr/io.h> #include <avr/pgmspace.h>
May I direct your attention to the methods used to “bracket” the various header files? The standard header files for features included in the Arduino-0021 installation are bracketed by the <> characters and the header file that I created for my project is bracketed by two ” characters. You may be curious about this. I did a lot of C programming back in the stone age, so this was familiar to me, but I want to review the preprocessor rules.
First off, the include statement is not part of the Arduino programming language. It is a preprocessor directive. The preprocessor is a mechanism that makes source code more manageable. You can read about this here. Also, header files typically are no used to assign values to variables. Wikipedia says it politely “Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers.” Lastly, the bracketing around the header file name directs the preprocessor to look for the header file in the system directories when the <> brackets are use and look in the current source file directory when the “” brackets are used, explained here.
Unfortunately the Arduino reference is not clear on this topic.