Programming Fundamentals
Arduino sketches are written in the C language with extensions from C++. We will only describe the most common and immediately useful features of C for two reasons. First, we only need to use a handful of the basic features of the language. Second, learning C as an end goal takes lots of time and practice. Recognizing these two aspects, we can fairly quickly learn enough about a subset of C to write sketches to perform fun and useful tasks.
Table of contents
Variables, operations and assignments
As you build your C/C++ programming skill, you will need to learn about variable types, operations and assignments.
Our immediate goal is to introduce these ideas so that you can recognize that terms like “variable types”, “operations” and “assignments”, have precise meanings that can affect how your program runs. As you learn, you will revisit these concepts and gain understanding how they are both simple and at times subtle.
A variable is a symbol associated with a chunk of computer memory in the running program. Think of a variable as a box that can hold different types of information such as integers, fractions, letters or logical values (True
or False
). Each variable in an Arduino sketch must be declared and in the declaration the variable is given a type. Continue reading here for more information on variable types and declaring variables.
An operation is an action like addition, subtraction, multiplication or comparison that involves one or more variables. Here are two examples of operations
a + b
2.0 * sqrt(c)
These operations have the common sense interpretation of performing mathematical calculations. Assuming that a
, b
and c
are variables that store numerical values (an assumption that may or may not be true!), the result of those operations is another numerical value. Note that *
is the multiplication operator and sqrt
is a builtin function that computes the square root of a number. Therefore, 2.0 * sqrt(c)
performs the mathematical operation, “two times the square root of c
”.
Continue reading here for more information on operations with variables.
An assignment stores the result of an operation (or set of operations) in a variable. Extending the preceding example of mathematical operations, a program could contain these two assignments.
c = a + b
d = 2.0 * sqrt(c)
The results follow our common sense. The value stored in d
is the sum of a
and b
. The value store in d
is the result of multiplying the square root of the number stored in c
by 2.0. However, even these straightforward assignments can have subtle effects if a
, b
, c
and d
are variables with different types.
Continue reading here for more information on assigning values to variables.
If this is your first exposure to Arduino programming, don’t worry too much just now about variable types, operations and assignments. These issues will matter and you will need to revisit them as your programs increase in complexity.
setup
and loop
Variables, operations and assignments are essential building blocks of computer programs in any language. To program an Arduino, we also have to supply another key ingredient called a sketch.
A sketch is the C/C++ statements that you provide to give the Arduino program its unique set of tasks. The Arduino IDE combines your sketch with other components (think boilerplate) to create the instructions that are uploaded to the Arduino board.
All Arduino sketches must have a setup
function and a loop
function. The setup
function is executed once when the Arduino board is first powered-up or when the reset button is pressed. The loop
function is repeated indefinitely after the first and only execution of the setup
function.
Programming Structures
There are a few basic programming structures that help us write more compact, efficient programs that are easier to write and maintain. These are
- Loops
if
constructs (a.k.a. branching)- Functions
These structures are described on another set of web pages on this site.