Getting programming questions answered on the internet can be problematic. Programmers love to have opinions, stick to those ideas, and express them to you even when their opinion has nothing to do with your question(s).
Not only am I going to explain how to use flag variables in your code, I am going to encourage their useβwhich most programmers avoid.
However, this advice comes with two caveats.
- This information only applies to limited resource environments like an Arduino, LaunchPad or PIC.
- Use flag variables very carefully when you do use them.
The following flag variable usage examples are Arduino-centric but apply to any microcontroller platform, including the Energia project for TI Launchpads.> Flag variables are a Good Thing(tm) – when used properly
What are Flag Variables?
Flag variables are used to indicate a single state. Like an LED that needs to be “ON” or “OFF.” In fact, you’ll see in many of my mlllis() cookbook examples, I use a flag variable to indicate the state of an LED.
Here are a few places where you can use flag variables.
Flag Variables Case #1: Interrupts
When using interrupts you want to get in and out of the interrupt service routine (ISR) as fast as possible. An ISR is an excellent example of where you should use flag variables. When an ISR occurs, set a flag and exit. In the main loop() of your program, you check for the flag and do what needs to be done when you see it.
Example
// Flag variable
// ISRs require them to be "volatile"
volatile bool needToSend = false;
void setup() {
Serial.begin(9600);
// Interrupt 0 on Uno is Pin 2!
attachInterrupt(0, printText, CHANGE);
}
void loop() {
if (needToSend) {
// send our very long message
Serial.println("Pin 2 changed states, thought you should know... kthxbye");
// clear the flag
needToSend = false;
}
}
// ISR for Interrupt 0
void printText() {
// Set the flag and get out
needToSend = true;
}
In this (contrived) example, when an interrupt occurs on Pin 2, we send a very long message over serial. You would not want to sit in the ISR waiting for this transmit to finish.
Flag Variables Case #2: Simplify Yes / No Operations
Often when reading multiple sensors, you may have one function or loop that speeds through all of the available inputs. If your code only needs to act if one of the inputs change, you could use a flag variable to indicate a the change. The flag would prevent you from having to run through another loop checking all the results.
Good practices when using flags:
- Make name yes/no question.
- When you read the code, its meaning should be obvious.
- Don’t use a flag when something else could have worked, like an enum.
Example
// Flag Variable
bool inputChanged = false;
int currentValues[6];
int previousValues[6];
void setup() {
// Just want to toggle something
pinMode(13, OUTPUT);
// load variables with known values
for (int i = 0; i < 6; i++) {
currentValues[i] = 0;
previousValues[i] = 0;
}
}
void loop() {
// Read A0..A5, compare to last read
for (int x = 0; x < 6; x++) {
currentValues[x] = analogRead(x);
if (currentValues[x] != previousValues[x])
inputChanged = true;
// Store previous for next check
previousValues[x] = currentValues[x];
}
if (inputChanged) {
// Just toggle LED, kinda lame, I know
digitalWrite(13, !digitalRead(13));
// Don't forget to clear
inputChanged = false;
}
}
Flag Variables Case #3: Don’t use flags, use states
A close cousin to a flag variable is the state. While a flag is a single state event, which is probably fine for something like an “ON” or “OFF”. However, think about blinking an LED. You might say “either I want it to blink or not blink.” However, there are four states: “Blinking,” “ON,” “OFF,” and “Not Blinking.”
One option is, of course, to have two flag variables. One called shouldBlink and blinkState. Another option is to use an enumerated type, also known as, enum. It’s probably better to save enums and state machines for another post.

