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 Type | Used to store | Memory Usage |
byte | a non-negative rounded numeric value between 0 and 255 | 1 byte |
int | a rounded numeric value between -32768 and 32767 | 2 bytes |
long | a rounded numeric value between -2147483648 and 2147483647 | 4 bytes |
float | a 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+38 | 4 bytes |
boolean | a binary value of 1 or 0 (true or false) – Booleans are typically used to test conditions or store binary states such as on/off | 1 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 Type | Used to store | Memory Usage |
unsigned int | a rounded numeric value between 0 and 65535 | 2 bytes |
unsigned long | a rounded numeric value between 0 and 4294967295 | 4 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