Here's some sagely guidance I once got from an excellent programmer:

Design your code to work for 0, 1, or infinite cases.

Designing for 0 cases is the trivial answer, don't write any code at all. Done. Designing for 1 case makes things easy as you can get away with sort of hardwiring things for that case. For example, we loop over only 10 NeoPixels in `showPixels()` as we are designing specifically for the Circuit Playground, which has 10 NeoPixels.

Designing for infinite cases is quite often much more difficult. However, we have an opportunity to do some of this in our code. The two functions `indicateYes()` and `indicateNo()` basically do the same thing - they turn on the NeoPixels and make a beep noise a certain amount of times. Yeah, we're only doing it once for "yes" and twice for "no", but it's not too difficult to create a function that could do this any number of times.

```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);
}
}```

Now we just call this function from `indicateYes()` and `indicateNo()` passing in the behavior we want. Like this:

```void indicateYes() {
lightsBeeps(1, 700, 750, 0xFF6600);
}

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

Here's the complete code with all the new changes.

```///////////////////////////////////////////////////////////////////////////////
// Circuit Playground Yes No v3
//
// One beep (left button) = Yes
// Two beeps (right button) = No
//
// Author: Carter Nelson

///////////////////////////////////////////////////////////////////////////////
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, 0xFF6600);
}

///////////////////////////////////////////////////////////////////////////////
void indicateNo() {
lightsBeeps(2, 700, 500, 0xFF6600);
}
///////////////////////////////////////////////////////////////////////////////
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 Mar 08, 2024.