最近寫的東西有點硬!放鬆一下,來點輕鬆的好了.哈!
這次來玩玩MP3 player(MP3 撥放器).
MP3的撥放器,在市面上早已是,恐龍級的產品.
現在也都內建在手機裡了.所以也沒人會特別去買.
那幹嘛還要介紹呢?好玩嘛...^_^|||
這次要採用的模組是DFPlayer Mini.
然後加上DFRobot/DFRobotDFPlayerMini的程式庫.
對程式庫不了解的請參考我的另一篇文章.
硬體:
1. Arduino Nano
2. DFPlayer Mini
3.小於3W的小喇叭(ps.我用的是0.3W,8歐姆)
硬體線路圖如下.
看起來也不難吧!
DFPlayer Mini的RX到Nano的D3中間有個1KR的電阻,不接也可以,但若喇叭有回授聲音,還是接上會比較好.
如果沒有適當喇叭的,或不想買喇叭的,可以試著將Spaker當輸出音源,即SPK_1,接到3.5吋的音樂端子的左右聲道,
SPK_2接到3.5吋的音樂端子的GND.啥?看不懂,好!有空我再來試給各位看.
再來就是將Nano插上PC的USB,開始寫軟體啦.
程式碼的部分不太難,就直接來吧.
|
//
// Name: MPlayer.ino
// Created: 2017/12/03 下午 05:18:40
// Author: 30sec
//
#include "Arduino.h"
#include "SoftwareSerial.h" // 採用SoftwareSerial程式庫
#include "DFRobotDFPlayerMini.h" // 採用DFRobotDFPlayerMini程式庫
SoftwareSerial mySoftwareSerial(2, 3); // mySoftwareSerial(RX, TX), 宣告軟體序列傳輸埠
// 用來與DFPlayerMini通訊用
DFRobotDFPlayerMini myDFPlayer; //宣告MP3 Player
void printDetail(uint8_t type, int value); //印出詳情
// the setup function runs once when you press reset or power the board
void setup()
{
Serial.begin(115200); // 定義Serial傳輸速率115200bps
mySoftwareSerial.begin(9600); // 定義mySoftwareSerial傳輸速率9600bps, DFPlayerMini的通訊速率為9600bps.
Serial.println(F("DFRobot DFPlayer Mini Demo")); // 印出DFRobot DFPlayer Mini Demo字串到Serial通訊埠
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)")); // 以下用法依此類通, 不再贅述喔
if (!myDFPlayer.begin(mySoftwareSerial)) // 如果DFPlayer Mini回應不正確.
{ //Use softwareSerial to communicate with mp3. // 印出下面3行字串
Serial.println(F("Unable to begin:")); // 然後程式卡死.
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true);
}
Serial.println(F("DFPlayer Mini online.")); // 如果DFPlayer Mini回應正確.印出"DFPlayer Mini online."
myDFPlayer.setTimeOut(500); // 設定通訊逾時為500ms
//----Set volume----
myDFPlayer.volume(10); // 設定音量, 範圍0~30.
//myDFPlayer.volumeUp(); // 增加音量
//myDFPlayer.volumeDown(); // 降低音量
//----Set different EQ---- // 設定EQ(等化器 Equalizer)
//myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
// myDFPlayer.EQ(DFPLAYER_EQ_POP);
myDFPlayer.EQ(DFPLAYER_EQ_ROCK); // 我比較喜歡搖滾模式
// myDFPlayer.EQ(DFPLAYER_EQ_JAZZ);
// myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC);
// myDFPlayer.EQ(DFPLAYER_EQ_BASS);
//----Set device we use SD as default---- // 設定SD卡
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_U_DISK);
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_AUX);
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_SLEEP);
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_FLASH);
//----Mp3 control---- // 設定MP3參數
// myDFPlayer.sleep(); //sleep
// myDFPlayer.reset(); //Reset the module
myDFPlayer.enableDAC(); //Enable On-chip DAC
// myDFPlayer.disableDAC(); //Disable On-chip DAC
// myDFPlayer.outputSetting(true, 15); //output setting, enable the output and set the gain to 15
//----Mp3 play---- // 設定MP3播放參數
myDFPlayer.play(1); // 播放第1首音樂
// 在SD卡裡放置MP3的目錄,然後放置名為1的MP3即可
// SD\MP3\1.mp3
// myDFPlayer.play(2);
// 即是撥放SD\MP3\2.mp3這首歌
// 接下來以此類推
// 好! 後面參數太多啦,懶得玩,有興趣的可以接下去玩.哈!
//myDFPlayer.pause(); //pause the mp3
//myDFPlayer.start();
//myDFPlayer.next(); //Play next mp3
//myDFPlayer.previous(); //Play previous mp3
//myDFPlayer.loop(1); //Loop the first mp3
//myDFPlayer.pause(); //pause the mp3
//myDFPlayer.start(); //start the mp3 from the pause
//myDFPlayer.playFolder(15, 4); //play specific mp3 in SD:/15/004.mp3; Folder Name(1~99); File Name(1~255)
//myDFPlayer.enableLoopAll(); //loop all mp3 files.
//myDFPlayer.disableLoopAll(); //stop loop all mp3 files.
//myDFPlayer.playMp3Folder(4); //play specific mp3 in SD:/MP3/0004.mp3; File Name(0~65535)
//myDFPlayer.advertise(3); //advertise specific mp3 in SD:/ADVERT/0003.mp3; File Name(0~65535)
//myDFPlayer.stopAdvertise(); //stop advertise
//myDFPlayer.playLargeFolder(2, 999); //play specific mp3 in SD:/02/004.mp3; Folder Name(1~10); File Name(1~1000)
//myDFPlayer.loopFolder(5); //loop all mp3 files in folder SD:/05.
//myDFPlayer.randomAll(); //Random play all the mp3.
//myDFPlayer.enableLoop(); //enable loop.
//myDFPlayer.disableLoop(); //disable loop.
//----Read imformation----
//Serial.print("State: ");
//Serial.println(myDFPlayer.readState()); //read mp3 state
//Serial.print("Volume: ");
//Serial.println(myDFPlayer.readVolume()); //read current volume
//Serial.print("EQ: ");
//Serial.println(myDFPlayer.readEQ()); //read EQ setting
//Serial.print("File Counts: ");
//Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
//Serial.print("CurrentFileNumber: ");
//Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
//Serial.print("FileCountsInFolder: ");
//Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read fill counts in folder SD:/03
}
// the loop function runs over and over again until power down or reset
void loop()
{
if (myDFPlayer.available()) // 監視MP3有沒有回應
{ // 有的話印出詳情
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
}
}
void printDetail(uint8_t type, int value)
{
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
|
程式下載完成後,就可以播第1首MP3啦.
對了!要記的先在SD卡內放入音樂喔.
Arduino MP3撥放器 - DFPlayer Mini由Peng Yi Hsing製作,以創用CC 姓名標示-非商業性-禁止改作 3.0 台灣 授權條款釋出。
Hi 謝謝你的文章 很有用呢。 另外想請教一下 可以用 blynk wifi透過 nodemcu去控制dfplayermini嗎? 謝謝
HI, 我沒玩過blynk跟nodemcu,不過理論上應該是可控制的. 這控制方式也蠻有趣的,改天有空我也來試試.謝謝喔.
老師 我不成功......怎麼辦 Archiving built core (caching) in: C:\Users\BLACKJ~1\AppData\Local\Temp\arduino_cache_438776\core\core_arduino_avr_nano_cpu_atmega328_26c701a1ea99e327efcdb4f17e754c4a.a 草稿碼使用了 5352 bytes (17%) 的程式儲存空間。上限為 30720 bytes。 全域變數使用了 353 bytes (17%) 的動態記憶體,剩餘 1695 bytes 給區域變數。上限為 2048 bytes 。 avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x0d avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x0a avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x44 avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x46 avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x52 avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x6f avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x62 avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x6f avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x74 avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x20 上傳草稿碼時發生錯誤
arduino ide在版本編譯的相容性上常會出現一些莫名的問題,如果你確定你的程式庫是正確的,那在檢查看看ide的版本是否與我的相同.我使用的是1.6.8的版本.建議使用跟我一樣的編譯看看.舊版的ide一樣可以在arduino - home裡面下載.
我是個剛開始玩arduino的菜鳥,我遇到了如樓上一樣的問題.是不是因為我用的是UNO而您用的是NANO?
我沒用過UNO.所以這回覆可能不會很精準. 你可以檢查一下,Arduino IDE的工具\板子:"Arduino/Genuino Uno"有沒有選對.
DFPlayer Mini 上面有顆燈都不會亮. 確認TF card有裝, 但都不會亮, 序列埠監控視窗也都沒顯示任何訊息, 這是哪邊出問題呢? 也已確認code有燒錄進去了, 謝謝
若在 ------------------------------------------------------ //----Mp3 play---- // 設定MP3播放參數 myDFPlayer.play(1); // 播放第1首音樂 ------------------------------------------------------ 這上面加一行程式碼,delay(10000); // 延遲10秒 變成如下 ------------------------------------------------------ delay(10000); // 延遲10秒 //----Mp3 play---- // 設定MP3播放參數 myDFPlayer.play(1); // 播放第1首音樂 ------------------------------------------------------ 則在開機後會先在"序列埠監控視窗"中看到 ========================== Opening port Port open DFRobot DFPlayer Mini Demo Initializing DFPlayer ... (May take 3~5 seconds) DFPlayer Mini online. (此處代表DFPlayer已經連線) ============================= 等待10秒後,執行myDFPlayer.play(1); DFPlayer的藍燈才會亮起,並撥放音樂. 檢查一下你的程序,故障原因應該是在DFPlayer跟Nano的連線.
請問一下杜邦線是(公對母)的嗎?
杜邦線???這篇文章用到的是單心線喔. 另外, 杜邦線你要用(公對公)(公對母)(母對母)都可以喔. 活用一下吧.
不好意思 我的dfplayer那顆燈沒有亮 而且序列埠監控視窗出現了亂碼 不過我是用UNO做的 是板子的問題嗎?
1.你可以看看是不是baud rate造成亂碼的問題,我的程式碼是用115200 2.我用的函數庫是針對nano寫的,不確定能否支援uno.