Variables

Variables aren’t unique to computer code, but they take some getting used to. One variable that you use very often is the idea of ‘time’. If you ask me “What time is it?”, I can tell you that it’s exactly 8:25:15 on 9/2/2015 (down to the second!). But if someone asks you tomorrow “What time is it?”, you won’t tell them it’s 8:25:15 on 9/2/2015. Time is a variable: You use the same name (time) for it, but the value of it is changing.


Variables are really fun to use in code because they allow your code to interact with the real world even when things are changing quickly. A clock that prints time will have a variable for hours, minutes, seconds, days, and years. A robot that moves faster when it sees bright light will have a variable for the light level and the motor. When you replace values with variables in your program, the code automatically updates with the world around it.

 

Number Variable Types

Each number variable type has unique features that make it work really well for some scenarios. The most important thing to remember is that if you try to use a number larger than the variable type will allow, strange things will happen to your code. It can be difficult to diagnose these problems. As a general rule, use the smallest variable type that you predict your variable will need.

int variable = -32,767 to 32,767; int stand for integer

bool variable = true (1) / false (0) bool stands for boolean

char variable = -128 to 127; char is usually pronounced like character

byte variable = 0 to 255;

unsigned int variable = 0 to 65,535;

long variable = -2,147,483,647 to 2,147,483,647;

unsigned long variable = 0 to 4,294,967,295;

float variable = - 3.4028235E+38 to 3.4028235E+38;

 

More on Variables

const

Const is a modifier of variables, not a variable itself. When you add const to the front of a variable declaration, you're saying “This variable won't change throughout the rest of the code.” This can be useful for a variable used in place of a pin number, for example. You don't want to accidentally change that variable.

 

Arrays

type_of_variables name_of_array[number_of_variables_in_array] = { array values} 
int guesses[7];
byte months[] = {0, 1, 2, 3, 4};
char variable[6] = {0, 1, 2, 3, 4};

Arrays are a unique variable type- they hold multiple values in a list. To create an array, you type which variable type will fill the array, the name of the array, the number of values in the array , an equals sign, then the values that are in the array.

To write to an array, you can type something like: guesses[0] = 123; and now the first position (position 0) of the array guesses has a value of 123. Then you can do math or other operations with the array value like: if((200 – guesses[0] ) > 100)… and the code will evaluate “Is 200 – 123 greater than 100?”.

 

Scope

When you create a new variable in your code, you give it a type, a name, and a value. Where you place the variable in the code gives it another quality: scope.

If you create a variable at the very top of your code, the entire program can see that variable, change it, and use its value. This is called a global variable.

If you create a variable between two braces { }, that variable only is recognized between the braces. The rest of the program cannot see that variable or use it.  When the curly brace closes } , that variable no longer exists This is called a local variable.

The best practice with scope is to make variables "as local as possible". If you do that, you will not have long lists of variables at the top of your programs. If you need a variable only in void loop(), declare it within loop. If you need the variable in both setup() and loop(), make the variable global by putting it at the top of the program. 

If you declare a variable more than once, such as once in setup() and once in loop(), that is not the same variable- it is two variables with the same name but in different scopes.

 

go to top