Guest User

Untitled

a guest
Sep 14th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. //Demo for Serial MP3 Player A module
  2. //by Open-Smart Team and Catalex Team
  3. //catalex_inc@163.com
  4. //Board: Arduino UNO R3
  5. //IDE: Arduino-1.6.4
  6. //Store: http://www.aliexpress.com/store/1199788
  7. // http://dx.com
  8. //Function: In the process that the Rotation Angle Sensor is rotated from
  9. // the 'Min' side to the 'Max' side, the volume is gradually greater.
  10. // If you touch the Touch Sensor,it will play or pause.
  11.  
  12. /* Wiring Guide */
  13. //Serial MP3 Player A | Arduino UNO R3//
  14. // RX | 7
  15. // TX | 8
  16. // VCC | 5V
  17. // GND | GND
  18. // output + | 13
  19. //-------------------------------------
  20.  
  21.  
  22.  
  23. #include <SoftwareSerial.h>
  24.  
  25. #define ARDUINO_RX 8//should connect to TX of the Serial MP3 Player module
  26. #define ARDUINO_TX 7//connect to RX of the module
  27. SoftwareSerial myMP3(ARDUINO_RX, ARDUINO_TX);
  28.  
  29. unsigned char playmode = 1;
  30. #define PLAY 1
  31.  
  32. static int8_t Send_buf[6] = {0} ;
  33. /************Command byte**************************/
  34. /*basic commands*/
  35. #define CMD_PLAY 0X01
  36.  
  37.  
  38. /*5 bytes commands*/
  39. #define CMD_SEL_DEV 0X35
  40. #define DEV_TF 0X01
  41. #define CMD_IC_MODE 0X35
  42.  
  43.  
  44. /*Special commands*/
  45. #define CMD_SET_VOLUME 0X31
  46. #define CMD_PLAY_W_VOL 0X31
  47.  
  48. #define CMD_SET_PLAY_MODE 0X33
  49. #define SINGLE_CYCLE 0X01
  50.  
  51. #define CMD_PLAY_COMBINE 0X45//can play combination up to 15 songs
  52.  
  53. /*********************************************************************/
  54. /*macro definitions of Rotary angle sensor and LED pin*/
  55. #define ROTARY_ANGLE_SENSOR A0
  56.  
  57. #define ADC_REF 5//reference voltage of ADC is 5v
  58. #define VCC 5 //the default value of VCC of the control interface is 5v
  59. #define FULL_ANGLE 280//full value of the rotary angle is 280 degrees
  60.  
  61. #define TOUCH_SENSOR 2
  62.  
  63. void sendCommand(int8_t command, int16_t dat = 0);
  64.  
  65. void setup()
  66. {
  67. Serial.begin(9600);
  68. myMP3.begin(9600);
  69. delay(500);
  70. pinMode(TOUCH_SENSOR, INPUT_PULLUP);
  71. attachInterrupt(digitalPinToInterrupt(TOUCH_SENSOR), playOrPause, RISING);
  72. sendCommand(CMD_SEL_DEV, DEV_TF);
  73. delay(200);
  74. playWithVolume(0X0F01);//play the first song with volume 15(0x0F) class
  75. // put your setup code here, to run once:
  76. pinMode( 9, INPUT );
  77. pinMode( 13, OUTPUT);// led for test
  78.  
  79. digitalWrite(13,LOW);
  80. }
  81. static int8_t pre_vol = 0x0f;
  82. void loop()
  83. {
  84. int degrees;
  85. degrees = getDegree();
  86.  
  87. int8_t volume;
  88. /*The degrees is 0~280, should be converted to be 0~255 to control the*/
  89. /*brightness of LED */
  90. volume = map(degrees, 0, 250, 0, 80);
  91. if(volume != pre_vol)
  92. {
  93. setVolume(volume);
  94. pre_vol = volume;
  95. // put your main code here, to run repeatedly:
  96. if( digitalRead(9) == LOW ){
  97. digitalWrite(13,HIGH);
  98. }else{
  99. digitalWrite(13,LOW);
  100. }
  101. }
  102. if(playmode == PLAY) sendCommand(CMD_PLAY);
  103.  
  104. delay(50);
  105. }
  106.  
  107.  
  108. /********************************************************************************/
  109. /*Function: Get the angle between the mark on the potentiometer cap and the starting position */
  110. /*Parameter:-void */
  111. /*Return: -int,the range of degrees is 0~280 */
  112. int getDegree()
  113. {
  114. int sensor_value = analogRead(ROTARY_ANGLE_SENSOR);
  115. float voltage;
  116. voltage = (float)sensor_value*ADC_REF/1023;
  117. float degrees = (voltage*FULL_ANGLE)/VCC;
  118. return degrees;
  119. }
  120.  
  121. /*Interrupt service routine*/
  122. void playOrPause()
  123. {
  124. playmode = !playmode;
  125. }
  126.  
  127. void setVolume(int8_t vol)
  128. {
  129. mp3_5bytes(CMD_SET_VOLUME, vol);
  130. }
  131. void playWithVolume(int16_t dat)
  132. {
  133. mp3_6bytes(CMD_PLAY_W_VOL, dat);
  134. }
  135.  
  136. /*cycle play with an index*/
  137. void cyclePlay(int16_t index)
  138. {
  139. mp3_6bytes(CMD_SET_PLAY_MODE,index);
  140. }
  141.  
  142. void setCyleMode(int8_t AllSingle)
  143. {
  144. mp3_5bytes(CMD_SET_PLAY_MODE,AllSingle);
  145. }
  146.  
  147.  
  148. void playCombine(int8_t song[][2], int8_t number)
  149. {
  150. if(number > 15) return;//number of songs combined can not be more than 15
  151. uint8_t nbytes;//the number of bytes of the command with starting byte and ending byte
  152. nbytes = 2*number + 4;
  153. int8_t Send_buf[nbytes];
  154. Send_buf[0] = 0x7e; //starting byte
  155. Send_buf[1] = nbytes - 2; //the number of bytes of the command without starting byte and ending byte
  156. Send_buf[2] = CMD_PLAY_COMBINE;
  157. for(uint8_t i=0; i < number; i++)//
  158. {
  159. Send_buf[i*2+3] = song[i][0];
  160. Send_buf[i*2+4] = song[i][1];
  161. }
  162. Send_buf[nbytes - 1] = 0xef;
  163. sendBytes(nbytes);
  164. }
  165.  
  166.  
  167. void sendCommand(int8_t command, int16_t dat)
  168. {
  169. delay(20);
  170. if((command == CMD_PLAY_W_VOL)||(command == CMD_SET_PLAY_MODE)||(command == CMD_PLAY_COMBINE))
  171. return;
  172. else if(command < 0x10)
  173. {
  174. mp3Basic(command);
  175. }
  176. else if(command < 0x40)
  177. {
  178. mp3_5bytes(command, dat);
  179. }
  180. else if(command < 0x50)
  181. {
  182. mp3_6bytes(command, dat);
  183. }
  184. else return;
  185.  
  186. }
  187.  
  188. void mp3Basic(int8_t command)
  189. {
  190. Send_buf[0] = 0x7e; //starting byte
  191. Send_buf[1] = 0x02; //the number of bytes of the command without starting byte and ending byte
  192. Send_buf[2] = command;
  193. Send_buf[3] = 0xef; //
  194. sendBytes(4);
  195. }
  196. void mp3_5bytes(int8_t command, uint8_t dat)
  197. {
  198. Send_buf[0] = 0x7e; //starting byte
  199. Send_buf[1] = 0x03; //the number of bytes of the command without starting byte and ending byte
  200. Send_buf[2] = command;
  201. Send_buf[3] = dat; //
  202. Send_buf[4] = 0xef; //
  203. sendBytes(5);
  204. }
  205. void mp3_6bytes(int8_t command, int16_t dat)
  206. {
  207. Send_buf[0] = 0x7e; //starting byte
  208. Send_buf[1] = 0x04; //the number of bytes of the command without starting byte and ending byte
  209. Send_buf[2] = command;
  210. Send_buf[3] = (int8_t)(dat >> 8);//datah
  211. Send_buf[4] = (int8_t)(dat); //datal
  212. Send_buf[5] = 0xef; //
  213. sendBytes(6);
  214. }
  215. void sendBytes(uint8_t nbytes)
  216. {
  217. for(uint8_t i=0; i < nbytes; i++)//
  218. {
  219. myMP3.write(Send_buf[i]) ;
  220. }
  221. }
Add Comment
Please, Sign In to add comment