最近寫的東西有點硬!放鬆一下,來點輕鬆的好了.哈!
這次來玩玩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 台灣 授權條款釋出。
留言列表