Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
You want to have different versions of your code that can be selectively compiled. For example, you may need code to work differently when debugging or when running with different boards.
You can use the conditional statements aimed at the preprocessor to control how your sketch is built.
This example from sketches in Chapter 15 includes the SPI.h library file that is only available for and needed with Arduino versions released after 0018:
#if ARDUINO > 18 #include <SPI.h> // needed for Arduino versions later than 0018 #endif
This example, using the sketch from Recipe 5.6,
displays some debug statements only if DEBUG is defined:
/*
Pot_Debug sketch
blink an LED at a rate set by the position of a potentiometer
Uses Serial port for debug if DEBUG is defined
*/
const int potPin = 0; // select the input pin for the potentiometer
const int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
#define DEBUG
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin); // read the voltage on the pot
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // blink rate set by pot value
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // turn L....