Now consider how we are calling lightsBeep()
in the indicateYes()
and indicateNo()
functions.
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.
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.
#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:
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.
/////////////////////////////////////////////////////////////////////////////// // 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(); } }
Page last edited January 13, 2017
Text editor powered by tinymce.