Skip to main content

Reply to "Arduino Uno Question"

This is a pretty good article on the Arduino preprocessor. It explains that the first thing that happens to your program when you build it is to run it through the preprocessor. This is a process that handles all the lines in your program that begin with the pound sign (#) character; like #define and #include. These are known as preprocessor directives.

The Arduino preprocessor is a hold-over from the days when the C programming language was first developed at Bell Labs decades ago. It is in fact still referred to as the C preprocessor by it's current supporters; the GNU Software Foundation. This version is widely used by C/C++ developers throughout the world. The official technical documentation can be found here.

The preprocessor is a kind of substitution machine. It takes your original program, substitutes text according to the instructions and definitions laid out by the preprocessor directives and spits out a new version of your program.

For example. The #define directive takes an identifier and a substitution string:

#define LIMIT_SWITCH_PIN 4

Here the identifier is "LIMIT_SWITCH_PIN" and the substitution string is "4". Following this definition, anywhere in your program where the identifier appears, the text string will replace it.

int limitReading = digitalRead(LIMIT_SWITCH_PIN);
becomes
int limitReading = digitalRead(4);

Note that preprocessor directives are not part of the programming language and do not end with a semi-colon.

There is more to #define than simple substitution but you can look into that yourself if you're so inclined. The other directive that is often used in Arduino programs is #include. That's how you tell the build process that you want to use a library:

#include <Servo.h>

The preprocessor will look for the file "Servo.h" in the usual place that libraries are located and pull the text from that file into your program. Again, no semi-colon. The new version of your program spit out by the preprocessor will now have a copy of that file placed where the #include directive appears.

There are other tricks to using the preprocessor that are useful in a different context. But for now, that's all that needs to be said.

OGR Publishing, Inc., 1310 Eastside Centre Ct, Suite 6, Mountain Home, AR 72653
800-980-OGRR (6477)
www.ogaugerr.com

×
×
×
×
×