GLOBAL VARIABLES

Tutorial Goals:

Global variables are a powerful programming tool in Arduino. They can be especially helpful in building dynamic adjustable programs.

Prerequisite Tutorials:

What Are Global Variables?

In Arduino there are several types of variables from floats and integers to booleans. There exists also, two different ways of calling a variable. Creating a variable within a program renders it for that specific program only. It can not be called by another program and for this reason does not create naming conflicts. Global variables are placed outside of any program, usually on the top of the sketch near any library inclusions before the setup() program.

 

// Global Variables here

void setup(){}

void loop(){}

 

Any variable placed in this space can be used and edited within any program in your sketch.

Using Constant Integers:

Constant variables are declared using "const" before the normal variable decleration. Constant variables are great for saving pin locations in any sketch. Because they are easy to access and adjust, they are also excellent for saving conditional or accuracy variables. These are the kinds of variables you want to have easy access to for adjusting how your project works.

 

const int led = 13, button = 5; // led pin D13 and button pin D5

const int rounds = 8; // How many rounds should the user play

const int debounce = 300; // Millisecond delay for debouncing button presses

 

Now every instance of these variables in the entire sketch can be adjusted by changing this decleration.

Integer and Boolean Conditions:

Some glabal variables are not constant, but they are used by many programs. The Arduino has more limited storage for global variables, so making every variable global is not recommended. This does present the oportunity, however, to create a few condition variables that do not need to be passed from program to program. Some of these conditions could include the state of a game or an adjustable variable such as brightness which affects many programs in the sketch.

 

boolean gameEnd = false, action = false; // These bianary values represent the state of the game

int volume = 5; // Start the volume at 5 before the other programs use and adjust it

 

Example Formatting:

 

const int led = 13, button = 5; // led pin D13 and button pin D5

const int rounds = 8; // How many rounds should the user play

const int debounce = 300; // Millisecond delay for debouncing button presses

boolean gameEnd = false, action = false; // These bianary values represent the state of the game

int volume = 5; // Start the volume at 5 before the other programs use and adjust it

void setup(){

pinMode(led, OUTPUT); // Set pin D13 to digital output

pinMode(button, INPUT_PULLUP); // Set pin D5 to an input with a 10K pullup resistor

digitalWrite(led, HIGH); // Turn LED on D13 high

}

void loop(){

for(int a = 0; a < rounds; a++){ // Run the loop 8 times

if(!digitalRead(button)){ // If pin D5 is pulled to ground (button pressed)

delay(debounce); // avoid double clicks

action = true; // Button is being pressed

if(action){ // If the button is pressed this round

digitalWrite(led, LOW); // Turn LED on D13 high

}

}

} // Run loop again if the rounds are not over

gameEnd = true; // This state boolean could be used to restart the game or show credits

}

 

 

Congratulations, you are ready to use Global Variables!

Still Need Help?

Contact us using our Support Form with any questions you have.