void StateMachine() {
// logic for when the note is detected
if(noteDet){
//Serial.println(songIndex);
lastHit = micros();
noteDet = false;
if(santaClauseIsComingToTown[songIndex] == noteVal){
send = 0;
SendData();
songIndex++;
sequenceIndex++;
Lights(XY_GREEN);
Serial.println("Green");
wrongCount = 0;
CheckSequence();
} //
else if (wrongCount < maxWrongNotes) {
Lights(XY_YELLOW);
send = 1;
SendData();
Serial.println("Yellow");
wrongCount++;
}
else {
send = 2;
SendData();
Lights(XY_RED);
Serial.println("Red");
if(maxWrongNotes < 5) {
maxWrongNotes++;
}
songIndex -= sequenceIndex;
sequenceIndex = 0;
wrongCount = 0;
}
} // check to see if it is time to reset as long as there wasn't a hit already
else if(lastHit != 0){
currentTime = micros();
// turn white to be ready for next hit
if((currentTime - lastHit) >= (MICROS_TO_RESET + MICROS_RESET_LIGHTS)){
//Serial.println("Ready for Hit");
Lights(XY_WHITE);
sentReset = 0;
} // Check to see if the last hit was long enough ago to reset
else if((currentTime - lastHit) >= MICROS_TO_RESET){
// send information to reset if it hasn't already been sent
if(!sentReset){
send = 4;
SendData();
Serial.println("Reset");
sentReset = 1;
}
// flash the lights red and reset variables for the beginning of the game
Lights(XY_RED);
songIndex = 0;
sequenceIndex = 0;
sequenceNum = 0;
leds.set_leds_grow(seqLengths[sequenceNum]);
wrongCount = 0;
} // Send a warning on the LEDs if the reset is about to happen
else if((currentTime - lastHit) >= (MICROS_TO_RESET - MICROS_WARNING)){
// flash red for even warning segments
// start back at the beginning of the sequence if they play a note during the warning
songIndex -= sequenceIndex;
sequenceIndex = 0;
wrongCount = 0;
sentReset = 0;
if((int)((((currentTime - lastHit) - (MICROS_TO_RESET - MICROS_WARNING))/(MICROS_WARNING/FLASH_SEGMENTS))) % 2 == 0){
Lights(XY_RED);
} // flash white for odd warning segments
else{
Lights(XY_WHITE);
}
//Serial.println("Warning");
}
}
}