DFplayermini x bluetooth mp3 player control
In this tutorial we will see how to make an MP3 file player with the DFplayer mini module, Arduino and a 128x32 OLED display.
Components and supplies
1
Arduino Nano
Apps and platforms
1
rogerbit.
Project description
Code
Source code
c
Spurce code
1#include "U8glib.h"//Librería para el control del display oled 2U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);// I2C / TWI Se habilita esta linea según el display a usar en este caso el driver SH1106 3#include "Arduino.h" 4#include "SoftwareSerial.h" 5#include "DFRobotDFPlayerMini.h" 6String texto = ""; 7int vol = 10; 8String cadena; 9SoftwareSerial mySoftwareSerial(10, 11); // RX, TX pines por software para la comunicación con el módulo dfplayer mini 10DFRobotDFPlayerMini myDFPlayer; 11int play_d2 = 2; 12int pause_d9 = 9; 13int stop_d4 = 4; 14int next_d5 = 5; 15int prev_d6 = 6; 16int voltmas_d7 = 7; 17int volmenos_d8 = 8; 18int estado_pausa = 0; 19void setup() 20{ 21 mySoftwareSerial.begin(9600); 22 Serial.begin(9600);//Velocidad del módulo bluetooth 23 pinMode(play_d2, INPUT_PULLUP); 24 pinMode(pause_d9, INPUT_PULLUP); 25 pinMode(stop_d4, INPUT_PULLUP); 26 pinMode(next_d5, INPUT_PULLUP); 27 pinMode(prev_d6, INPUT_PULLUP); 28 pinMode(voltmas_d7, INPUT_PULLUP); 29 pinMode(volmenos_d8, INPUT_PULLUP); 30 Serial.println(); 31 Serial.println(F("Inicializando DFPlayer mini")); 32 33 if (!myDFPlayer.begin(mySoftwareSerial)) { //Usa softwareSerial para communicarte con el módulo mp3. 34 Serial.println(F("No es posible iniciar:")); 35 Serial.println(F("1.Comprueba las conexiones")); 36 Serial.println(F("2.Por favor inserte una tarjeta SD")); 37 while(true); 38 } 39 Serial.println(F("DFPlayer Mini online.")); 40 41 myDFPlayer.volume(vol); //Se setea el volumen 42 //Se mostrará el siguente texto en el display 43 texto = "NO TRACK"; 44 oled(); 45} 46 47void loop() 48{ 49 while (Serial.available()) { 50 delay(3); 51 char c = Serial.read(); 52 cadena += c; 53 } 54 //Lectura del estado de los pulsadores 55 int boton_play = digitalRead(play_d2); 56 int boton_pause = digitalRead(pause_d9); 57 int boton_stop = digitalRead(stop_d4); 58 int boton_next = digitalRead(next_d5); 59 int boton_prev = digitalRead(prev_d6); 60 int boton_volmas = digitalRead(voltmas_d7); 61 int boton_volmenos = digitalRead(volmenos_d8); 62//Si se aprieta el botón de play se cumple 63 if(boton_play == LOW || cadena == "play"){ 64 delay(100); 65 myDFPlayer.enableLoopAll(); 66 myDFPlayer.play(); 67texto = "PLAY";//Se mostrará el siguente texto en el display 68oled(); 69 } 70//Pone en pausa el track de música 71 if(boton_pause == LOW || cadena == "pause"){ 72 if(estado_pausa == 0){ 73 delay(200); 74 myDFPlayer.pause(); 75 texto = "PAUSE"; 76 oled(); 77 estado_pausa = 1; 78 }else{ 79 delay(200); 80 myDFPlayer.start();//Vuelve a iniciar el tema donde quedó la pausa 81 texto = "PLAY"; 82 oled(); 83 estado_pausa = 0; 84 } 85 } 86//Para el track de música que se estaba repoduciendo 87 if(boton_stop == LOW || cadena == "stop"){ 88 delay(100); 89 myDFPlayer.stop(); 90 texto = "STOP"; 91 oled(); 92 } 93//Reproduce el próximo tack de música 94 if(boton_next == LOW || cadena == "next"){ 95 delay(100); 96 myDFPlayer.next(); 97 texto = "NEXT"; 98 oled(); 99 delay(500); 100 texto = "PLAY"; 101 oled(); 102 } 103//Reproduce el anterior tack de música 104 if(boton_prev == LOW || cadena == "prev"){ 105 delay(100); 106 myDFPlayer.previous(); 107 texto = "PREV"; 108 oled(); 109 delay(500); 110 texto = "PLAY"; 111 oled(); 112 } 113//Incrementa el vólumen hasta 35 como máximo 114 if(boton_volmas == LOW || cadena == "volmas"){ 115 delay(100); 116 if(vol <35){ 117 myDFPlayer.volumeUp(); 118 vol = vol + 1; 119 oled(); 120 } 121 } 122//Decrementa el vólumen hasta 0 como mínimo 123 if(boton_volmenos == LOW || cadena == "volmenos"){ 124 delay(100); 125 if(vol >0){ 126 myDFPlayer.volumeDown(); 127 vol = vol - 1; 128 oled(); 129 } 130 } 131//Habilita la reproducción la aleatoria 132 if(cadena == "random"){ 133 delay(100); 134 myDFPlayer.randomAll(); 135 texto = "RANDOM"; 136 oled(); 137 } 138//Habilita la reproducción en bucle 139 if(cadena == "eloop"){ 140 delay(100); 141 myDFPlayer.enableLoopAll(); 142 texto = "E.LOOP"; 143 oled(); 144 } 145//deshabilita la reproducción en bucle 146 if(cadena == "dloop"){ 147 delay(100); 148 myDFPlayer.disableLoopAll(); 149 texto = "D.LOOP"; 150 oled(); 151 } 152//Funciones de equalizador 153//Habilita Normal 154 if(cadena == "normal"){ 155 delay(100); 156 myDFPlayer.EQ(DFPLAYER_EQ_NORMAL); 157 texto = "NORMAL"; 158 oled(); 159 } 160//Habilita Pop 161 if(cadena == "pop"){ 162 delay(100); 163 myDFPlayer.EQ(DFPLAYER_EQ_POP); 164 texto = "POP"; 165 oled(); 166 } 167//Habilita Rock 168 if(cadena == "rock"){ 169 delay(100); 170 myDFPlayer.EQ(DFPLAYER_EQ_ROCK); 171 texto = "ROCK"; 172 oled(); 173 } 174//Habilita Jazz 175 if(cadena == "jazz"){ 176 delay(100); 177 myDFPlayer.EQ(DFPLAYER_EQ_JAZZ); 178 texto = "JAZZ"; 179 oled(); 180 } 181//Habilita Classic 182 if(cadena == "classic"){ 183 delay(100); 184 myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC); 185 texto = "CLASSIC"; 186 oled(); 187 } 188//Habilita Bass 189 if(cadena == "bass"){ 190 delay(100); 191 myDFPlayer.EQ(DFPLAYER_EQ_BASS); 192 texto = "BASS"; 193 oled(); 194 } 195 if(cadena != ""){ 196 cadena = ""; 197 } 198 } 199 200 201//Funcíon para mostrar texto en el dislplay oled 202void oled(){ 203u8g.firstPage(); 204 do { 205 draw();//Llama a la función draw 206 } while( u8g.nextPage() ); 207 // Reconstruir la imagen después de un tiempo 208 delay(50); 209 } 210 211 void draw(void) { 212 //Imprimimos en pantalla 213 u8g.setFont(u8g_font_unifont); 214 u8g.setPrintPos(0, 20); 215 u8g.print("REPRODUCTOR MP3");// 216 //Muestra la palabra ESTADO 217 u8g.setPrintPos(0, 40); 218 u8g.print("ESTADO: ");// 219 u8g.setPrintPos(55, 40); 220 u8g.print(texto);//Estado de reproducción del track de música PLAY STOP PAUSE 221//Muestra la palabra de VOLUMEN 222 u8g.setPrintPos(0, 60); 223 u8g.print("VOLUMEN:"); 224 u8g.setPrintPos(65, 60); 225 u8g.print(vol);//Muestra el incremento o decremento del volumen de reproducción 226 }
Comments
Only logged in users can leave comments