This is a kid’s game project (Buzz Wire) I got off the internet. Thanks to very good help from groundFungus on this forum, it works fine, but…
The original code includes the Monty Python theme played on piezos. It’s a nice bit of programming but somewhat grating as it repeats endlessly and rather loudly.
I’m a rank beginner, but I have worked through two online tutorials (Code Academy was quite basic, but I got through about 12 or 14 lessons on Alex Allain’s cprogramming.com before finding myself over my head). I’ve been able to modify the code so that when the circuit is completed the game actually ends (using “exit(0)”), which also stops the music. So now the problem is that ending the annoying music is not only a distraction, but a positive incentive to lose the game!
What I’d like to do is have the ability to manually turn the music on or off. I’m thinking I could add a button switch that would start or stop the music whenever it was pushed. I’ve made a few attempts, but I really can’t see how the code flows, so all my tries have been way off the mark.
Can anyone help me out with a plausible solution? And if so, I need guidance on what areas of the code you need to see. I’m including the “sing()” block, which is what runs the music. I’m also including the fritz board diagram, in case that’s helpful.
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
}
}
}
BuzzGame_bb.pdf (187 KB)