stop_bounce is a bool. When checkKey is called the state of stop_bounce either allows a read from the key pin or not. It Not.. it increments a counter and once that counter hits 31, stop_bounce is set to false so the next time checkKey is called, the key pin can be read again. The sub should have to be called 30 times before allowing another key pin test, For some reason, it is allowing keycode to be set to 1, 2 times before stop_bounce is true. Hope that's understandable. I suck at translation of thought. 
Here is all of the Sketch. I built a StarGate that is pretty amazing. All the code works but the key repeating before reading is blocked. Think I will try a timer and not a counter.
Any recommendations on this code?
#include <SoftwareSerial.h>
#define FASTLED_ALLOW_INTERRUPTS 1
#include <FastLED.h>
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#define rxPin 10
#define txPin 5
SoftwareSerial nanoPort(rxPin, txPin);
int master_vol = 20;//bigger = quieter
// key pins
#define key1 A5
// define the pins used
#define CLK 13 // SPI Clock, shared with SD card
#define MISO 12 // Input data, from VS1053/SD card
#define MOSI 11 // Output data, to VS1053/SD card
#define SHIELD_RESET 8 // VS1053 reset pin (unused!)
#define SHIELD_CS 6 // VS1053 chip select pin (output)
#define SHIELD_DCS 7 // VS1053 Data/command select pin (output)
#define CARDCS 9 // Card chip select pin
#define DREQ 2 // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer =
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
// led pins
#define DATA_PIN_CHEVS A0 // StarGate 9 led data
#define DATA_PIN_GATE A1 // StarGate 134 led data
#define DATA_PIN_BACKLIGHT A3 // back lighting 9 leds
#define NUM_LEDS_CHEVS 9
#define NUM_LEDS_GATE 133
#define NUM_LEDS_BACKLIGHTING 9
// Define the array of leds
CRGB leds_chevs[NUM_LEDS_BACKLIGHTING];
CRGB leds_gate[NUM_LEDS_GATE];
CRGB leds_backlighting[NUM_LEDS_BACKLIGHTING];
byte out_data[3];
byte data_in;
byte keycode = 0;
byte bounce;
byte led_index = 0;
byte led_shift_gate = 0;
byte current_addy;
byte next_addy;
byte p_pnt = 0;
byte stop_bounce = false;
unsigned long openGateTimer = 0;
float current_angle = 0.0;
boolean stepperBusy = false;
boolean activate = false;
boolean activating = false;
boolean flip = false;
boolean waitForFx = false;
// out commands
#define RUNTOFORWARD 100
#define RUNTOBACKWARDS 101
#define SHUTMOTOROFF 2
// in commands
#define RUNCOMPLETE 42
#define ACK 41
/////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(9600);
Serial.println(F("Reset"));
nanoPort.begin(9600);
if (! musicPlayer.begin()) { // initialise the music player
Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
while (1);
}
musicPlayer.setVolume(200, 200); // shut it off
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int
if (!SD.begin(CARDCS)) {
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
// shutOffChevs();
// shutOffGate();
// shutOffBack();
// Set up key
pinMode(key1, INPUT_PULLUP);
delay(500);
}
// subs //----------------------------------------------------------------
void convertToArray(byte code, int val) {
out_data[0] = code;
byte b1 = val & 255;
byte b2 = (val & 65280) / 256;
out_data[1] = b1;
out_data[2] = b2;
}
void dialAddress(void) {
static byte p_address[] = {26, 6, 14, 31, 11, 29, 0}; //abydos default
if (p_pnt == 7) {
return;
}
if (p_pnt == 0) {
shutOffChevs();
}
// Serial.println("playing");
next_addy = p_address[p_pnt] ;
//Serial.println(next_addy);
int s = 100 * current_addy;
int d = 100 * next_addy;
int v = d - s;
if (v < 0) {
out_data[0] = RUNTOBACKWARDS;
} else {
out_data[0] = RUNTOFORWARD;
}
stepperBusy = true;
convertToArray(abs(v));
nanoPort.write(out_data, 3);
current_addy = next_addy;
play("fx/roll.mp3", 10 + master_vol);
// while ( ! musicPlayer.playingMusic ) {} // Make sure it starts
delay(120);
FastLED.addLeds<WS2812B, DATA_PIN_CHEVS, RGB>(leds_chevs, NUM_LEDS_CHEVS);
leds_chevs[0] = CHSV(96, 255, 255);
leds_chevs[8] = CHSV(96, 255, 255);
leds_chevs[4] = CHSV(96, 255, 255);
FastLED.show();
back_FO(96, 255);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
//states the app can be running in
#define dark 0
#define idle 1
#define warning 2
#define wait_alarm 3
#define dialing 4
#define activate 5
#define openinggate 6
#define gateopen 7
#define gateclosing 8
#define gateclosed 9
// start off with color changing lights
byte state = idle;
void loop()
{
static byte hue = 0;
// triggers events and sound
// There are 10 possible states during the operation.
// Setting state to 1 starts the dialing process and show.
switch (state) {
case dark:
// nothing is happening. Lights are off.
break;
case idle:
// the show may have been canceled so read off any pending data
if (nanoPort.available()) {
Serial.println("nano data available");
data_in = nanoPort.read();
}
p_pnt = 0; // this is the current dialling address. Reset it!
waitForFx = false; // incase we canceled and its waiting for FX to stop playing.
// if music is playing after cancel, stop it NOW!
if (musicPlayer.playingMusic) {
musicPlayer.stopPlaying();
}
// idle mode so lets do some lighting :)
Spiral_sine(hue, -1, 16);
back_FO(hue, 255); // all leds at that colar at that level.
chevs_FO(hue, 255); // all leds at that colar at that level.
break;
case warning:
shutOffChevs();
shutOffGate();
shutOffBack();
play("fx/alarm.mp3", master_vol);
delay(50); // time to start playing
waitForFx = true;
Serial.println("Warning");
state = wait_alarm;
break;
case wait_alarm:
back_FO(96, hue);
hue += 1;
break;
case dialing:
if (nanoPort.available()) {
Serial.println("nano data available");
data_in = nanoPort.read();
if (data_in == RUNCOMPLETE) {
//dialed all chevrons so lets setup for ativatie sequence.
if (p_pnt == 6) {
state = activate;
playChevronLocked(); // Set leds, play lock fx and waits to get done playing
waitForFx = true;
p_pnt = 0;
state = activate;
} else {
// Serial.println(F("RUNCOMPLETE"));
playChevronLocked(); // Set leds, play lock fx and waits to get done playing
// Serial.println(p_pnt);
}
}
stepperBusy = false;
}
if (state == dialing) {
if (! stepperBusy) {
dialAddress();
}
}
break;
case activate:
break;
case openinggate:
//shake it up
back_random_levels(1, true);
quake();
break;
case gateopen:
Spiral_sine(185, -1, 16);
back_random_levels(185, false);
break;
case gateclosing:
//shake it up
back_random_levels(1, true);
quake();
break;
}
// wait for FX sound to finish playing and move on
if (waitForFx) {
if (! musicPlayer.playingMusic) {
switch (state) {
case wait_alarm:
state = dialing;
break;
case dialing:
break;
case activate:
play("fx/activate.mp3", 10 + master_vol);
// Serial.println("fx/activate.mp3");
state = openinggate;
activating = true;
waitForFx = true;
led_index = 0;
delay(300);
break;
case openinggate:
play("/fx/loop.mp3", 0 + master_vol);
delay(200); // synch light start
waitForFx = true;
state = gateopen;
break;
case gateopen:
play("fx/close.mp3", 10 + master_vol);
waitForFx = true;
state = gateclosing;
break;
case gateclosing:
state = gateclosed;
waitForFx = false;
state = idle;
break;
}
}
}
hue++;
scanKey();
}//loop
////////////////////////////////////////////////////////
void play(String s, int v) {
musicPlayer.stopPlaying();
musicPlayer.startPlayingFile(s.c_str());
musicPlayer.setVolume(v, v);
Serial.print("playing :");
Serial.println(s);
}
void playChevronLocked(void) {
static byte chev_index[] = {3, 2, 1, 7, 6, 5, 4};
// Serial.println("lock playing");
byte cv = p_pnt + 1;
String t = "fx/chevron" + String(cv) + ".mp3";
// t = "fx/loop.mp3";
play(t, 10 + master_vol);
delay(200);
FastLED.addLeds<WS2812B, DATA_PIN_CHEVS, RGB>(leds_chevs, NUM_LEDS_CHEVS);
leds_chevs[0] = CHSV(255, 0, 255);
leds_chevs[8] = CHSV(255, 0, 255);
leds_chevs[4] = CHSV(96, 255, 0);
leds_chevs[chev_index[p_pnt]] = CHSV(192, 255, 255);
FastLED.show();
//Serial.println(musicPlayer.currentTrack );
back_FO(185, 255);
while (musicPlayer.playingMusic);
p_pnt += 1;
}
void checkKey(void) {
if (stop_bounce) {
bounce++;
if (bounce > 30) {
stop_bounce = false;
bounce = 0;
}
return;
} else {
if (digitalRead(key1) == LOW) {
bounce = 0;
stop_bounce = true; // got a press so lets not accept anymore for a while
keycode = 1;
Serial.println(F("K1"));
}
}
}
void scanKey(void) {
//check keys
checkKey();
if (keycode == 1) {
lights_out();
switch (state) {
case dark:
Serial.println(F("to idle state"));
state = idle;
break;
case idle:
Serial.println(F("to dark state"));
state = dark;
break;
default:
if (state > idle) {
state = idle;
Serial.println(F("to idle state"));
break;
}
}
keycode = 0;
stop_bounce = true;
}
}
void lights_out(void) {
shutOffChevs();
shutOffGate();
shutOffBack();
}
void Spiral_sine(int color, int dir, int scaler) {
FastLED.addLeds<WS2812B, DATA_PIN_GATE, RGB>(leds_gate, NUM_LEDS_GATE);
//Serial.println(led_index);
float pi = 3.14159;
float p = pi * scaler / 133.0;
float cur_ang = 0;
float j = random(255) / 255; //0 to 1
if (state == gateopen) {
color += random(10);
}
for (int i = 0; i < NUM_LEDS_GATE; i++) {
int sine_val = sin(cur_ang + pi / 2.0) * 127;
//sine_val = min(sine_val, led_index * 28);
cur_ang += p;
if (state == gateopen) {
sine_val *= j;
leds_gate[ check_overflow_gate(i + led_shift_gate * dir) ] = CHSV(color, 255, sine_val + random(125));
} else {
leds_gate[ check_overflow_gate(i + led_shift_gate * dir) ] = CHSV(color, 255, sine_val + 127.5);
}
}
FastLED.show();
led_shift_gate++;
if (led_shift_gate >= NUM_LEDS_GATE) {
led_shift_gate = 0;
}
}
//led_shift_gate rollover check
int check_overflow_gate( int n) {
if (n >= NUM_LEDS_GATE) {
return n - NUM_LEDS_GATE;
}
if (n < 0) {
return n + NUM_LEDS_GATE;
}
return n;
}
//
void back_FO(int color, byte level) {
FastLED.addLeds<WS2812B, DATA_PIN_BACKLIGHT, RGB>(leds_backlighting, NUM_LEDS_BACKLIGHTING);
for (int i = 0; i < NUM_LEDS_BACKLIGHTING; i++) {
leds_backlighting[i] = CHSV(color, 255, level);
}
FastLED.show();
}
void chevs_FO(int color, byte level) {
FastLED.addLeds<WS2812B, DATA_PIN_CHEVS, RGB>(leds_chevs, NUM_LEDS_CHEVS);
for (int i = 0; i < NUM_LEDS_CHEVS; i++) {
leds_chevs[i] = CHSV(color, 255, level);
}
FastLED.show();
}
void shutOffChevs(void) {
FastLED.addLeds<WS2812B, DATA_PIN_CHEVS, RGB>(leds_chevs, NUM_LEDS_CHEVS);
for (int i = 0; i < NUM_LEDS_CHEVS; i++) {
leds_chevs[i] = CRGB::Black;
}
FastLED.show();
}
void shutOffGate(void) {
FastLED.addLeds<WS2812B, DATA_PIN_GATE, RGB>(leds_gate, NUM_LEDS_GATE);
for (int i = 0; i < NUM_LEDS_GATE; i++) {
leds_gate[i] = CRGB::Black;
}
FastLED.show();
}
void shutOffBack(void) {
FastLED.addLeds<WS2812B, DATA_PIN_BACKLIGHT, RGB>(leds_backlighting, NUM_LEDS_BACKLIGHTING);
for (int i = 0; i < NUM_LEDS_BACKLIGHTING; i++) {
leds_backlighting[i] = CRGB::Black;
}
FastLED.show();
}
//
void back_random_levels(int color, boolean W) {
FastLED.addLeds<WS2812B, DATA_PIN_BACKLIGHT, RGB>(leds_backlighting, NUM_LEDS_BACKLIGHTING);
if (W) {
for (int i = 0; i < NUM_LEDS_BACKLIGHTING; i++) {
leds_backlighting[i] = CHSV(255, 0, random(255));
}
} else {
for (int i = 0; i < NUM_LEDS_BACKLIGHTING; i++) {
leds_backlighting[i] = CHSV(color, 255, random(255));
}
}
FastLED.show();
}
//
void quake() {
FastLED.addLeds<WS2812B, DATA_PIN_GATE, RGB>(leds_gate, NUM_LEDS_GATE);
int r = random(255);
for (int i = 0; i < NUM_LEDS_GATE; i++) {
leds_gate[i] = CHSV(185, led_index, r);
}
led_index += 2;
FastLED.show();
}
void wave_134(int color, boolean W) {
FastLED.addLeds<WS2812B, DATA_PIN_GATE, RGB>(leds_gate, NUM_LEDS_GATE);
int v = (sin(current_angle) + 1) * 127.4;
//Serial.println(v);
if (W) {
for (int i = 0; i < NUM_LEDS_GATE; i++) {
leds_gate[i] = CHSV(255, 0, v);
}
} else {
for (int i = 0; i < NUM_LEDS_GATE; i++) {
leds_gate[i] = CHSV(color, 255, v);
}
}
FastLED.show();
}
//
void convertToArray(int val) {
byte b1 = val & 255;
byte b2 = (val & 65280) / 256;
out_data[1] = b1;
out_data[2] = b2;
}