/*
Hiya, this is my first game on the Arduino platform!
How it works:
- Press the green button (or space bar) to start the game.
- You'll hear two tones, and it's your goal to guess the difference between them!
- You can guess using the grey buttons (numbers 1 - 7 on the keyboard)
The left button is an interval of 1 (the same note), while the right most button is 7 notes difference.
- If you want to listen to the note again, press the green button (or tap space bar) again to hear it.
Music Theory Disclaimer:
This game only includes notes from the C major scale. This means that the intervals are full notes rather
than semitones, so there are more intervals that this tool doesn't include. I could expand it to take
semitones too, but it would be a lot of buttons extra. At that point, it would be more efficient to use a keypad matrix.
If you enjoyed this and want to reach out to me with questions or comments, I'm edd.r on Discord :).
*/
const int spkOne = 12;
const int button[] = {2, 3, 4, 5, 6, 7, 8};
const int startButton = 9;
#define TONE_USE_INT
#define TONE_PITCH 440
#include <TonePitch.h>;
int notes[] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4};
const char *noteNames[] = {"C4", "D4", "E4", "F4", "G4", "A4", "B4"};
int interval;
int canChoose;
int note1;
int note2;
void setup() {
// put your setup code here, to run once:
pinMode(spkOne, OUTPUT);
pinMode(startButton, INPUT_PULLUP);
for(int i = 0; i < sizeof(button) / sizeof(button[0]); i++) {
pinMode(button[i], INPUT_PULLUP);
}
Serial.begin(9600);
canChoose = false;
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(startButton) == LOW && canChoose == false){ // When start button is pressed, select two random notes
Serial.println("Choosing notes now...");
note1 = random(0, 7);
do {
note2 = random(0, 7);
} while (note1 == note2);
Serial.print("Chosen notes: ");
Serial.print(noteNames[note1]);
Serial.print(" and ");
Serial.println(noteNames[note2]);
//play the two randomly chosen notes:
Serial.print("Playing the stored notes: ");
Serial.print(noteNames[note1]);
Serial.print(" and ");
Serial.println(noteNames[note2]);
tone(spkOne, notes[note1], 500);
delay(500);
tone(spkOne, notes[note2], 500);
delay(500);
interval = abs(note1 - note2) + 1;
Serial.print("The interval is: ");
Serial.println(interval);
canChoose = true;
}
if(digitalRead(startButton) == LOW && canChoose == true){
//If you've already received the prompt, play the two randomly chosen notes:
//playing the two randomly chosen notes again
Serial.print("Playing the stored notes again: ");
Serial.print(noteNames[note1]);
Serial.print(" and ");
Serial.println(noteNames[note2]);
tone(spkOne, notes[note1], 500);
delay(500);
tone(spkOne, notes[note2], 500);
delay(500);
}
// If the start button has been pressed, the player can choose an interval. If the interval coincides with the button pressed, a happy tune is played
if(canChoose == true) {
if(digitalRead(button[interval - 1]) == LOW){
Serial.println("Correct!");
//play the two randomly chosen notes:
tone(spkOne, notes[note1], 500);
delay(500);
tone(spkOne, notes[note2], 500);
delay(800);
//celebrate! (C major chord for fun)
tone(spkOne, NOTE_C4, 200);
delay(100);
tone(spkOne, NOTE_E4, 200);
delay(100);
tone(spkOne, NOTE_G4, 200);
delay(100);
tone(spkOne, NOTE_C5, 200);
delay(100);
canChoose = false;
} else {
// Check if wrong button was pressed
bool wrongButtonPressed = false;
for (int i = 0; i < sizeof(button) / sizeof(button[0]); i++) {
if (i != interval - 1 && digitalRead(button[i]) == LOW) {
wrongButtonPressed = true;
break;
}
}
// Play error sound if wrong button pressed
if (wrongButtonPressed) {
Serial.println("Incorrect guess I'm afraid");
tone(spkOne, NOTE_G4, 90);
delay(100);
tone(spkOne, NOTE_C4, 90);
delay(800);
// give the interval again for a new try
tone(spkOne, notes[note1], 500);
delay(500);
tone(spkOne, notes[note2], 500);
delay(800);
}
}
// if(digitalRead(button[]))
}
else {
noTone(spkOne);
// noTone(spkTwo);
// Serial.println("No input detected.");
}
}