Reference Arduino In .cpp and .h Source Code

by Visual Micro 1. June 2012 12:00

When C++ files are used in an Arduino project the Arduino core is not automatically available to the source code, as it is with .pde/ino files. This document describes how to include the Arduino core manually in c++ files.

The following code example is how to include the correct Arduino core header and also to provide access to Arduino Serial functions from mycode.cpp and mycode.h files:-

mycode.c or mycode.cpp:-

#if defined(ARDUINO) && (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "HardwareSerial.h"
#include "myc.h"

mycode.h:-

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "HardwareSerial.h"
#include "myc.h"

Tip: All arduino versions 1.0 and above have a version >= 100. Example: Arduino 1.0.1 has a version of 101.

Tags:

General

Other Pages