Refactor 4

Now consider how we are calling lightsBeep() in the indicateYes() and indicateNo() functions.

Download: file
void indicateYes() {
  lightsBeeps(1, 700, 750, 0xFF6600);
}

void indicateNo() {
  lightsBeeps(2, 700, 500, 0xFF6600);
}

Note how we are specifying the color of the NeoPixels in two places. What if you wanted to change the color? You'd have to edit both of those lines of code. With only two places to look, this isn't that difficult. But as code grows, there's the possibility of it showing up in more places and the chances of overlooking one or more of them increases. Also, what if you needed that value somewhere else in your code. You'd have to remember what it was, or go find it, so you could type it in again.

The term 'magic number' is used to refer to these types of values.

A better way to deal with values like this is to define them in some global manner and then use that reference as needed in the code. For things that are constant, this is typically done using a #define as follows.

Download: file
#define PIXEL_COLOR 0xFF6600

Note that there is no = sign. These lines that begin with # are actually dealt with before the code is even compiled. In the case of a #define, it works likes a simple search-and-replace. Every occurrence of PIXEL_COLOR is replaced with 0xFF6600.

Now we can use this in our code like this:

Download: file
void indicateYes() {
  lightsBeeps(1, 700, 750, PIXEL_COLOR);
}

void indicateNo() {
  lightsBeeps(2, 700, 500, PIXEL_COLOR);
}

And anytime we want to change the color, we only have to change one thing in one place. Here's the complete code with all the changes.

Download: file
///////////////////////////////////////////////////////////////////////////////
// Circuit Playground Yes No v4
//
// One beep (left button) = Yes
// Two beeps (right button) = No
//
// Author: Carter Nelson
// MIT License (https://opensource.org/licenses/MIT)

#include <Adafruit_CircuitPlayground.h>

#define PIXEL_COLOR 0xFF6600

///////////////////////////////////////////////////////////////////////////////
void lightsBeeps(int repeats, int note, int duration, uint32_t color) {
  for (int n=0; n<repeats; n++) {
    showPixels(color);
    CircuitPlayground.playTone(note, duration);
    CircuitPlayground.clearPixels();
    if (repeats>1) delay(duration/2);
  }
}

///////////////////////////////////////////////////////////////////////////////
void showPixels(uint32_t color) {
  for (int p=0; p<10; p++) {
    CircuitPlayground.setPixelColor(p, color);
  }  
}
  
///////////////////////////////////////////////////////////////////////////////
void indicateYes() {
  lightsBeeps(1, 700, 750, PIXEL_COLOR);
}

///////////////////////////////////////////////////////////////////////////////
void indicateNo() {
  lightsBeeps(2, 700, 500, PIXEL_COLOR);
}
     
///////////////////////////////////////////////////////////////////////////////
void setup() {
  CircuitPlayground.begin();
}

///////////////////////////////////////////////////////////////////////////////
void loop() {
  if (CircuitPlayground.leftButton()) {
    indicateYes();
  }  
  if (CircuitPlayground.rightButton()) {
    indicateNo();    
  }
}
This guide was first published on Jan 23, 2017. It was last updated on Jan 23, 2017. This page (Refactor 4) was last updated on Mar 01, 2020.