Syntax

Syntax is like the punctuation of code. There aren't too many rules, but getting something very slightly incorrect will result in a big headache when you're trying to write a program. A good idea when you are writing a large program is to press the Compile button when you finish up a section of the code. If you get a syntax error, you can hunt down the problem before it's buried in code. Here we cover the core syntax of the Arduino language. Besides void setup( ) and void loop( ), the rest applies to C++ as well.


void setup( ){ }

Void setup is technically a function that you create at the top of each program. Inside the curly brackets is the code that you want to run one time as soon as the program starts running. You set things like pinMode in this section.

 

void loop( ){ }

The loop is another function that Arduino uses as a part of its structure. The code inside the loop function runs over and over as long as the Maker Board is turned on.

 

; (semicolon)

The end of a command or statement. The compiler doesn't look for spaces in your code, it looks for semicolons

 

{ } curly braces

A group of code statements. You always need a closing curly brace to match each opening curly brace.

 

( ) parentheses

A group of arguments for a function, a method, or a code statement. You can also use these to group math within equations like: random( (10-7), (500 -100)); so that the math will result in: random(3,400);

 

//single line comment

When you type two forward slashes, the code from that point until the end of that line is ignored by the compiler. Use comments to explain what your code does, help you remember how the hardware is hooked up, or to remove some code from the program without deleting it.

 

/* multi-line comment */

A multi-line comment starts with the /* and doesn't end until the */ characters. When you have a lot to type, use a multi-line comment to mmake it easier to group the infornation together.

 

#define button 2

the #define command is used for a 'find and replace within your code. It is similar to creating a variable to replace a number, but it doesn't take up any memory. The downside of the #define is that, using the example above, a variable called buttonPress becomes 2Press when the code is compiled.

 

#include <library.h> or #include “library.h”

In the functions section of this text, we spoke about many of the built-in Arduino functions and how useful they can be to simplify your code and make hardware easier to interface with. Libraries are files that you can include in your code to use even more methods and functions. Anyone can write a library, so there are thousands of Arduino libraries available to you.

 

For example, Servo.h is automatically included with the Arduino software when you download it. It includes methods to use your Maker Board pins for driving motors. Opening File-→ Examples-→ Servo-→Sweep will open a program that shows you how Servo.h uses methods to control motors. The best libraries are those that clearly explain what the author wants to accomplish with the library and how you can use it for your code.

go to top