Arduino Language

Arduino Language

The Arduino language uses C/C++ syntax and includes a set of C/C++ methods which can be called from your sketch code. Details of these methods and other aspects of the language can be found here: Arduino Language Reference.

Additional libraries can also be installed as required. Information about Arduino libraries and how to install them can be found on the following page: Arduino Libraries Guide

For general reference on C/C++ syntax and language see:
C/C++ Syntax & Language Reference on cprogramming.com

Structure

A typical Arduino sketch consists of three main sections:

  • Declarations of global constants, variables and objects: Used to define any global values, variables, or objects which are used throughout the sketch.
  • The setup() method: This method is called once when the sketch is started (whenever the device is powered on or reset). It is used for setup and initialization.
  • The loop() method: This method contains the core functionality of the sketch. It is called consecutively while the sketch is running.

Example Sketch

//Global constants, variables and objects
#define START_MSG "Running Setup()..."
#define LOOP_DELAY 500
int loopCount = 0;

//Initialization
void setup() 
{
      Serial.begin(9600);
      Serial.println(START_MSG);
}
//Execution loop
void loop() 
{
      loopCount++;
      Serial.println("Loop() has run " + String(loopCount) + " times");
      delay(LOOP_DELAY);
}

Sketch Code Breakdown

Globals

Here two constants are defined (using the preprocessor directive #define). One of them defines the message which will be displayed at startup and the other defines the effective frequency that the loop() method will run at. Defining constants in this way allows for easy modification of these values. This is particularly true in cases where the constants are referenced multiple times throughout a sketch. We also declare a global variable,loopCount, of type int and initialize it to 0.

//Global constants, variables and objects
#define START_MSG "Running Setup()..."
#define LOOP_DELAY 500
int loopCount = 0;

setup()

Here we first open the serial connection using the method Serial.begin(). We then send a message using the Serial.println() method, passing it the string constant START_MSG we defined above.

//Initialization
void setup() 
{
      Serial.begin(9600);
      Serial.println(START_MSG);
}

loop()

Here we increment the value stored in loopCount using the ++ operator. We then display a message which shows how many times the loop() method has been called so far. Finally we call the delay() method which only returns after the specified number of milliseconds have elapsed. This effectively controls the execution frequency of the loop() method.

//Execution loop
void loop() 
{
      loopCount++;
      Serial.println("Loop() has run " + String(loopCount) + " times");
      delay(LOOP_DELAY);
}

If you load the sketch onto the SODAQ Mbili board and then open the Serial Monitor you should see output that looks similar to this: