r/ArduinoHelp • u/Electrical-Rate-5674 • 40m ago
Arduino DFPlayer Zelda OOT/MM coding project help
Hello I am currently working on a dfplayer project, that plays the ocarina songs from zelda OOT & MM. I started this project months ago and I thought it was time for an upgrade, so I was gonna move off the connection to my PC and to a PCB. However, before that I wanted to fix one major issue...the code. Months ago I did not think much of the code bc I had it generated through chatgpt but then for this is upgrade of the project, I thought I should make most of the code myself instead of relying on LLM to do it for me. well I attempted it with ideas I had to improve and clean the code, however that caused me to hit a wall. I would like some hard critiquing guidance on my mistakes and what I can learn so I can do better. (I'm still rusty and practicing with arduino so some of what I wrote might be very wrong)
Here the changes I made so there is a better picture of what I am talking about:
//new#
void checkInput(int buttonPressed) {//function of checking of which button was pressed
// 🔊 Play note
dfplayer.play(buttonPressed + 1);//tells the dfplayer to play corresponding sound to the button
delay(250);//pauses for 250 miliseconds, to give time for the next note to play
// Store input
inputSequence[inputIndex] = buttonPressed;//remembers the button sequence by storing the pressed button's index in the current array position
inputIndex++;//increases the index by one, so the program knows how many notes were entered
bool match++;//I replace the repeating Booleans with a single line that add the matches but not sure if this would work
}
for (int i = 0; i < inputIndex; i++) {//a for loop to examine every note/button entered by the user
if (i >= song_len || inputSequence[i] != song[i]++) match = false;//I decide to use a single line that loops with the function
}
// ===== FULL MATCHES =====
if (match1 && inputIndex == song1_len) {
dfplayer.play(27);
delay(1500);
dfplayer.play(song_track++); resetInput(); return; }//checks if both sequence and sequence length match the song, if its correct it tells the dfplayer to play the correct sound followed by a 1.5 sec delay, tells the dfplayer to play the song, calls the resetInput() function, return exits the checkInput function
// ❌ WRONG INPUT
if (!match++) {
// dfplayer.play(28); // optional error
resetInput();//calls the resetInput() function and clears current sequence
return;//exits the current function
}
//old#2
void checkInput(int buttonPressed) {//function of checking of which button was pressed
// 🔊 Play note
dfplayer.play(buttonPressed + 1);//tells the dfplayer to play corresponing sound to the button
delay(250);//pauses for 250 miliseconds, to give time for the next note to play
// Store input
inputSequence[inputIndex] = buttonPressed;//remembers the button sequence by storing the pressed button's index in the current array position
inputIndex++;//increases the index by one, so the program knows how many notes were entered
// ===== MATCH FLAGS =====
//th boolean variables are used to deterime if the current sequence matches a song
bool match1 = true;
bool match2 = true;
bool match3 = true;
bool match4 = true;
bool match5 = true;
bool match6 = true;
bool match7 = true;
bool match8 = true;
bool match9 = true;
bool match10 = true;
bool match11 = true;
bool match12 = true;
bool match13 = true;
bool match14 = true;
bool match15 = true;
bool match16 = true;
bool match17 = true;
bool match18 = true;
bool match19 = true;
bool match20 = true;
bool match21 = true;
for (int i = 0; i < inputIndex; i++) {//a for loop to examine every note/button entered by the user
if (i >= song1_len || inputSequence[i] != song1[i]) match1 = false;// i >= song1_len checks if the input goes beyond the length of the song, inputSequence[i] != song1[i] checks if the user's differs from the note expectd by the song
if (i >= song2_len || inputSequence[i] != song2[i]) match2 = false;
if (i >= song3_len || inputSequence[i] != song3[i]) match3 = false;
if (i >= song4_len || inputSequence[i] != song4[i]) match4 = false;
if (i >= song5_len || inputSequence[i] != song5[i]) match5 = false;
if (i >= song6_len || inputSequence[i] != song6[i]) match6 = false;
if (i >= song7_len || inputSequence[i] != song7[i]) match7 = false;
if (i >= song8_len || inputSequence[i] != song8[i]) match8 = false;
if (i >= song9_len || inputSequence[i] != song9[i]) match9 = false;
if (i >= song10_len || inputSequence[i] != song10[i]) match10 = false;
if (i >= song11_len || inputSequence[i] != song11[i]) match11 = false;
if (i >= song12_len || inputSequence[i] != song12[i]) match12 = false;
if (i >= song13_len || inputSequence[i] != song13[i]) match13 = false;
if (i >= song14_len || inputSequence[i] != song14[i]) match14 = false;
if (i >= song15_len || inputSequence[i] != song15[i]) match15 = false;
if (i >= song16_len || inputSequence[i] != song16[i]) match16 = false;
if (i >= song17_len || inputSequence[i] != song17[i]) match17 = false;
if (i >= song18_len || inputSequence[i] != song18[i]) match18 = false;
if (i >= song19_len || inputSequence[i] != song19[i]) match19 = false;
if (i >= song20_len || inputSequence[i] != song20[i]) match20 = false;
if (i >= song21_len || inputSequence[i] != song21[i]) match21 = false;
}
// ===== FULL MATCHES =====
if (match1 && inputIndex == song1_len) { dfplayer.play(27); delay(1500); dfplayer.play(song1_track); resetInput(); return; }//checks if both sequence and sequence length match the song, if its correct it tells the dfplayer to play the correct sound followed by a 1.5 sec delay, tells the dfplayer to play the song, calls the resetInput() function, return exits the checkInput function
if (match2 && inputIndex == song2_len) { dfplayer.play(27); delay(1500); dfplayer.play(song2_track); resetInput(); return; }
if (match3 && inputIndex == song3_len) { dfplayer.play(27); delay(1500); dfplayer.play(song3_track); resetInput(); return; }
if (match4 && inputIndex == song4_len) { dfplayer.play(27); delay(1500); dfplayer.play(song4_track); resetInput(); return; }
if (match5 && inputIndex == song5_len) { dfplayer.play(27); delay(1500); dfplayer.play(song5_track); resetInput(); return; }
if (match6 && inputIndex == song6_len) { dfplayer.play(27); delay(1500); dfplayer.play(song6_track); resetInput(); return; }
if (match7 && inputIndex == song7_len) { dfplayer.play(27); delay(1500); dfplayer.play(song7_track); resetInput(); return; }
if (match8 && inputIndex == song8_len) { dfplayer.play(27); delay(1500); dfplayer.play(song8_track); resetInput(); return; }
if (match9 && inputIndex == song9_len) { dfplayer.play(27); delay(1500); dfplayer.play(song9_track); resetInput(); return; }
if (match10 && inputIndex == song10_len) { dfplayer.play(27); delay(1500); dfplayer.play(song10_track); resetInput(); return; }
if (match11 && inputIndex == song11_len) { dfplayer.play(27); delay(1500); dfplayer.play(song11_track); resetInput(); return; }
if (match12 && inputIndex == song12_len) { dfplayer.play(27); delay(1500); dfplayer.play(song12_track); resetInput(); return; }
if (match13 && inputIndex == song13_len) { dfplayer.play(27); delay(1500); dfplayer.play(song13_track); resetInput(); return; }
if (match14 && inputIndex == song14_len) { dfplayer.play(27); delay(1500); dfplayer.play(song14_track); resetInput(); return; }
if (match15 && inputIndex == song15_len) { dfplayer.play(27); delay(1500); dfplayer.play(song15_track); resetInput(); return; }
if (match16 && inputIndex == song16_len) { dfplayer.play(27); delay(1500); dfplayer.play(song16_track); resetInput(); return; }
if (match17 && inputIndex == song17_len) { dfplayer.play(27); delay(1500); dfplayer.play(song17_track); resetInput(); return; }
if (match18 && inputIndex == song18_len) { dfplayer.play(27); delay(1500); dfplayer.play(song18_track); resetInput(); return; }
if (match19 && inputIndex == song19_len) { dfplayer.play(27); delay(1500); dfplayer.play(song19_track); resetInput(); return; }
if (match20 && inputIndex == song20_len) { dfplayer.play(27); delay(1500); dfplayer.play(song20_track); resetInput(); return; }
if (match21 && inputIndex == song21_len) { dfplayer.play(27); delay(1500); dfplayer.play(song21_track); resetInput(); return; }
// ❌ WRONG INPUT
if (!match1 && !match2 && !match3 && !match4 && !match5 &&
!match6 && !match7 && !match8 && !match9 && !match10 &&
!match11 && !match12 && !match13 && !match14 && !match15 &&
!match16 && !match17 && !match18 && !match19 && !match20 &&
!match21) {//checks if the sequence is correct
// dfplayer.play(28); // optional error
resetInput();//calls the resetInput() function and clears current sequence
return;//exits the current function
}
//new#1
for(int i = 0; i < inputSequence[MAX_LEN]; i++){//idk if this part will work, but its supposed to the arrays, the length of each array, and the which number file to play, I was trying to see if a for loop would do it but now i am not sure
int song[] = {};
int song_len++;
int song_track++;
}
//old#1
// Zelda's Lullaby: B D2 A B D2 A
int song1[] = {1, 3, 0, 1, 3, 0};
int song1_len = 6;
int song1_track = 6;
// Epona's Song: D2 B A D2 B A
int song2[] = {3, 1, 0, 3, 1, 0};
int song2_len = 6;
int song2_track = 7;
// Saria's Song: F A B F A B
int song3[] = {4, 0, 1, 4, 0, 1};
int song3_len = 6;
int song3_track = 8;
// Sun's Song: A F D2 A F D2
int song4[] = {0, 4, 3, 0, 4, 3};
int song4_len = 6;
int song4_track = 9;
// Song of Time: A D F A D F
int song5[] = {0, 2, 4, 0, 2, 4};
int song5_len = 6;
int song5_track = 10;
// Song of Storms: D F D2 D F D2
int song6[] = {2, 4, 3, 2, 4, 3};
int song6_len = 6;
int song6_track = 11;
etc.