// music
int songState = 0;
int melody[] = {
NOTE_F4, NOTE_E4, NOTE_D4, NOTE_CS4,
NOTE_C4, NOTE_B3, NOTE_AS3, NOTE_A3,
NOTE_G3, NOTE_A3, NOTE_AS3, NOTE_A3,
NOTE_G3, NOTE_C4, 0,
NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
NOTE_GS3, NOTE_A3, NOTE_F4, NOTE_C4,
NOTE_C4, NOTE_A3, NOTE_AS3, NOTE_AS3,
NOTE_AS3, NOTE_C4, NOTE_D4, 0,
NOTE_AS3, NOTE_G3, NOTE_G3, NOTE_G3,
NOTE_FS3, NOTE_G3, NOTE_E4, NOTE_D4,
NOTE_D4, NOTE_AS3, NOTE_A3, NOTE_A3,
NOTE_A3, NOTE_AS3, NOTE_C4, 0,
NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
NOTE_GS3, NOTE_A3, NOTE_A4, NOTE_F4,
NOTE_F4, NOTE_C4, NOTE_B3, NOTE_G4,
NOTE_G4, NOTE_G4, NOTE_G4, 0,
NOTE_G4, NOTE_E4, NOTE_G4, NOTE_G4,
NOTE_FS4, NOTE_G4, NOTE_D4, NOTE_G4,
NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_C4,
NOTE_B3, NOTE_C4, NOTE_B3, NOTE_C4, 0
};
int tempo[] = {
8, 16, 8, 16,
8, 16, 8, 16,
16, 16, 16, 8,
16, 8, 3,
12, 16, 16, 16,
8, 16, 8, 16,
8, 16, 8, 16,
8, 16, 4, 12,
12, 16, 16, 16,
8, 16, 8, 16,
8, 16, 8, 16,
8, 16, 4, 12,
12, 16, 16, 16,
8, 16, 8, 16,
8, 16, 8, 16,
8, 16, 4, 16,
12, 17, 17, 17,
8, 12, 17, 17,
17, 8, 16, 8,
16, 8, 16, 8, 1
};
// non blocking setup
// free play
unsigned long previousMillis1 = 0; // time words last changed
const long interval1 = 1500; // interval between changing
// music
unsigned long previousMillis2 = 0; // time last changed
const long interval2 = 100; // interval between notes
int displayStatus = 0; // keep track of what's displayed
int mode = 0; // keep track of game mode -- change to 0 or 1 for different modes
bool countdown = false;
unsigned long previousMillis3 = 0; // time last changed
const long interval3 = 1000; // interval between countdown
int count = 20; // challenge mode timer
void setup() {
// put your setup code here, to run once:
pinMode(9, INPUT); // setup circuit
pinMode(10, OUTPUT); // setup buzzer 1
pinMode(11, OUTPUT); // setup buzzer 2
pinMode(2, INPUT); // setup button
}
void buzz(int targetPin, long frequency, long length) {
const int buzzerAnthony = 9;
const int metalAnthony = 2;
void buzzer() {
pinMode(buzzerAnthony, OUTPUT);
pinMode(metalAnthony, INPUT);
}
void tester() {
if (digitalRead(metalAnthony) == HIGH)
{
tone(buzzerAnthony, 1000);
}
else {
noTone(buzzerPin);
}
}
}
void loop() {
// put your main code here, to run repeatedly:
if(mode == 0) {
// challenge mode
if(digitalRead(2) == HIGH) {
delay(25);
if(digitalRead(2) == HIGH) {
countdown = true; // stop the countdown
}
else {
countdown = false; // stop the countdown
}
}
if(countdown) {
showCountdown(); // advance countdown
}
}
else {
// free play
toggleFreePlay();
}
if(digitalRead(10) == HIGH) {
delay(25);
if(digitalRead(10) == HIGH) {
while(digitalRead(10) == HIGH) {
buzz(11, NOTE_B0, 1000/24);
}
}
}
else
sing();
}
void showNumber(int number) {
// show numbers (maximum 99) on display
_display->display(0, 25); // write - to segment 1
_display->display(3, 25); // write - to segment 4
// write number to middle of display
if(number == 10)
{
_display->display(1,1);
_display->display(2,0);
}
else if(number > 9)
{
_display->display(1,1);
int newVal = number - 10;
_display->display(2, newVal);
}
else
{
_display->display(1,0);
_display->display(2,number);
}
}
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
_display->display(0, 21); // write P to segment 1
_display->display(1, 18); // write L to segment 2
_display->display(2, 10); // write A to segment 3
_display->display(3, 4); // write Y to segment 4
displayStatus = 2;
}
void showFree() {
// write "Free" to the display
_display->display(0, 15); // write F to segment 1
_display->display(1, 23); // write r to segment 2
_display->display(2, 14); // write E to segment 3
_display->display(3, 14); // write E to segment 4
displayStatus = 1;
}
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
}
}
}