Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Rev 1 Created 12/4/2022 plays through the trumpet scale automatically using eighth notes (500 ms duration). The note duration is set by noteDuration
- //Rev 2 Created 12/4/2022 Uses arrays to play through a list of notes instead of spelling each note out in the void loop function
- //Rev 3 Created 12/4/2022 Plays the first section of Raiders March from Indiana Jones, pauses 5 seconds, and then loops
- //Rev 3a On 12/8/2022 I got this code working to play the full stargate song. Note that I ran it on an Arduino Mega using the same pins as the Uno. This isn't the most efficient program because it won't work on the Uno but does on the Mega
- //Rev 3b On 12/8/2022 This was just fixing the sF variable (it was previously a decimal value saved as an int so was really an int)
- //Rev 3c Created on 12/8/2022 This version works on an Uno. I've changed a few things that allow this. The speed factor variable was changed from 2.5 to 2 because it is an integer. The note duration array was changed from a float type to an integer type. The if statement that checks if the void loop count is > the number of array elements was changed from a >= to an > because it was not playing the last note of the array before that change.
- //Rev 3d Created on 12/8/2022 Converts playNote function to a switch..case statement from a large if-else statement
- #include <SoftwareSerial.h> //Needed for FullFunction DFRobotDFPlayerMini library sketch
- #include "Arduino.h" //Needed for FullFunction DFRobotDFPlayerMini library sketch
- #include "DFRobotDFPlayerMini.h" //Needed for FullFunction DFRobotDFPlayerMini library sketch
- #define qn 1000 //quarter note duration is 1000 ms
- #define hn 2000 //half note duration is 2000 ms
- #define wn 4000 //whole note duration is 4000 ms
- #define dhn 3000 //dotted half note duration is 3000 ms
- #define dqn 1500 //dotted quarter note duration is 1500 ms
- #define en 500 //eighth note duration is 500 ms
- #define sn 250 //sixteenth note duration is 250 ms
- #define tsn 125 //thirty-second note duration is 125 ms
- #define wnq 1250 //whole note tied to a quarter note duration is 1250 ms
- enum Notes:byte
- { //Beginning of Notes definition
- N_C4 = 0,
- N_D4 = 1,
- N_E4 = 2,
- N_F4 = 3,
- N_G4 = 4,
- N_A5 = 5,
- N_B5 = 6,
- N_C5 = 7,
- N_D5 = 8,
- N_E5 = 9,
- N_F5 = 10,
- N_G5 = 11,
- N_B5F = 12,
- N_A5F = 13,
- N_SIL = 14,
- N_C5SH = 15,
- N_D5F = 16,
- N_D5SH = 17,
- N_E5F = 18,
- N_G5F = 19,
- N_A6 = 20
- }; //End of Notes definition
- //Now to Use a Software Serial Port to Control DFPlayer Mini
- SoftwareSerial portDFPlayer(10, 11); //Here Arduino Pin D10 is Rx and Arduino Pin D11 is Tx - This section is if using an Arduino Uno or Nano
- //Connect Arduino Pin D10 to DFPlayer Mini Pin Tx through 1 kΩ Resistor
- //Connect Arduino Pin D11 to DFPlayer Mini Pin Rx through 1 kΩ Resistor
- DFRobotDFPlayerMini myDFPlayer; //Defines an Object to Call the DFPlayer Mini with object name myDFPlayer - Needed for FullFunction DFRobotDFPlayerMini library sketch
- void printDetail(uint8_t type, int value); //Needed for FullFunction DFRobotDFPlayerMini library sketch
- int volC=15; //Volume Level returned from VolumeControl function
- String note2Play;
- Notes bArr[93]={N_C4,N_G4,N_G4,N_C5,N_B5F,N_A5F,N_G4,N_A5F,N_G4,N_SIL,N_G4,N_E5,N_D5,N_E5,N_F5,N_D5,N_E5,N_E5,N_F5,N_D5,N_E5,N_E5,N_F5,N_D5,N_E5,N_G5,N_F5,N_E5,N_D5,N_E5,N_D5,N_C5,N_A5F,N_C5,N_A5,N_G4,N_A5,N_B5F,N_G4,N_A5,N_A5,N_B5F,N_G4,N_A5,N_A5,N_B5F,N_G4,N_A5,N_C5,N_B5F,N_A5,N_G4,N_A5,N_C5,N_D5F,N_F5,N_G5F,N_F5,N_D5,N_E5F,N_E5,N_C5SH,N_D5SH,N_E5,N_G5F,N_F5,N_D5,N_E5F,N_E5,N_C5SH,N_D5SH,N_E5,N_D5,N_G5F,N_D5,N_G5F,N_A6,N_A6,N_A6,N_SIL,N_A6,N_A6,N_A6,N_SIL,N_A6,N_A6,N_A6,N_SIL,N_A6,N_A6,N_A6,N_SIL,N_A6}; //Up to section 29 Stargate SG-1 Theme Song - Incorporates firstSectionSG and secondSectionSG and thirdSectionSG and fourthSectionSG from Stargate_Song.ino - Note: In sections 25 & 27 of the Stargate SG-1 Main Theme Song Arranged by Frank Bernaerts there are sixteenth notes which I changed to eighth notes because I don't think the DFPlayer handles sixteenth notes well at the speed I'm running the music at
- #define numArrayElementsS2 92 //Define number of elements in song2 array Stargate Theme Song
- int sF=2; //Speed Factor to Speed Up Song
- int song2D[]={qn/sF,dhn/sF,qn/sF,qn/sF,hn/sF,en/sF,en/sF,en/sF,dhn/sF,hn/sF,hn/sF,dhn/sF,en/sF,en/sF,qn/sF,qn/sF,wnq/sF,en/sF,qn/sF,qn/sF,dqn/sF,en/sF,qn/sF,qn/sF,qn/sF,qn/sF,qn/sF,en/sF,en/sF,wnq/sF,qn/sF,qn/sF,qn/sF,hn/sF,dhn/sF,en/sF,en/sF,qn/sF,qn/sF,wnq/sF,en/sF,qn/sF,qn/sF,dqn/sF,en/sF,qn/sF,qn/sF,qn/sF,qn/sF,qn/sF,en/sF,en/sF,qn/sF,qn/sF,qn/sF,qn/sF,qn/sF,hn/sF,en/sF,en/sF,qn/sF,qn/sF,qn/sF,qn/sF,qn/sF,hn/sF,en/sF,en/sF,qn/sF,qn/sF,qn/sF,qn/sF,hn/sF,hn/sF,hn/sF,hn/sF,qn/sF,qn/sF,qn/sF,en/sF,qn/sF,qn/sF,qn/sF,en/sF,en/sF,en/sF,en/sF,en/sF,en/sF,en/sF,en/sF,en/sF,qn/sF};
- int voidLoopCnt=0; //Keeps track of iterations of void loop
- #define noteDuration 500
- void setup() {
- Serial.begin(9600); // Serial port to computer
- portDFPlayer.begin(9600); //Start Serial Port to DFPlayer Mini
- //The rest of the setup code comes from FullFunction DFRobotDFPlayerMini library sketch
- Serial.println();
- Serial.println(F("DFRobot DFPlayer Mini Demo"));
- Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
- if (!myDFPlayer.begin(portDFPlayer)) { //Use softwareSerial to communicate with mp3.
- Serial.println(F("Unable to begin:"));
- Serial.println(F("1.Please recheck the connection!"));
- Serial.println(F("2.Please insert the SD card!"));
- while(true);
- }
- Serial.println(F("DFPlayer Mini online."));
- myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
- //----Set volume----
- myDFPlayer.volume(volC); //Set volume value (0~30).
- myDFPlayer.volumeUp(); //Volume Up
- myDFPlayer.volumeDown(); //Volume Down
- //----Set different EQ----
- myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
- //----Set device we use SD as default----
- myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
- //----Read imformation----
- Serial.println(myDFPlayer.readState()); //read mp3 state
- Serial.println(myDFPlayer.readVolume()); //read current volume
- Serial.println(myDFPlayer.readEQ()); //read EQ setting
- Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
- Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
- Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03
- // Serial.print("Size of Note Array: ");
- // Serial.println(sizeof(song2)/sizeof(song2[0]));
- // Serial.print("Size of Note Duration Array: ");
- // Serial.println(sizeof(song2D)/sizeof(song2D[0]));
- } //End of void setup
- void loop() {
- Serial.print(bArr[voidLoopCnt]);
- Serial.print(" voidLoopCnt: ");
- Serial.print(voidLoopCnt);
- Serial.println("");
- //playNote(bArr[voidLoopCnt]);
- myDFPlayer.playMp3Folder(bArr[voidLoopCnt]);
- delay(song2D[voidLoopCnt]);
- voidLoopCnt++; //Increment the void loop counter so that the next note is played on the next iteration
- if (voidLoopCnt > numArrayElementsS2)
- {
- myDFPlayer.pause(); //pause the mp3
- delay(5000);
- voidLoopCnt=0; //Set the loop counter back to zero so it starts over
- }
- }//End of void loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement