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
- #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
- //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
- //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;
- const String song2[]={"c4","g4","g4","c5","b5f","a5f","g4","a5f","g4","sil","g4","e5","d5","e5","f5","d5","e5","e5","f5","d5","e5","e5","f5","d5","e5","g5","f5","e5","d5","e5","d5","c5","a5f","c5","a5","g4","a5","b5f","g4","a5","a5","b5f","g4","a5","a5","b5f","g4","a5","c5","b5f","a5","g4","a5","c5","d5f","f5","g5f","f5","d5","e5f","e5","c5sh","d5sh","e5","g5f","f5","d5","e5f","e5","c5sh","d5sh","e5","d5","g5f","d5","g5f","a6","a6","a6","sil","a6","a6","a6","sil","a6","a6","a6","sil","a6","a6","a6","sil","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.5; //Speed Factor to Speed Up Song
- float 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};
- //float song2D[]={wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/sF,wn/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
- } //End of void setup
- void loop() {
- Serial.print(song2[voidLoopCnt]);
- Serial.print(" voidLoopCnt: ");
- Serial.print(voidLoopCnt);
- Serial.println("");
- playNote(song2[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
- void playNote(String note2Play)
- {
- if (note2Play=="c4")
- {
- myDFPlayer.playMp3Folder(0); //play specific mp3 in SD:/MP3/0000.mp3; File Name(0~65535)
- }
- else if (note2Play=="d4")
- {
- myDFPlayer.playMp3Folder(1); //play specific mp3 in SD:/MP3/0001.mp3; File Name(0~65535)
- }
- else if (note2Play=="e4")
- {
- myDFPlayer.playMp3Folder(2); //play specific mp3 in SD:/MP3/0002.mp3; File Name(0~65535)
- }
- else if (note2Play=="f4")
- {
- myDFPlayer.playMp3Folder(3); //play specific mp3 in SD:/MP3/0003.mp3; File Name(0~65535)
- }
- else if (note2Play=="g4")
- {
- myDFPlayer.playMp3Folder(4); //play specific mp3 in SD:/MP3/0004.mp3; File Name(0~65535)
- }
- else if (note2Play=="a5")
- {
- myDFPlayer.playMp3Folder(5); //play specific mp3 in SD:/MP3/0005.mp3; File Name(0~65535)
- }
- else if (note2Play=="b5")
- {
- myDFPlayer.playMp3Folder(6); //play specific mp3 in SD:/MP3/0006.mp3; File Name(0~65535)
- }
- else if (note2Play=="c5")
- {
- myDFPlayer.playMp3Folder(7); //play specific mp3 in SD:/MP3/0007.mp3; File Name(0~65535)
- }
- else if (note2Play=="d5")
- {
- myDFPlayer.playMp3Folder(8); //play specific mp3 in SD:/MP3/0008.mp3; File Name(0~65535)
- }
- else if (note2Play=="e5")
- {
- myDFPlayer.playMp3Folder(9); //play specific mp3 in SD:/MP3/0009.mp3; File Name(0~65535)
- }
- else if (note2Play=="f5")
- {
- myDFPlayer.playMp3Folder(10); //play specific mp3 in SD:/MP3/0010.mp3; File Name(0~65535)
- }
- else if (note2Play=="g5")
- {
- myDFPlayer.playMp3Folder(11); //play specific mp3 in SD:/MP3/0011.mp3; File Name(0~65535)
- }
- else if (note2Play=="b5f")
- {
- myDFPlayer.playMp3Folder(12); //play specific mp3 in SD:/MP3/0012.mp3; File Name(0~65535)
- }
- else if (note2Play=="a5f")
- {
- myDFPlayer.playMp3Folder(13); //play specific mp3 in SD:/MP3/0013.mp3; File Name(0~65535)
- }
- else if (note2Play=="sil")
- {
- myDFPlayer.playMp3Folder(14); //play specific mp3 in SD:/MP3/0014.mp3; File Name(0~65535)
- }
- else if (note2Play=="c5sh")
- {
- myDFPlayer.playMp3Folder(15); //play specific mp3 in SD:/MP3/0015.mp3; File Name(0~65535)
- }
- else if (note2Play=="d5f")
- {
- myDFPlayer.playMp3Folder(16); //play specific mp3 in SD:/MP3/0016.mp3; File Name(0~65535)
- }
- else if (note2Play=="d5sh")
- {
- myDFPlayer.playMp3Folder(17); //play specific mp3 in SD:/MP3/0017.mp3; File Name(0~65535)
- }
- else if (note2Play=="e5f")
- {
- myDFPlayer.playMp3Folder(18); //play specific mp3 in SD:/MP3/0018.mp3; File Name(0~65535)
- }
- else if (note2Play=="g5f")
- {
- myDFPlayer.playMp3Folder(19); //play specific mp3 in SD:/MP3/0019.mp3; File Name(0~65535)
- }
- else if (note2Play=="a6")
- {
- myDFPlayer.playMp3Folder(20); //play specific mp3 in SD:/MP3/0020.mp3; File Name(0~65535)
- }
- } //End of playNote function definition
Advertisement
Add Comment
Please, Sign In to add comment