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.

Advertisements

~ by ratdad on March 20, 2011.

One Response to “The include directive”

  1. The ####### Arduino method of handling #includes – I’ve spent a good deal of time trying to get my head round it today.

    I’ve made a class, SimServo, which is a wrapper around the standard Arduino Servo.h library to link very easily with the X-Plane flight simulator. SimServo lives in SimServo.h, and I try to #include from there. No joy, Arduino states that my Servo member variable is not a type. With #include “Servo.h”, it doesn’t find the file. Putting the #include into the main .ino file works, but burdens the user with something extra to avoid forgetting. I’m becoming exasperated with the Arduino IDE!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: