JonD1988

AutoTrumpet Rev 3a

Dec 7th, 2022
1,512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //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
  2. //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
  3. //Rev 3 Created 12/4/2022 Plays the first section of Raiders March from Indiana Jones, pauses 5 seconds, and then loops
  4. #include <SoftwareSerial.h> //Needed for FullFunction DFRobotDFPlayerMini library sketch
  5. #include "Arduino.h" //Needed for FullFunction DFRobotDFPlayerMini library sketch
  6. #include "DFRobotDFPlayerMini.h" //Needed for FullFunction DFRobotDFPlayerMini library sketch
  7.  
  8. #define qn 1000 //quarter note duration is 1000 ms
  9. #define hn 2000 //half note duration is 2000 ms
  10. #define wn 4000 //whole note duration is 4000 ms
  11. #define dhn 3000 //dotted half note duration is 3000 ms
  12. #define dqn 1500 //dotted quarter note duration is 1500 ms
  13. #define en 500 //eighth note duration is 500 ms
  14. #define sn 250 //sixteenth note duration is 250 ms
  15. #define tsn 125 //thirty-second note duration is 125 ms
  16. #define wnq 1250 //whole note tied to a quarter note duration is 1250 ms
  17.  
  18. //Now to Use a Software Serial Port to Control DFPlayer Mini
  19. SoftwareSerial portDFPlayer(10, 11); //Here Arduino Pin D10 is Rx and Arduino Pin D11 is Tx
  20. //Connect Arduino Pin D10 to DFPlayer Mini Pin Tx through 1 kΩ Resistor
  21. //Connect Arduino Pin D11 to DFPlayer Mini Pin Rx through 1 kΩ Resistor
  22. DFRobotDFPlayerMini myDFPlayer; //Defines an Object to Call the DFPlayer Mini with object name myDFPlayer - Needed for FullFunction DFRobotDFPlayerMini library sketch
  23. void printDetail(uint8_t type, int value); //Needed for FullFunction DFRobotDFPlayerMini library sketch
  24. int volC=15; //Volume Level returned from VolumeControl function
  25. String note2Play;
  26. 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
  27. #define numArrayElementsS2 92 //Define number of elements in song2 array Stargate Theme Song
  28. int sF=2.5; //Speed Factor to Speed Up Song
  29. 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};
  30. //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};
  31. int voidLoopCnt=0; //Keeps track of iterations of void loop
  32.  
  33. #define noteDuration 500
  34.  
  35. void setup() {
  36.   Serial.begin(9600);             // Serial port to computer
  37.  
  38.   portDFPlayer.begin(9600);            //Start Serial Port to DFPlayer Mini
  39.  
  40.   //The rest of the setup code comes from FullFunction DFRobotDFPlayerMini library sketch
  41.   Serial.println();
  42.   Serial.println(F("DFRobot DFPlayer Mini Demo"));
  43.   Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  44.  
  45.   if (!myDFPlayer.begin(portDFPlayer)) {  //Use softwareSerial to communicate with mp3.
  46.     Serial.println(F("Unable to begin:"));
  47.     Serial.println(F("1.Please recheck the connection!"));
  48.     Serial.println(F("2.Please insert the SD card!"));
  49.     while(true);
  50.   }
  51.   Serial.println(F("DFPlayer Mini online."));
  52.  
  53.   myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
  54.  
  55.   //----Set volume----
  56.   myDFPlayer.volume(volC);  //Set volume value (0~30).
  57.   myDFPlayer.volumeUp(); //Volume Up
  58.   myDFPlayer.volumeDown(); //Volume Down
  59.  
  60.   //----Set different EQ----
  61.   myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
  62.  
  63.   //----Set device we use SD as default----
  64.   myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
  65.  
  66.   //----Read imformation----
  67.   Serial.println(myDFPlayer.readState()); //read mp3 state
  68.   Serial.println(myDFPlayer.readVolume()); //read current volume
  69.   Serial.println(myDFPlayer.readEQ()); //read EQ setting
  70.   Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
  71.   Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
  72.   Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03
  73.  
  74. } //End of void setup
  75.  
  76. void loop() {
  77.  
  78.   Serial.print(song2[voidLoopCnt]);
  79.   Serial.print(" voidLoopCnt: ");
  80.   Serial.print(voidLoopCnt);
  81.   Serial.println("");
  82.   playNote(song2[voidLoopCnt]);
  83.   delay(song2D[voidLoopCnt]);
  84.   voidLoopCnt++; //Increment the void loop counter so that the next note is played on the next iteration
  85.  
  86.   if (voidLoopCnt >= numArrayElementsS2)
  87.   {
  88.     myDFPlayer.pause();  //pause the mp3
  89.     delay(5000);
  90.     voidLoopCnt=0; //Set the loop counter back to zero so it starts over
  91.   }
  92. }//End of void loop
  93.  
  94. void playNote(String note2Play)
  95. {
  96.   if (note2Play=="c4")
  97.   {
  98.      myDFPlayer.playMp3Folder(0); //play specific mp3 in SD:/MP3/0000.mp3; File Name(0~65535)
  99.   }
  100.   else if (note2Play=="d4")
  101.   {
  102.     myDFPlayer.playMp3Folder(1); //play specific mp3 in SD:/MP3/0001.mp3; File Name(0~65535)
  103.   }
  104.   else if (note2Play=="e4")
  105.   {
  106.     myDFPlayer.playMp3Folder(2); //play specific mp3 in SD:/MP3/0002.mp3; File Name(0~65535)
  107.   }
  108.   else if (note2Play=="f4")
  109.   {
  110.     myDFPlayer.playMp3Folder(3); //play specific mp3 in SD:/MP3/0003.mp3; File Name(0~65535)
  111.   }
  112.   else if (note2Play=="g4")
  113.   {
  114.     myDFPlayer.playMp3Folder(4); //play specific mp3 in SD:/MP3/0004.mp3; File Name(0~65535)
  115.   }
  116.   else if (note2Play=="a5")
  117.   {
  118.     myDFPlayer.playMp3Folder(5); //play specific mp3 in SD:/MP3/0005.mp3; File Name(0~65535)
  119.   }
  120.   else if (note2Play=="b5")
  121.   {
  122.     myDFPlayer.playMp3Folder(6); //play specific mp3 in SD:/MP3/0006.mp3; File Name(0~65535)
  123.   }
  124.   else if (note2Play=="c5")
  125.   {
  126.     myDFPlayer.playMp3Folder(7); //play specific mp3 in SD:/MP3/0007.mp3; File Name(0~65535)
  127.   }
  128.   else if (note2Play=="d5")
  129.   {
  130.     myDFPlayer.playMp3Folder(8); //play specific mp3 in SD:/MP3/0008.mp3; File Name(0~65535)
  131.   }
  132.   else if (note2Play=="e5")
  133.   {
  134.     myDFPlayer.playMp3Folder(9); //play specific mp3 in SD:/MP3/0009.mp3; File Name(0~65535)
  135.   }
  136.   else if (note2Play=="f5")
  137.   {
  138.     myDFPlayer.playMp3Folder(10); //play specific mp3 in SD:/MP3/0010.mp3; File Name(0~65535)
  139.   }
  140.   else if (note2Play=="g5")
  141.   {
  142.     myDFPlayer.playMp3Folder(11); //play specific mp3 in SD:/MP3/0011.mp3; File Name(0~65535)
  143.   }
  144.   else if (note2Play=="b5f")
  145.   {
  146.     myDFPlayer.playMp3Folder(12); //play specific mp3 in SD:/MP3/0012.mp3; File Name(0~65535)
  147.   }
  148.   else if (note2Play=="a5f")
  149.   {
  150.     myDFPlayer.playMp3Folder(13); //play specific mp3 in SD:/MP3/0013.mp3; File Name(0~65535)
  151.   }
  152.   else if (note2Play=="sil")
  153.   {
  154.     myDFPlayer.playMp3Folder(14); //play specific mp3 in SD:/MP3/0014.mp3; File Name(0~65535)
  155.   }
  156.   else if (note2Play=="c5sh")
  157.   {
  158.     myDFPlayer.playMp3Folder(15); //play specific mp3 in SD:/MP3/0015.mp3; File Name(0~65535)
  159.   }
  160.   else if (note2Play=="d5f")
  161.   {
  162.     myDFPlayer.playMp3Folder(16); //play specific mp3 in SD:/MP3/0016.mp3; File Name(0~65535)
  163.   }
  164.   else if (note2Play=="d5sh")
  165.   {
  166.     myDFPlayer.playMp3Folder(17); //play specific mp3 in SD:/MP3/0017.mp3; File Name(0~65535)
  167.   }
  168.   else if (note2Play=="e5f")
  169.   {
  170.     myDFPlayer.playMp3Folder(18); //play specific mp3 in SD:/MP3/0018.mp3; File Name(0~65535)
  171.   }
  172.   else if (note2Play=="g5f")
  173.   {
  174.     myDFPlayer.playMp3Folder(19); //play specific mp3 in SD:/MP3/0019.mp3; File Name(0~65535)
  175.   }
  176.   else if (note2Play=="a6")
  177.   {
  178.     myDFPlayer.playMp3Folder(20); //play specific mp3 in SD:/MP3/0020.mp3; File Name(0~65535)
  179.   }
  180. } //End of playNote function definition
Advertisement
Add Comment
Please, Sign In to add comment