Skip to content

SALES INQUIRIES: 1 (888) 767-9864

Understanding Arduino Syntax

The coding language that Arduino uses is very much like C++ (“see plus plus”), which is a rather common language in the world of computing.  As I have alluded to in previous lessons, the code you learn to write for your Arduino will be very similar to code you write in any other computer language – all the basic concepts remain the same – it’s just a matter of learning a new dialect should you pursue other ventures.

The code you will be writing is called “human readable”, that is, it will make sense to you (sometimes) and will be organized for a human to follow.  Part of the job of the IDE is to take the human readable code and translate it into machine-readable code to be executed by the Arduino.  This process is called compiling.

The process of compiling is seamless to the user. All you have to do is press a button.  If you have some errors in your computer code, the compiler will display an error message at the bottom of the IDE and highlight the line of code that seems to be the issue.  The error message is meant to help you identify what you might have done wrong – sometimes they are very explicit, like saying, “Hey – you forget a semi-colon”, some times they are way out there.

Why would I be concerned with a semi colon you ask? A semi-colon is part of the Arduino languages syntax, the rules that govern how the code is written.  It is sort of like grammar when you think of writing.  Say for example we didn’t use periods when we wrote – every one would have a heck of a time trying to figure out when sentences ended and started.  Or if we didn’t employ the comma, how would we convey a dramatic pause to the reader?

And let me tell you, if you ever had an English teacher with an overactive red pen, the complier is 10 times worse.  In fact – your programs WILL NOT compile without perfect syntax.  This might drive you crazy at first because it is very natural to forget syntax, but as you program more you will to learn to be assiduous with coding grammar.

So lets get our hands dirty and introduce some sytax.

The semi-colon ;

A semi colon needs to follow every statement we write in Arduino.  For example

int LEDpin = 9;

In this statement, I am assigning a value to an integer variable (we will cover this later), notice at the end, the semi-colon.  This lets the compiler know that you have finished a chunk of code and are moving on to the next piece.

The double back slash for single line comments //

// When you type these all the text that follows on the same line will be greyed out

Comments are what you use to annotate your code.  Good code is commented well.  Comments are meant to inform you and anyone else who might stumble across your code, what the heck you were thinking when you wrote it.  A good comment would be something like this…

//This is the pin on the Arduino that the LED is plugged into

int LEDpin = 9;

Now, in 3 months when I review this program, I know where to stick my LED.

Comments will be ignored by the compiler – so you can write whatever you like in them.  If you have a lot you need to explain, you can also use a multi-line comment which looks like this…

/* The multi-line comment opens with a single backslash followed by an asterisk. Everything that follows is grayed out and will be ignored by the compiler, until you close the comment using first an asterisk and then a backslash like so */

Curly Braces { }

The curly braces are used to enclose information for certain functions. There is always an opening curly bracket and a closing curly bracket.  If you forget to close a curly bracket, your complier will not like it and throw an error code.

Functions

Lets switch gears a bit and talk about functions.

Functions are pieces of code that are used so often, that they are encapsulated in certain keywords, so that you can use them more easily.

For example, a function could be the following set of instructions…

Wash the Dog:

  1. Get a bucket
  2. Fill it with water
  3. Add soap
  4. Find dog
  5. Lather dog
  6. Wash dog
  7. Rinse dog
  8. Dry dog
  9. Put away bucket

This set of simple instructions could be encapsulated in a function that we call WashDog.  Every time we wanted to carry out all those instructions we would just type WashDog, and whalla!, all the instructions would be carried out.

In Arduino, there are certain functions that are used so often they have been built into the IDE.  When you type them, the name of the function will change to a color of orange.  The function pinMode(), for example, is a common function used to designate the mode of an Arduino pin.

So what’s the deal with the parentheses following the function pinMode()?  Well, we were a little incomplete with our explanation of a function.  Many functions require arguments to work.  An argument is information the function will use when it runs.

For our WashDog function, the arguments might be dog name and soap type, or temperature and size of bucket.

For the pinMode() function, the arguments are Pin Number and Mode.

pinMode(13, OUTPUT)

The argument 13 refers to Pin 13, and OUTPUT is the mode that you want the Pin to operate.  When you enter these arguments, the terminology is called passing.  You pass certain information to the functions.  Not all functions take arguments, but all have opening and closing parentheses regardless.

Do you notice that the word OUTPUT is blue?  There are certain keywords in Arduino that are used very often and having them blue helps you identify them.  The IDE will turn them blue automatically.

Now we won’t get into it here, but you can make your own functions in Arduino, and you can even get the IDE to color them for you.

We will however talk about the two functions used in nearly EVERY Arduino program.

Void Setup()

setup() as the name implies is used to set up the Arduino board.  The Arduino will execute all the code that is contained between the curly braces of void setup only once.  Typical things that happen in setup() are setting the modes of pins, starting serial communication, and other things that generally only need to take place once for most Arduino programs.

void setup() {

//the code in between the curly braces is only run once

}

You might be wondering what void means before the function setup().  Void means that the function does not return any information back from its instructions.  Some functions do return value’s –  our DogWash function might return CleanDog.  The function analogRead() returns an integer value between 0-1023.  If this seems a bit odd now, don’t worry as we will cover every common Arduino function in depth as we continue through the course.

So back to setup() – a couple things you should know.

  1. setup() only runs once
  2. setup() needs be the first function in your Arduino sketch
  3. must have opening and closing curly braces

Void loop()

You have to love the Arduino developers, because the function names are so telling.  As the name implies, all the code between the curly braces in loop() gets repeated over and over again – in a loop.  The loop() function is where the body of your program will reside.

As with setup(), the function loop() does not return any values and so the word void comes before it.

void loop() {

//whatever code you put here is written over and over 

}

It might seem odd to you that the body of the code would just repeat over and over – so lets just take our DogWash example, and instead of thinking of it as a function, lets try to write a sketch that would do the same thing.

//we will only do these things once

void setup() {

get Bucket;

get Water; //I may have to do this again if I run our of water

get Brush;

get Soap;

get Dog; //I may have to do this again later if the dog runs away

} /* this curly bracket closes the setup() function every thing in loop() runs over and over again */

void loop() {

dip Brush in Water;

scrub Dog;

/* check to see if any of the following conditions

are met every time we repeat the loop() function */

if the dog runs away, get Dog;

if the we run out of water, get Water;

if Dog is clean, end the program;

} /* this curly bracket closes loop() function, once the program gets here,

it starts over again at the beginning of loop() */

Well, this might look a little cryptic at first, and it will for a while, but as you continue to learn and sharpen you programming skills, all this will be second nature.

Try On Your Own:

This course is based around the example sketches provided with the Arduino IDE.  Open up your Arduino IDE and go to File > Example > 01.Basics and open up three different sketches.  Identify the following syntax and functions that you find in the sketches:

    • ; semi-colons
    • // single line comments
    • /* */ multi-line comments
    • { } open and clsoing curly braces
    • ( ) parenthesis
    • void setup() – identify the opening and closing curly braces
    •  void loop() –  identify the opening and closing curly braces
    • some blue keywords like OUTPUT or INPUT

Further Reading:

Go to the Arduino Reference page and check out all the syntax under Further Syntax that we talked about.  You will find some further useful info on each of these elements.