Advertisement
JonD1988

Trumpet_Organ_Rev_3d

Dec 8th, 2022
1,227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.19 KB | None | 0 0
  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. enum Notes:byte
  48. { //Beginning of Notes definition
  49.   N_C4 = 0,
  50.   N_D4,
  51.   N_E4,
  52.   N_F4,
  53.   N_G4,
  54.   N_A5,
  55.   N_B5,
  56.   N_C5,
  57.   N_D5,
  58.   N_E5,
  59.   N_F5,
  60.   N_G5,
  61.   N_B5F,
  62.   N_A5F,
  63.   N_SIL,
  64.   N_C5SH,
  65.   N_D5F,
  66.   N_D5SH,
  67.   N_E5F,
  68.   N_G5F,
  69.   N_A6
  70. }; //End of Notes definition
  71.  
  72. //Now to Use a Software Serial Port to Control DFPlayer Mini
  73. 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
  74. //Connect Arduino Pin D10 to DFPlayer Mini Pin Tx through 1 kΩ Resistor
  75. //Connect Arduino Pin D11 to DFPlayer Mini Pin Rx through 1 kΩ Resistor
  76. DFRobotDFPlayerMini myDFPlayer; //Defines an Object to Call the DFPlayer Mini with object name myDFPlayer - Needed for FullFunction DFRobotDFPlayerMini library sketch
  77. void printDetail(uint8_t type, int value); //Needed for FullFunction DFRobotDFPlayerMini library sketch
  78. int volC=15; //Volume Level returned from VolumeControl function
  79. String note2Play;
  80. //const String song2[]={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
  81. Notes bArr[92]={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
  82. #define numArrayElementsS2 92 //Define number of elements in song2 array Stargate Theme Song
  83. int sF=2; //Speed Factor to Speed Up Song
  84. 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};
  85. int voidLoopCnt=0; //Keeps track of iterations of void loop
  86.  
  87. #define noteDuration 500
  88.  
  89. void setup() {
  90.   Serial.begin(9600);             // Serial port to computer
  91.  
  92.   portDFPlayer.begin(9600);            //Start Serial Port to DFPlayer Mini
  93.  
  94.   //The rest of the setup code comes from FullFunction DFRobotDFPlayerMini library sketch
  95.   Serial.println();
  96.   Serial.println(F("DFRobot DFPlayer Mini Demo"));
  97.   Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  98.  
  99.   if (!myDFPlayer.begin(portDFPlayer)) {  //Use softwareSerial to communicate with mp3.
  100.     Serial.println(F("Unable to begin:"));
  101.     Serial.println(F("1.Please recheck the connection!"));
  102.     Serial.println(F("2.Please insert the SD card!"));
  103.     while(true);
  104.   }
  105.   Serial.println(F("DFPlayer Mini online."));
  106.  
  107.   myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
  108.  
  109.   //----Set volume----
  110.   myDFPlayer.volume(volC);  //Set volume value (0~30).
  111.   myDFPlayer.volumeUp(); //Volume Up
  112.   myDFPlayer.volumeDown(); //Volume Down
  113.  
  114.   //----Set different EQ----
  115.   myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
  116.  
  117.   //----Set device we use SD as default----
  118.   myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
  119.  
  120.   //----Read imformation----
  121.   Serial.println(myDFPlayer.readState()); //read mp3 state
  122.   Serial.println(myDFPlayer.readVolume()); //read current volume
  123.   Serial.println(myDFPlayer.readEQ()); //read EQ setting
  124.   Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
  125.   Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
  126.   Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03
  127.  
  128.   Serial.print("Size of Note Array: ");
  129.   Serial.println(sizeof(song2)/sizeof(song2[0]));
  130.   Serial.print("Size of Note Duration Array: ");
  131.   Serial.println(sizeof(song2D)/sizeof(song2D[0]));
  132.  
  133. } //End of void setup
  134.  
  135. void loop() {
  136.  
  137.   Serial.print(bArr[voidLoopCnt]);
  138.   Serial.print(" voidLoopCnt: ");
  139.   Serial.print(voidLoopCnt);
  140.   Serial.println("");
  141.   playNote(bArr[voidLoopCnt]);
  142.   delay(song2D[voidLoopCnt]);
  143.   voidLoopCnt++; //Increment the void loop counter so that the next note is played on the next iteration
  144.  
  145.   if (voidLoopCnt > numArrayElementsS2)
  146.   {
  147.     myDFPlayer.pause();  //pause the mp3
  148.     delay(5000);
  149.     voidLoopCnt=0; //Set the loop counter back to zero so it starts over
  150.   }
  151. }//End of void loop
  152.  
  153. void playNote(int i)
  154. {
  155.   switch (bArr[i])
  156.   { //Beginning of switch...case statement
  157.     case N_C4:
  158.       myDFPlayer.playMp3Folder(0); //play specific mp3 in SD:/MP3/0000.mp3; File Name(0~65535)
  159.       break;
  160.     case N_D4:
  161.       myDFPlayer.playMp3Folder(1); //play specific mp3 in SD:/MP3/0001.mp3; File Name(0~65535)
  162.       break;
  163.     case N_E4:
  164.       myDFPlayer.playMp3Folder(2); //play specific mp3 in SD:/MP3/0002.mp3; File Name(0~65535)
  165.       break;
  166.     case N_F4:
  167.       myDFPlayer.playMp3Folder(3); //play specific mp3 in SD:/MP3/0003.mp3; File Name(0~65535)
  168.       break;
  169.     case N_G4:
  170.       myDFPlayer.playMp3Folder(4); //play specific mp3 in SD:/MP3/0004.mp3; File Name(0~65535)
  171.       break;
  172.     case N_A5:
  173.       myDFPlayer.playMp3Folder(5); //play specific mp3 in SD:/MP3/0005.mp3; File Name(0~65535)
  174.       break;
  175.     case N_B5:
  176.       myDFPlayer.playMp3Folder(6); //play specific mp3 in SD:/MP3/0006.mp3; File Name(0~65535)
  177.       break;
  178.     case N_C5:
  179.       myDFPlayer.playMp3Folder(7); //play specific mp3 in SD:/MP3/0007.mp3; File Name(0~65535)
  180.       break;
  181.     case N_D5:
  182.       myDFPlayer.playMp3Folder(8); //play specific mp3 in SD:/MP3/0008.mp3; File Name(0~65535)
  183.       break;
  184.     case N_E5:
  185.       myDFPlayer.playMp3Folder(9); //play specific mp3 in SD:/MP3/0009.mp3; File Name(0~65535)
  186.       break;
  187.     case N_F5:
  188.       myDFPlayer.playMp3Folder(10); //play specific mp3 in SD:/MP3/0010.mp3; File Name(0~65535)
  189.       break;
  190.     case N_G5:
  191.       myDFPlayer.playMp3Folder(11); //play specific mp3 in SD:/MP3/0011.mp3; File Name(0~65535)
  192.       break;
  193.     case N_B5F:
  194.       myDFPlayer.playMp3Folder(12); //play specific mp3 in SD:/MP3/0012.mp3; File Name(0~65535)
  195.       break;
  196.     case N_A5F:
  197.       myDFPlayer.playMp3Folder(13); //play specific mp3 in SD:/MP3/0013.mp3; File Name(0~65535)
  198.       break;
  199.     case N_SIL:
  200.       myDFPlayer.playMp3Folder(14); //play specific mp3 in SD:/MP3/0014.mp3; File Name(0~65535)
  201.       break;
  202.     case N_C5SH:
  203.       myDFPlayer.playMp3Folder(15); //play specific mp3 in SD:/MP3/0015.mp3; File Name(0~65535)
  204.       break;
  205.     case N_D5F:
  206.       myDFPlayer.playMp3Folder(16); //play specific mp3 in SD:/MP3/0016.mp3; File Name(0~65535)
  207.       break;
  208.     case N_D5SH:
  209.       myDFPlayer.playMp3Folder(17); //play specific mp3 in SD:/MP3/0017.mp3; File Name(0~65535)
  210.       break;
  211.     case N_E5F:
  212.       myDFPlayer.playMp3Folder(18); //play specific mp3 in SD:/MP3/0018.mp3; File Name(0~65535)
  213.       break;
  214.     case N_G5F:
  215.       myDFPlayer.playMp3Folder(19); //play specific mp3 in SD:/MP3/0019.mp3; File Name(0~65535)
  216.       break;
  217.     case N_A6:
  218.       myDFPlayer.playMp3Folder(20); //play specific mp3 in SD:/MP3/0020.mp3; File Name(0~65535)
  219.       break;
  220.     default:
  221.       Serial.println("No note played.");
  222.       break;
  223.   } //End of switch...case statement
  224.  
  225. } //End of playNote function definition
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement