VARIABLES

This page is part of my Model Remodel series of articles and a subset of my Arduino section.

DISCLAIMER: If you choose to attempt any of these modifications, you assume all risks thereof. I just wanted to share my experiences here. Neither Fanhome, nor myself, are responsible for any damages that may occur.


Using Variables

Variables are used to temporarily store numeric or text values in your Arduino’s onboard memory. These values can then be used in our sketch for various purposes. They are temporary because when the Arduino is powered off, these values are erased.

Using variables in programming is almost guaranteed, so it will be very beneficial to be familiar with how they work. I like to think of variables as little labeled boxes of different sizes that each hold a piece of paper with something written on it, such as a number or a bit of text.

Any variable that we want to be able access throughout our sketch is called a global variable. These global variables must have names and initial (default) values that are declared (created) in advance. This means they need to be placed in our sketch before the setup() and loop() functions. In our sketch, we will declare a global variable named LEDPin to store our DIGITAL Pin value of 3. We can then use the name of this variable throughout the sketch to refer back to that value. Taking our first sketch, I modified it as shown:

/*
  A sketch that will light a LED and uses a Variable
*/
// Create a byte-sized variable named LEDPin and set its value to 3
byte LEDPin = 3;
void setup() {
  // Set DIGITAL Pin 3 as an output pin
  pinMode(LEDPin, OUTPUT);
}
void loop() {
  // Turn Pin 3 on
  digitalWrite(LEDPin, HIGH);
}

Variable Types

You may notice the word ‘byte’ before the name of our new LEDPin variable. This is the required variable type and tells the Arduino what ‘kind’ of variable this value is. There are many variable types, but I will describe the most common numeric ones here.

TIP: It is considered best practice to choose variable types with the smallest memory usage to store a value. Since we only needed to store a value of 3 for our LEDPin, using the byte type is the best choice as it can hold a numeric value between 0 and 255 and only consumes one byte of memory.

Variable TypeUsed to storeMemory Usage
bytea non-negative rounded numeric value between 0 and 2551 byte
inta rounded numeric value between -32768 and 327672 bytes
longa rounded numeric value between -2147483648 and 21474836474 bytes
floata numeric value that has floating decimal space, such as 3.1415 – It can hold values with 6-7 digits of precision from -3.4028235E+38 to 3.4028235E+384 bytes
booleana binary value of 1 or 0 (true or false) – Booleans are typically used to test conditions or store binary states such as on/off1 byte
Unsigned Variables

The unsigned modifier can be used with the int and long variable types where only positive values are expected. Since the value will be unsigned (without a – or + sign), it moves the range of possible values to the positive side of zero. Unsigned variables can come in handy to store larger numbers when no negative values are expected:

Variable TypeUsed to storeMemory Usage
unsigned inta rounded numeric value between 0 and 655352 bytes
unsigned longa rounded numeric value between 0 and 42949672954 bytes
Constant Variables

Variables that will never change their value (read only) should be declared as constant. By placing the const modifier in front of the variable type, we are setting that variable to be read-only. Constant variable values cannot be changed by the sketch later on, so only use it when you want to set the value once and leave it alone.

Using my box analogy from earlier, a normal variable is like an open box where we can then reach in change the value on that piece of paper at any time. Or, we could seal a box closed and make it so the value on that paper will stay the same forever (constant variable).

TIP: You may see sketches where constant variables are set using the #define command, however using const is the preferred method.

In our first circuit example, since we have physically wired the LED to DIGITAL Pin 3, our LEDPin byte variable should never change. This is a perfect example of where to use const to declare a constant variable:

/*
  A sketch that will light a LED and uses a Variable
*/
// Create a byte-sized variable named LEDPin and set its value to 3
const byte LEDPin = 3;
void setup() {
  // Set LEDPin (Pin 3) as an output pin
  pinMode(LEDPin,OUTPUT);
}
void loop() {
  // Turn Pin 3 on
  digitalWrite(LEDPin,HIGH);
}

Download my Sketch

To help others, I have shared this updated sketch via the Arduino Editor. Simply click the Open Code or Download buttons!

Next Arduino Page


IF/ELSE STATEMENTS – Evaluating conditions and acting on the results

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.