3

(coco) #1

Arduino Programming: Variables, constants, and more


SCHOOL OF MAKING


Arduino Programming:


Variables, constants, and more


o you want your Arduino sketches
to look like they were written
by a professional? Use variables
and constants to self-document
your code.
In this series so far, you’ve learned
how to wire up some simple circuits and write
some Arduino code to interact with the circuit.
In this article, you’ll put aside the hardware and
learn about some fundamentals of the Arduino’s
language: variables, constants, and more. The goal
is to make you a better programmer and help you
deliver code that’s easier to read and for others
to understand.
Development languages such as Arduino’s
language provide mechanisms for managing data:
the information (numbers, Boolean values, strings)
that your sketches use to do their work. These
are implemented through Variable and Constant
language elements you’ve seen used in our
previous articles. Arduino devices have a limited
amount of memory and processing capabilities, and
you’ll want to make the best use of both in your
projects. As you code your Arduino sketches, there
are things you, as a developer, can do to manage
the memory profile for a sketch, and make the
sketch more readable at the same time.

VARIABLES
In the Arduino language, a variable is a pointer to
a memory location that stores a particular piece of
data. Variables are used to store data used by the
sketch, but also to increase code readability and
maintainability. Variables enable you to refer to a
data value by name rather than its value. As your
sketch runs, your code will store and retrieve data
by referring to the variable in expressions. Unlike
some other languages, in the Arduino language you
can’t use a variable until you’ve defined it. So to
define a variable, use the following expression:

Dig deeper into the data capabilities of the Arduino


language and make your sketches easier to read and maintain


D


DATA_TYPE VARIABLE_NAME;

Notice the semicolon at the end of the expression


  • it’s required.
    In this example, DATA_TYPE defines the type of
    data that will be stored in the variable (supported
    types are array, bool, boolean, byte, char, double,
    float, int, long, short, string, unsigned char, unsigned
    int, unsigned long, word). The compiler allocates
    memory for the variable based on the data type
    you specify when you create the variable. The
    VARIABLE_NAME element of the expression defines
    the name you’ll use to refer to the variable in your
    code. A variable’s name can only have upper-
    case and lower-case letters, numbers, and the
    underscore character; using anything else will cause
    a compiler error.
    To create a variable called myInt that stores an
    integer value, use the following variable declaration:


John Wargo


@johnwargo

John is a professional
software developer,
writer, presenter,
father, husband, and
geek. He is currently a
Program Manager at
Microsoft, working on
Visual Studio Mobile
Center. You can find
him at johnwargo.com

Figure 1
The Arduino compiler will block some types of error before you
can upload your code the the board
Free download pdf