Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- File name : IRRemoteControlsDFPlayerMiniMp3.ino
- Description : this sketch process ir remote decoded info and
- controls mp3 player df3mini board
- Author : Anatoly A. Sharapov
- Email : a.a.sharapov@gmail.com
- Phone : +7 903 231-11-26
- Location : 124575 Moscow, Zelenograd, 1206 - 131
- Board : Arduino Nano
- Processor : ATmega328P (Old Bootloader)
- Controls : DFPlayer Mini
- Library1 : SoftwareSerial
- Library2 : DFMiniMp3-master
- Library3 : IRRemote
- Library4 : EEPROM
- URL1 : https://doc.arduino.ua/ru/prog/SoftwareSerial
- URL2 : https://lesson.iarduino.ru/page/urok-17-podklyuchenie-mini-mp3-pleera-k-arduino/
- URL3 : https://github.com/Makuna/DFMiniMp3/blob/master/src/DFMiniMp3.h
- Last revision : 12.09.2021
- */
- #include <SoftwareSerial.h>
- #include <DFMiniMp3.h>
- #include <IRremote.h>
- #include <EEPROM.h>
- const uint8_t RX = 5;
- const uint8_t TX = 4;
- SoftwareSerial DFPlayerMiniSerial(RX, TX);
- // SoftwareSerial(uint8_t rxPin, uint8_t txPin, bool inverse_logic = false);
- /*
- Схема:
- * RX - цифровой вывод (необходимо соединить с выводом TX другого устройства)
- * TX - цифровой вывод (необходимо соединить с выводом RX другого устройства)
- */
- // forward declare the notify class, just the name
- class Mp3Notify;
- // define a handy type using serial and our notify class
- typedef DFMiniMp3 <SoftwareSerial, Mp3Notify> DFMiniMp3_t;
- // Arduino Nano.D5 (RX) - DFPlayer Mini.3 (TX) - orange
- // Arduino Nano.D4 (TX) - through 1kOhm - DFPlayer Mini.2 (RX) - yellow
- // Arduino Nano.5V - DFPlayer Mini.1 (VCC) - red
- // Arduino Nano.GND - DFPlayer Mini.7 (GND) - brown
- // instance an object
- DFMiniMp3_t DFPlayerMiniMp3(DFPlayerMiniSerial);
- // implement a notification class,
- // its member methods will get called
- class Mp3Notify
- {
- public:
- static void PrintlnPlaybackMode(DfMp3_PlaybackMode PlaybackMode)
- {
- switch (PlaybackMode) {
- case DfMp3_PlaybackMode_Repeat :
- Serial.println(F("DfMp3_PlaybackMode_Repeat"));
- break;
- case DfMp3_PlaybackMode_FolderRepeat :
- Serial.println(F("DfMp3_PlaybackMode_FolderRepeat"));
- break;
- case DfMp3_PlaybackMode_SingleRepeat :
- Serial.println(F("DfMp3_PlaybackMode_SingleRepeat"));
- break;
- case DfMp3_PlaybackMode_Random :
- Serial.println(F("DfMp3_PlaybackMode_Random"));
- break;
- default:
- Serial.println(F("Error - Undefined"));
- break;
- }
- }
- static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action)
- {
- if (source & DfMp3_PlaySources_Sd)
- {
- Serial.print(F("SD Card, "));
- }
- if (source & DfMp3_PlaySources_Usb)
- {
- Serial.print(F("USB Disk, "));
- }
- if (source & DfMp3_PlaySources_Flash)
- {
- Serial.print(F("Flash, "));
- }
- Serial.println(action);
- }
- static void OnError(DFMiniMp3_t& mp3, uint16_t errorCode)
- {
- // see DfMp3_Error for code meaning
- Serial.println();
- Serial.print(F("errorCode = "));
- Serial.print(errorCode);
- Serial.print(F(" stands for "));
- switch (errorCode) {
- case DfMp3_Error_Busy :
- Serial.print(F("DfMp3_Error_Busy"));
- break;
- case DfMp3_Error_Sleeping :
- Serial.print(F("DfMp3_Error_Sleeping"));
- break;
- case DfMp3_Error_SerialWrongStack :
- Serial.print(F("DfMp3_Error_SerialWrongStack"));
- break;
- case DfMp3_Error_CheckSumNotMatch :
- Serial.print(F("DfMp3_Error_CheckSumNotMatch"));
- break;
- case DfMp3_Error_FileIndexOut :
- Serial.print(F("DfMp3_Error_FileIndexOut"));
- break;
- case DfMp3_Error_FileMismatch :
- Serial.print(F("DfMp3_Error_FileMismatch"));
- break;
- case DfMp3_Error_Advertise :
- Serial.print(F("DfMp3_Error_Advertise"));
- break;
- case DfMp3_Error_RxTimeout :
- Serial.print(F("DfMp3_Error_RxTimeout"));
- break;
- case DfMp3_Error_PacketSize :
- Serial.print(F("DfMp3_Error_PacketSize"));
- break;
- case DfMp3_Error_PacketHeader :
- Serial.print(F("DfMp3_Error_PacketHeader"));
- break;
- case DfMp3_Error_PacketChecksum :
- Serial.print(F("DfMp3_Error_PacketChecksum"));
- break;
- case DfMp3_Error_General :
- Serial.print(F("DfMp3_Error_General"));
- break;
- default :
- Serial.print(F("unknown"));
- break;
- }
- }
- static void OnPlayFinished(DFMiniMp3_t& mp3, DfMp3_PlaySources source, uint16_t track)
- {
- Serial.print(F("Play finished for source = "));
- switch (source) {
- case 0x01 :
- Serial.print(F("USB"));
- break;
- case 0x02 :
- Serial.print(F("SD"));
- break;
- case 0x04 :
- Serial.print(F("PC"));
- break;
- case 0x08 :
- Serial.print(F("Flash"));
- break;
- default :
- Serial.print(F("unknown"));
- break;
- }
- Serial.print(F(" and track = "));
- Serial.println(track);
- }
- static void OnPlaySourceOnline(DFMiniMp3_t& mp3, DfMp3_PlaySources source)
- {
- PrintlnSourceAction(source, "online");
- }
- static void OnPlaySourceInserted(DFMiniMp3_t& mp3, DfMp3_PlaySources source)
- {
- PrintlnSourceAction(source, "inserted");
- }
- static void OnPlaySourceRemoved(DFMiniMp3_t& mp3, DfMp3_PlaySources source)
- {
- PrintlnSourceAction(source, "removed");
- }
- };
- const int SerialBaudRate = 9600;
- const int LedPinNum = 13;
- const int LedStateOff = LOW;
- const int LedStateOn = HIGH;
- int LedPinState = LedStateOff;
- const bool LedOff = false;
- const bool LedOn = true;
- int LedState;
- uint8_t DFPlayerMiniInitVolume,
- DFPlayerMiniNewVolume;
- const uint8_t DFPlayerMiniMinVolume = 0,
- DFPlayerMiniMaxVolume = 30,
- DFPlayerMiniSetVolume = DFPlayerMiniMaxVolume;
- const int DFPLAYER_MINI_MP3_INTER_COMMANDS_DELAY = 100;
- // Между двумя командами необходимо делать задержку 100 миллисекунд,
- // в противном случае некоторые команды могут работать не стабильно.
- const bool DFPLAYER_MINI_MP3_INTER_COMMANDS_DELAY_IS_ABSENT = false;
- const bool DFPLAYER_MINI_MP3_INTER_COMMANDS_DELAY_IS_PRESENT = true;
- bool DFPlayerMiniMp3InterCommandsDelayMode;
- void DFPlayerMiniMp3InterCommandsDelay() {
- if (DFPlayerMiniMp3InterCommandsDelayMode == DFPLAYER_MINI_MP3_INTER_COMMANDS_DELAY_IS_PRESENT) {
- delay(DFPLAYER_MINI_MP3_INTER_COMMANDS_DELAY);
- }
- }
- uint8_t CurrentFolderNum;
- uint16_t CurrentTrackNum;
- uint16_t TotalFolderCount;
- uint8_t CurrentVolume;
- const int VS1838B_PIN = 11;
- IRrecv irrecv(VS1838B_PIN);
- decode_results results;
- uint8_t ir_button;
- const bool DFPLAYER_MINI_MP3_PAUSED = false;
- const bool DFPLAYER_MINI_MP3_PLAYING = true;
- bool PlayMode;
- // older devices: sd:/###/###track name
- // newer devices: sd:/##/###track name
- // folder and track numbers are zero padded
- void LoopFolder() {
- if (CurrentFolderNum > TotalFolderCount) {
- CurrentFolderNum = TotalFolderCount;
- }
- DFPlayerMiniMp3.loopFolder(CurrentFolderNum);
- DFPlayerMiniMp3InterCommandsDelay();
- Serial.println(F("In LoopFolder ->"));
- Serial.print(F("CurrentFolderNum = "));
- Serial.print(CurrentFolderNum);
- Serial.print(F(", CurrentTrackNum = "));
- Serial.println(CurrentTrackNum);
- }
- DFMiniMp3::DfMp3_PlaybackMode PlaybackMode;
- void LoopPlayBackMode(DFMiniMp3_t::DfMp3_PlaybackMode PlaybackMode) {
- Serial.println(F("In LoopPlayBackMode -> PlayBackMode = "));
- DfMp3_PlaybackMode_Print(PlaybackMode);
- DFPlayerMiniMp3.loopGlobalTrack(PlaybackMode);
- }
- void PlayTrack() {
- DFPlayerMiniMp3.playFolderTrack16(CurrentFolderNum, CurrentTrackNum);
- DFPlayerMiniMp3InterCommandsDelay();
- Serial.println(F("In PlayTrack -> "));
- Serial.print(F("CurrentFolderNum = "));
- Serial.print(CurrentFolderNum);
- Serial.print(F(", CurrentTrackNum = "));
- Serial.println(CurrentTrackNum);
- }
- void PlayPrevTrack() {
- Serial.println(F("In PlayPrevTrack -> "));
- CurrentTrackNum = DFPlayerMiniMp3.getCurrentTrack();
- Serial.print(F("CurrentTrackNum = "));
- Serial.println(CurrentTrackNum);
- if (CurrentTrackNum > 0) {
- CurrentTrackNum--;
- } else {
- CurrentTrackNum = DFPlayerMiniMp3.getFolderTrackCount(CurrentFolderNum);
- }
- PlayTrack();
- }
- void PlayNextTrack() {
- Serial.println(F("In PlayNextTrack -> "));
- CurrentTrackNum = DFPlayerMiniMp3.getCurrentTrack();
- Serial.print(F("CurrentTrackNum = "));
- Serial.println(CurrentTrackNum);
- if (CurrentTrackNum == DFPlayerMiniMp3.getFolderTrackCount(CurrentFolderNum)) {
- CurrentTrackNum = 0;
- } else {
- CurrentTrackNum++;
- }
- PlayTrack();
- }
- void SetVolume() {
- DFPlayerMiniMp3.setVolume(CurrentVolume);
- DFPlayerMiniMp3InterCommandsDelay();
- Serial.println(F("In SetVolume ->"));
- Serial.print(F("CurrentVolume = "));
- Serial.println(CurrentVolume);
- }
- void IncreaseVolume() {
- CurrentVolume = DFPlayerMiniMp3.getVolume();
- if (CurrentVolume < DFPlayerMiniMaxVolume) {
- CurrentVolume++;
- }
- SetVolume();
- }
- void DecreaseVolume() {
- CurrentVolume = DFPlayerMiniMp3.getVolume();
- if (CurrentVolume > 0) {
- CurrentVolume--;
- }
- SetVolume();
- }
- const int EEPROM_ADDRESS_4_CURRENT_FOLDER_NUM = 0;
- const int EEPROM_ADDRESS_4_CURRENT_TRACK_NUM_HIGH = 1;
- const int EEPROM_ADDRESS_4_CURRENT_TRACK_NUM_LOW = 2;
- const int EEPROM_ADDRESS_4_CURRENT_VOLUME = 3;
- uint8_t CurrentTrackNumHigh,
- CurrentTrackNumLow;
- void SaveParameters2EEPROM() {
- CurrentTrackNumHigh = CurrentTrackNum >> 8;
- CurrentTrackNumLow = CurrentTrackNum & 0xff;
- EEPROM.write(EEPROM_ADDRESS_4_CURRENT_FOLDER_NUM, CurrentFolderNum);
- EEPROM.write(EEPROM_ADDRESS_4_CURRENT_TRACK_NUM_HIGH, CurrentTrackNumHigh);
- EEPROM.write(EEPROM_ADDRESS_4_CURRENT_TRACK_NUM_LOW, CurrentTrackNumLow);
- EEPROM.write(EEPROM_ADDRESS_4_CURRENT_VOLUME, CurrentVolume);
- }
- const uint8_t ParamReadFromEEPROMIsNeverBeenWritten = 255;
- uint8_t SetInitialParamReadFromEEPROM(uint8_t ParamReadFromEEPROM) {
- if (ParamReadFromEEPROM == ParamReadFromEEPROMIsNeverBeenWritten) {
- ParamReadFromEEPROM = 0;
- }
- return ParamReadFromEEPROM;
- }
- void RestoreParametersFromEEPROM() {
- Serial.println("In RestoreParametersFromEEPROM ->");
- CurrentFolderNum = SetInitialParamReadFromEEPROM(EEPROM.read(EEPROM_ADDRESS_4_CURRENT_FOLDER_NUM));
- Serial.print(F("CurrentFolderNum = "));
- Serial.print(CurrentFolderNum);
- CurrentVolume = SetInitialParamReadFromEEPROM(EEPROM.read(EEPROM_ADDRESS_4_CURRENT_VOLUME));
- Serial.print(F(", CurrentVolume = "));
- Serial.print(CurrentVolume);
- CurrentTrackNumHigh = SetInitialParamReadFromEEPROM(EEPROM.read(EEPROM_ADDRESS_4_CURRENT_TRACK_NUM_HIGH));
- CurrentTrackNumLow = SetInitialParamReadFromEEPROM(EEPROM.read(EEPROM_ADDRESS_4_CURRENT_TRACK_NUM_LOW));
- CurrentTrackNum = CurrentTrackNumHigh << 8 & CurrentTrackNumLow;
- Serial.print(F(", CurrentTrackNum = "));
- Serial.println(CurrentTrackNum);
- }
- void setup() {
- PlayMode = DFPLAYER_MINI_MP3_PAUSED;
- DFPlayerMiniMp3InterCommandsDelayMode = DFPLAYER_MINI_MP3_INTER_COMMANDS_DELAY_IS_ABSENT;
- Serial.begin(SerialBaudRate);
- while (!Serial); // wait for serial port to connect. Needed for native USB
- Serial.println(F("PS2KeyboardControlsDFPlayerMiniMp3 is started"));
- RestoreParametersFromEEPROM();
- DFPlayerMiniMp3.begin();
- DFPlayerMiniMp3InterCommandsDelay();
- PlaybackMode = DFPlayerMiniMp3.DfMp3_PlaybackMode_FolderRepeat;
- DfMp3_PlaybackMode_Print(PlaybackMode);
- TotalFolderCount = DFPlayerMiniMp3.getTotalFolderCount();
- Serial.print(F("TotalFolderCount = "));
- Serial.println(TotalFolderCount);
- pinMode(LedPinNum, OUTPUT);
- LedPinState = LedStateOff;
- digitalWrite(LedPinNum, LedPinState);
- irrecv.enableIRIn(); // Start the receiver
- ir_button = 100;
- }
- void loop() {
- if (irrecv.decode()) {
- switch (irrecv.decodedIRData.decodedRawData) {
- case 0xBA45FF00 :
- ir_button = 1;
- CurrentFolderNum = 1;
- CurrentTrackNum = 0;
- Serial.println(F("*Pressed 1"));
- LoopFolder();
- break;
- case 0xB946FF00 :
- ir_button = 2;
- CurrentFolderNum = 2;
- CurrentTrackNum = 0;
- Serial.println(F("Pressed 2"));
- LoopFolder();
- break;
- case 0xB847FF00 :
- ir_button = 3;
- CurrentFolderNum = 3;
- CurrentTrackNum = 0;
- Serial.println(F("Pressed 3"));
- LoopFolder();
- break;
- case 0xBB44FF00 :
- ir_button = 4;
- CurrentFolderNum = 4;
- CurrentTrackNum = 0;
- Serial.println(F("Pressed 4"));
- LoopFolder();
- break;
- case 0xBF40FF00 :
- ir_button = 5;
- CurrentFolderNum = 5;
- CurrentTrackNum = 0;
- Serial.println(F("Pressed 5"));
- LoopFolder();
- break;
- case 0xBC43FF00 :
- ir_button = 6;
- CurrentFolderNum = 6;
- CurrentTrackNum = 0;
- Serial.println(F("Pressed 6"));
- LoopFolder();
- break;
- case 0xF807FF00 :
- ir_button = 7;
- CurrentFolderNum = 7;
- CurrentTrackNum = 0;
- Serial.println(F("Pressed 7"));
- LoopFolder();
- break;
- case 0xEA15FF00 :
- ir_button = 8;
- CurrentFolderNum = 8;
- CurrentTrackNum = 0;
- Serial.println(F("Pressed 8"));
- LoopFolder();
- break;
- case 0xF609FF00 :
- ir_button = 9;
- CurrentFolderNum = 9;
- CurrentTrackNum = 0;
- Serial.println(F("Pressed 9"));
- LoopFolder();
- break;
- case 0xE619FF00 :
- ir_button = 0;
- CurrentFolderNum = 0;
- CurrentTrackNum = 0;
- Serial.println(F("Pressed 0"));
- LoopFolder();
- break;
- case 0xE916FF00 :
- ir_button = 10;
- Serial.println(F("Pressed *"));
- SaveParameters2EEPROM();
- break;
- case 0xF20DFF00 :
- ir_button = 11;
- Serial.println(F("Pressed #"));
- DFPlayerMiniMp3.playRandomTrackFromAll();
- DFPlayerMiniMp3InterCommandsDelay();
- break;
- case 0xE718FF00 :
- ir_button = 12;
- Serial.println(F("Pressed Arrow Up"));
- IncreaseVolume();
- break;
- case 0xAD52FF00 :
- ir_button = 13;
- Serial.println(F("Pressed Arrow Down"));
- DecreaseVolume();
- break;
- case 0xF708FF00 :
- ir_button = 14;
- Serial.println(F("Pressed Arrow Left"));
- PlayPrevTrack();
- break;
- case 0xA55AFF00 :
- ir_button = 15;
- Serial.println(F("Pressed Arrow Right"));
- PlayNextTrack();
- break;
- case 0xE31CFF00 :
- ir_button = 16;
- Serial.println("Pressed Ok");
- if (PlayMode == DFPLAYER_MINI_MP3_PAUSED) {
- DFPlayerMiniMp3.start();
- DFPlayerMiniMp3InterCommandsDelay();
- PlayMode = DFPLAYER_MINI_MP3_PLAYING;
- }
- else {
- DFPlayerMiniMp3.pause();
- DFPlayerMiniMp3InterCommandsDelay();
- PlayMode = DFPLAYER_MINI_MP3_PAUSED;
- }
- break;
- default: // иначе просто печатать все обычные символы
- // Serial.println(irrecv.decodedIRData.decodedRawData, HEX);
- break;
- }
- irrecv.resume(); // Receive the next value
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement