I'm going to attempt to upload my Fritz diagram as a jpg. I seem to remember attachments are tricky on this forum, so I may not be able to. Unfortunately, this diagram doesn't include the button that turns off the music, but other than that I think it is up to date.
And here's part 2 of the code:
//START OF PART 2 OF 2 POSTINGS
/*============== START of TIMER BUTTON PRESS ===================*/
// Change mode manually here, in the code. 0 = challenge; 1 = free play
// NOTE: Challenge mode is Free Play until the button is pressed to start countdown
if (mode == 0)
{
// challenge mode
if (digitalRead(2) == HIGH) //Pin 2 is the button
{
delay(25); //Presumably there needs to be a 25ms delay to ensure the button was pressed
if (digitalRead(2) == HIGH)
{
countdown = true; // start the countdown
}
else
{
countdown = false; // stop the countdown
}
}
if (countdown)
{
showCountdown(); // advance countdown
}
}
else
{
// free play
toggleFreePlay();
}
/*============== END of TIMER BUTTON PRESS ===================*/
//Pin 9 is the course-handle circuit. When 9 is activated "Game Over" is displayed and game ends.
//Press the white reset button on the UNO board to reload and play again.
if (digitalRead(9) == HIGH)
{
delay(25);
if (digitalRead(9) == HIGH)
{
quit = true; //When the course circuit is complete the quit flag is set.
while (digitalRead(9) == HIGH)
{
buzz(8, NOTE_B0, 1000 / 24);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Game Over! RESET");
lcd.setCursor(0,1);
lcd.print("to play again");
}
// Regarding exiting the loop, see Zoul007's post here: https://stackoverflow.com/questions/23096366/how-to-stop-a-loop-arduino
if (quit == true){exit(0);} //When the course circuit is broken again, the game ends
}
}
else
{
if (playMontyFlag == true)
{
sing();
}
}
} // END OF 'VOID LOOP'
void showCountdown()
{
// countdown the time remaining
unsigned long currentMillis = millis(); // current time
if (currentMillis - previousMillis3 >= interval3)
{
previousMillis3 = currentMillis;
--count;
showNumber(count);
if (count == 0)
{
// game over
quit = true; //TEST
countdown = false;
// reset countdown
count = 21;
// buzz 3 times
buzz(8, NOTE_B0, 1000 / 24); //[changed from 11 to 8]
delay(100);
buzz(8, NOTE_B0, 1000 / 24); //[changed from 11 to 8]
delay(100);
buzz(8, NOTE_B0, 1000 / 24); //[changed from 11 to 8]
lcd.clear();
lcd.setCursor(0,0);
/*lcd.print("Handle down");
lcd.setCursor(0,1);
lcd.print("Game Over!");*/
lcd.print("Game Over! RESET");
lcd.setCursor(0,1);
lcd.print("to play again");
if (quit == true){exit(0);} //TEST
}
}
}
void showNumber(int number)
{
lcd.clear();
lcd.setCursor(4,0);
lcd.print("-- ");
lcd.setCursor(7,0);
lcd.print(number);
lcd.print(" --");
}
void toggleFreePlay()
{
// scroll between words without blocking
unsigned long currentMillis = millis(); // current time
if (currentMillis - previousMillis1 >= interval1)
{
previousMillis1 = currentMillis;
if (displayStatus == 1)
showPlay();
else
showFree();
}
}
void showPlay()
{
// write "PLAY" to the display
lcd.clear();
lcd.setCursor(6,1);
lcd.print("PLAY");
displayStatus = 2;
}
void showFree()
{
// write "Free" to the display
lcd.clear();
lcd.setCursor(6,0);
lcd.print("FREE");
displayStatus = 1;
}
void buzz(int targetPin, long frequency, long length)
{
/* Buzzer example function by Rob Faludi
http://www.faludi.com
https://gist.github.com/AnthonyDiGirolamo/1405180
*/
long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i = 0; i < numCycles; i++) // for the calculated length of time...
{
digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphragm
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphragm
delayMicroseconds(delayValue); // wait again for the calculated delay value
}
}
void sing()
{
// play the song in a non blocking way
unsigned long currentMillis = millis();
if (currentMillis - previousMillis2 >= interval2)
{
previousMillis2 = currentMillis;
int noteDuration = 1000 / tempo[songState];
buzz(10, melody[songState], noteDuration);
int pauseBetweenNotes = noteDuration;
delay(pauseBetweenNotes);
// stop the tone playing:
buzz(10, 0, noteDuration);
++songState;
// start song again if finished
if (songState > 79)
{
songState = 14; // skip intro
}
}
}
//END OF PART 2 POSTING