Difference between revisions of "Arduino Coding Guide"

From EG1003 Lab Manual
Jump to: navigation, search
Line 74: Line 74:
These are some third party resources and tutorials with additional information on working with Arduino:
These are some third party resources and tutorials with additional information on working with Arduino:
* [https://docs.google.com/spreadsheets/d/1X-Sj3W7rKKYeUzUMY5eqmQMuMU6Gh1op5HMCAlGhO-I/edit?ts=5d76dbee#gid=682782030 Technical Online Library]
* [https://docs.google.com/spreadsheets/d/1X-Sj3W7rKKYeUzUMY5eqmQMuMU6Gh1op5HMCAlGhO-I/edit?ts=5d76dbee#gid=682782030 Technical Online Library]
* [https://www.arduino.cc/reference/en/]  
* [https://www.arduino.cc/reference/en/| Arduino Language Reference]  
* [https://www.arduino.cc/en/Tutorial/Variables]
* [https://www.arduino.cc/en/Tutorial/Variables| Arduino Variables Tutorial]
* [https://www.arduino.cc/en/Guide/Troubleshooting]
* [https://www.arduino.cc/en/Guide/Troubleshooting| Arduino Troubleshooting]
* [https://www.tutorialspoint.com/arduino/index.htm]
* [https://www.tutorialspoint.com/arduino/index.htm| TutorialsPoint Arduino Guide]

Revision as of 07:42, 6 February 2020

Arduino Coding Guide

Arduino Programming

The Arduino programming language is based on C/C++, but it is designed to be simpler and easier to learn. The most intuitive way to think about programming is like building with LEGO blocks: certain rules must be followed and different building blocks can be used to build bigger parts.

General Syntax

  • Every line must either end with a semicolon ‘;’ unless it’s a conditional, loop, or function
  • Conditionals, loops, and functions are enclosed in curly braces {}
  • Comments start with a //
    • Comments are text that the program ignores
    • Used to label and explain code


Datatypes are the different kinds of data values that can be used, manipulated and stored using C++. The table below includes the most basic and widely used datatypes.


Operators perform operations on variables and constants. The results of these operations are usually stored in a variable. The table below includes common operators.

Constants and Variables

Constants and variables hold data according to their datatype. They need to be given a name so that they can be referred to later. Constants hold data that will NOT change while a program is running. Constants usually contain pin numbers or sensor threshold values. Variables contain data that WILL change while a program is running. Variables usually contain sensor values and other values that need to have mathematical operations done on them. Below is an example of how to create different constants and variables.

Insert picture

Conditional Statements

Conditional statements run code enclosed by their curly brackets when a condition is met.

Insert picture


Loops run the code enclosed by their curly brackets a specific amount of times or until a condition is met. While-loop While-loops are used to perform a task until a condition is met For-loop For-loops are used when you want something to run a specific number of times. Although they seem complicated at first, the structure of most for-loops is the same. In the parentheses, the first part sets a variable (usually ‘i’ for ‘index’) to a value used to begin a count, the middle is the condition when the loop stops, and the third part is where you increment or decrement the counting variable.

Insert Picture


Functions are predefined sections of code that can be called multiple times to perform a defined task. These are typically used when a programmer needs to perform the same task multiple times. Figure 4 shows the anatomy of a function.

Insert Picture

The header portion is the function declaration. This tells the code what the function will return (return type), what the name of the function is (function name), and any parameters needed.The return type can be any valid Arduino C data type. If the function will not return any values, the return type should be void. Parameters are optional when creating a function.

For example, the following function is called addOne. This function takes in an integer, and returns that integer value plus one.


An example use of this function is below. After calling the function addOne(4), the value of the integer number would be 5.


Commonly Used Arduino Functions

These functions are included with Arduino C to be used with the Arduino board.


Arduino Libraries

Arduino Libraries are commonly used for robot movement and can be helpful with various sensors. Libraries are files that are already written in order to provide your sketch with extra functionality through various functions.

To use an existing library in a sketch simply go to the Sketch menu, choose "Import Library", and pick from the libraries available. This will insert an #include statement at the top of the sketch for each header (.h) file in the library's folder. These statements make the public functions and constants defined by the library available to your sketch. They also signal the Arduino environment to link that library's code with your sketch when it is compiled or uploaded.

To install a third party library download the libraries zip file to the Arduino folder of your computer. Extract the library to its own folder. Arduino must be restarted in order to use the library.

Additional Resources

These are some third party resources and tutorials with additional information on working with Arduino: