Advertisement
JonD1988

Trumpet_Organ_Rev_3dFixed

Dec 8th, 2022
1,193
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. //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
  5. //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)
  6. //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.
  7. //Rev 3d Created on 12/8/2022 Converts playNote function to a switch..case statement from a large if-else statement
  8. #include <SoftwareSerial.h> //Needed for FullFunction DFRobotDFPlayerMini library sketch
  9. #include "Arduino.h" //Needed for FullFunction DFRobotDFPlayerMini library sketch
  10. #include "DFRobotDFPlayerMini.h" //Needed for FullFunction DFRobotDFPlayerMini library sketch
  11.  
  12. #define qn 1000 //quarter note duration is 1000 ms
  13. #define hn 2000 //half note duration is 2000 ms
  14. #define wn 4000 //whole note duration is 4000 ms
  15. #define dhn 3000 //dotted half note duration is 3000 ms
  16. #define dqn 1500 //dotted quarter note duration is 1500 ms
  17. #define en 500 //eighth note duration is 500 ms
  18. #define sn 250 //sixteenth note duration is 250 ms
  19. #define tsn 125 //thirty-second note duration is 125 ms
  20. #define wnq 1250 //whole note tied to a quarter note duration is 1250 ms
  21.  
  22. enum Notes:byte
  23. { //Beginning of Notes definition
  24.   N_C4 = 0,
  25.   N_D4 = 1,
  26.   N_E4 = 2,
  27.   N_F4 = 3,
  28.   N_G4 = 4,
  29.   N_A5 = 5,
  30.   N_B5 = 6,
  31.   N_C5 = 7,
  32.   N_D5 = 8,
  33.   N_E5 = 9,
  34.   N_F5 = 10,
  35.   N_G5 = 11,
  36.   N_B5F = 12,
  37.   N_A5F = 13,
  38.   N_SIL = 14,
  39.   N_C5SH = 15,
  40.   N_D5F = 16,
  41.   N_D5SH = 17,
  42.   N_E5F = 18,
  43.   N_G5F = 19,
  44.   N_A6 = 20
  45. }; //End of Notes definition
  46.  
  47. //Now to Use a Software Serial Port to Control DFPlayer Mini
  48. 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
  49. //Connect Arduino Pin D10 to DFPlayer Mini Pin Tx through 1 kΩ Resistor
  50. //Connect Arduino Pin D11 to DFPlayer Mini Pin Rx through 1 kΩ Resistor
  51. DFRobotDFPlayerMini myDFPlayer; //Defines an Object to Call the DFPlayer Mini with object name myDFPlayer - Needed for FullFunction DFRobotDFPlayerMini library sketch
  52. void printDetail(uint8_t type, int value); //Needed for FullFunction DFRobotDFPlayerMini library sketch
  53. int volC=15; //Volume Level returned from VolumeControl function
  54. String note2Play;
  55. 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
  56. #define numArrayElementsS2 92 //Define number of elements in song2 array Stargate Theme Song
  57. int sF=2; //Speed Factor to Speed Up Song
  58. 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};
  59. int voidLoopCnt=0; //Keeps track of iterations of void loop
  60.  
  61. #define noteDuration 500
  62.  
  63. void setup() {
  64.   Serial.begin(9600);             // Serial port to computer
  65.  
  66.   portDFPlayer.begin(9600);            //Start Serial Port to DFPlayer Mini
  67.  
  68.   //The rest of the setup code comes from FullFunction DFRobotDFPlayerMini library sketch
  69.   Serial.println();
  70.   Serial.println(F("DFRobot DFPlayer Mini Demo"));
  71.   Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  72.  
  73.   if (!myDFPlayer.begin(portDFPlayer)) {  //Use softwareSerial to communicate with mp3.
  74.     Serial.println(F("Unable to begin:"));
  75.     Serial.println(F("1.Please recheck the connection!"));
  76.     Serial.println(F("2.Please insert the SD card!"));
  77.     while(true);
  78.   }
  79.   Serial.println(F("DFPlayer Mini online."));
  80.  
  81.   myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
  82.  
  83.   //----Set volume----
  84.   myDFPlayer.volume(volC);  //Set volume value (0~30).
  85.   myDFPlayer.volumeUp(); //Volume Up
  86.   myDFPlayer.volumeDown(); //Volume Down
  87.  
  88.   //----Set different EQ----
  89.   myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
  90.  
  91.   //----Set device we use SD as default----
  92.   myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
  93.  
  94.   //----Read imformation----
  95.   Serial.println(myDFPlayer.readState()); //read mp3 state
  96.   Serial.println(myDFPlayer.readVolume()); //read current volume
  97.   Serial.println(myDFPlayer.readEQ()); //read EQ setting
  98.   Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
  99.   Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
  100.   Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03
  101.  
  102. //  Serial.print("Size of Note Array: ");
  103. //  Serial.println(sizeof(song2)/sizeof(song2[0]));
  104. //  Serial.print("Size of Note Duration Array: ");
  105. //  Serial.println(sizeof(song2D)/sizeof(song2D[0]));
  106.  
  107. } //End of void setup
  108.  
  109. void loop() {
  110.  
  111.   Serial.print(bArr[voidLoopCnt]);
  112.   Serial.print(" voidLoopCnt: ");
  113.   Serial.print(voidLoopCnt);
  114.   Serial.println("");
  115.   //playNote(bArr[voidLoopCnt]);
  116.   myDFPlayer.playMp3Folder(bArr[voidLoopCnt]);
  117.   delay(song2D[voidLoopCnt]);
  118.   voidLoopCnt++; //Increment the void loop counter so that the next note is played on the next iteration
  119.  
  120.   if (voidLoopCnt > numArrayElementsS2)
  121.   {
  122.     myDFPlayer.pause();  //pause the mp3
  123.     delay(5000);
  124.     voidLoopCnt=0; //Set the loop counter back to zero so it starts over
  125.   }
  126. }//End of void loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement