3

(coco) #1

Arduino Programming: Variables, constants, and more


SCHOOL OF MAKING


sketch. Constants defined within any function are
local constants and can only be accessed within
the function.
As with variables, to use a constant in a sketch,
simply refer to the constant any place where
you want to access the constant’s value. For
example, to use the pi constant in a circumference
calculation, you would use the following:
const float pi = 3.14;
float circumference;
float radius;

radius = 2.52;
circumference = 2 * pi * radius;

Constants are used primarily to increase code
readability and maintainability. If your sketch
measures the radius of
a circle, then calculates
the circle’s area and
circumference, you
could insert the value
of 3.14 into each of the
formulas. What happens
then if we want to
use a different value
for pi (perhaps including more decimal places)?
You’d have to change the value for pi in each of the
expressions that references it. By storing the value
in a constant, you’d only have to change its value in
one place to effect all of the expressions that use
it. Additionally, from a readability standpoint, seeing
pi in your code is much clearer in meaning to the
reader than 3.14.
When you try to change the value assigned to
a constant in a sketch, the Arduino compiler will
complain, displaying the error message shown
in Figure 2. You will not be able to deploy this
sketch to the Arduino device until you’ve resolved
this error.

MEMORY MANAGEMENT
Arduino devices are inexpensive programmable
microcontrollers, and in order to meet its imposed
cost limitation, the devices have limited memory

in separate functions; however, doing this is likely
to lead to an intense headache if you have to debug
this code. Figure 1 shows the compiler error you’ll
see when you violate scope.

CONSTANTS
In C, constants define storage locations for data
that cannot be modified; the data is referenced by
a sketch, but never changed. They’re typically used
to define values used in calculations or string values
used repeatedly by a sketch. Constant values are
stored in memory and can be easily accessed by
any part of your sketch.
Constants are defined
the same way you
define variables, except
the constant definition
always starts with the
const keyword and you
must assign a value to
the constant. To define a
constant in a sketch, use the following expression:
const DATA_TYPE CONSTANT_NAME = VALUE;

Constants are referenced by name, so
CONSTANT_NAME defines the name associated with
the constant. VALUE defines the data value stored
in the constant. Here are some example constant
definitions, creating (in order) an integer constant,
floating-point constant (decimal value), and a string
constant (a character array) :
const int myConstValue = 3;
const float pi = 3.14;
const char myString = “My string array”;

Constants have the same scope options as
variables. Constants defined at the beginning of
a sketch, before the sketch’s setup() and loop()
functions, have a global scope which means
they can be accessed from anywhere within the

If you learn to program on
devices with limited RAM,
it will set up up with good
habits for the rest of your
coding career

QUICK TIP


Constant values are stored
in memory and can be
easily accessed by any part
of your sketch



Good variable names are essential for making your code
easy to read. Think about them at the start as they’re
awkward to change later

QUICK TIP


Figure 3
Keep an eye on the
size of the sketch as
you’re developing
it to make sure you
don’t suddenly run
out of space on your
chosen hardware
Free download pdf