safwan092

Untitled

Nov 13th, 2022
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. /// MP3 PLAYER PROJECT
  2. /// http://educ8s.tv/arduino-mp3-player/
  3. //////////////////////////////////////////
  4.  
  5.  
  6. # define Start_Byte 0x7E
  7. # define Version_Byte 0xFF
  8. # define Command_Length 0x06
  9. # define End_Byte 0xEF
  10. # define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
  11.  
  12. # define ACTIVATED LOW
  13.  
  14. int buttonNext = 2;
  15. int buttonPause = 3;
  16. int buttonPrevious = 4;
  17. boolean isPlaying = false;
  18.  
  19.  
  20.  
  21. void setup () {
  22.  
  23. pinMode(buttonPause, INPUT);
  24. digitalWrite(buttonPause,HIGH);
  25. pinMode(buttonNext, INPUT);
  26. digitalWrite(buttonNext,HIGH);
  27. pinMode(buttonPrevious, INPUT);
  28. digitalWrite(buttonPrevious,HIGH);
  29.  
  30. Serial2.begin(9600);
  31.  
  32. delay(1000);
  33. playFirst();
  34. isPlaying = true;
  35.  
  36.  
  37. }
  38.  
  39.  
  40.  
  41. void loop () {
  42.  
  43. if (digitalRead(buttonPause) == ACTIVATED)
  44. {
  45. if(isPlaying)
  46. {
  47. pause();
  48. isPlaying = false;
  49. }else
  50. {
  51. isPlaying = true;
  52. play();
  53. }
  54. }
  55.  
  56.  
  57. if (digitalRead(buttonNext) == ACTIVATED)
  58. {
  59. if(isPlaying)
  60. {
  61. playNext();
  62. }
  63. }
  64.  
  65. if (digitalRead(buttonPrevious) == ACTIVATED)
  66. {
  67. if(isPlaying)
  68. {
  69. playPrevious();
  70. }
  71. }
  72. }
  73.  
  74. void playFirst()
  75. {
  76. execute_CMD(0x3F, 0, 0);
  77. delay(500);
  78. setVolume(20);
  79. delay(500);
  80. execute_CMD(0x11,0,1);
  81. delay(500);
  82. }
  83.  
  84. void pause()
  85. {
  86. execute_CMD(0x0E,0,0);
  87. delay(500);
  88. }
  89.  
  90. void play()
  91. {
  92. execute_CMD(0x0D,0,1);
  93. delay(500);
  94. }
  95.  
  96. void playNext()
  97. {
  98. execute_CMD(0x01,0,1);
  99. delay(500);
  100. }
  101.  
  102. void playPrevious()
  103. {
  104. execute_CMD(0x02,0,1);
  105. delay(500);
  106. }
  107.  
  108. void setVolume(int volume)
  109. {
  110. execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
  111. delay(2000);
  112. }
  113.  
  114. void execute_CMD(byte CMD, byte Par1, byte Par2)
  115. // Excecute the command and parameters
  116. {
  117. // Calculate the checksum (2 bytes)
  118. word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
  119. // Build the command line
  120. byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
  121. Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
  122. //Send the command line to the module
  123. for (byte k=0; k<10; k++)
  124. {
  125. Serial2.write( Command_line[k]);
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment