Basic Overview

  • 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: https://www.arduino.cc/en/Reference/HomePage

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

https://www.arduino.cc/en/Guide/Libraries

For general reference on C/C++ syntax and language see:

http://www.cprogramming.com/reference/

  • 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 when reset button is pushed ).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.
//Global constants, variables and objects 
#define START_MSG "Running Setup()..."
#define  LOOP_DELAY 500
int loopCount = 0;

// Initialization Method, run just once when the device is powered on or reset button is pushed 
void setup() {
SerialUSB.begin(9600);
SerialUSB.println(START_MSG);
}
//  Execution loop , This method contains the core functionality of the sketch and runs in a loop
void loop() {
loopCount++;
SerialUSB.println("Loop() has run " + String(loopCount) + " times");
delay(LOOP_DELAY);
}
  • Global constants, variables and objects

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 SerialUSB.begin().We then send a message to the serial monitor using the SerialUSB.println() method, passing it the string constant START_MSG as we defined above.

//Initialization
void setup()
{  
SerialUSB.begin(9600);    
SerialUSB.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++;
SerialUSB.println("Loop() has run " + String(loopCount) + " times");  
delay(LOOP_DELAY);
}
  • Serial Monitor

If you load the sketch on Autonomo board and then open the Serial Monitor (Shift+M) you should see output that looks similar to this:

SerialMon

We can use PUTTY software, to view Serial Monitor too, (Choose Serial and Type the COM corresponding to your Autonomo )

SerialMon

SerialMon

Key Difference between Autonomo and Arduino Serial Monitor
  • We use SerialUSB instead of Serial for communication with the serial monitor (or any other terminal emulation software as PUTTY for example).
  • Re-opening the serial monitor doesn’t reset board. However if you reset the board from the reset button on the board, you must re-open the serial monitor.
  • When the Autonomo is running on a battery, plugging in the USB cable will not reset it.
  • Once the sketch is uploaded it will start executing. This means you may miss any initial messages sent to the serial monitor. We can avoid this using wait function until the serial monitor is opened.
  • While loop function for the Serial Monitor

You can make your sketch wait for the serial monitor to be opened by adding the following code to the start of your setup() method:

void setup()
{
 // Wait for SerialUSB to open
while (!SerialUSB);
SerialUSB.begin(9600);
SerialUSB.println("Serial Monitor is opened");
}

However, this while loop in setup method has a drawback that it will stop the sketch from proceeding unless the SerialUSB connection is opened, which will not be practical if we are using an external battery to power Autonomo. Modifying the while loop function by adding a timeout will be the idle way to handle that situation.

void setup()
{
 // Code will stop in the while loop until Serial monitor is opened or the 10 seconds is over   
while ((!SerialUSB) && (millis() < 10000)); 
SerialUSB.begin(9600);
SerialUSB.println("Serial Monitor is opened");
}

Now we can see the loop counting from the beginning on our previous code.

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

// Initialization Method, run just once when the device is powered on or reset button is pushed
void setup() {
// Wait for SerialUSB to open
while (!SerialUSB);
SerialUSB.begin(9600);  
SerialUSB.println(START_MSG);
}

//  Execution loop , This method contains the core functionality of the sketch and runs in a loop
void loop() {  
loopCount++;  
SerialUSB.println("Loop() has run " + String(loopCount) + " times"); 
delay(LOOP_DELAY);
}