想通过Arduino Nano,基本某种限定条件下,控制MP3的播放与停止。我选择的是利用串口模式的控制。在此之前,测试了DFRobotDFPlayerMini库和DFPlayer_Mini_Mp3_by_Makuna库。这两个库都可以正常的控制指定MP3的播放与停止。但当我偿试加入条件语句进行判别并决定MP3的播放与停止时,我发现扬声器并不能够正确播放或停止音乐。感觉条件语句一直在被执行。现将基于DFPlayer_Mini_Mp3_by_Makuna库的代码发上来,请指教。
- include <VarSpeedServo.h>
- #include <SoftwareSerial.h>
- #include <DFMiniMp3.h>
- class Mp3Notify
- {
- public:
- static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action)
- {
- if (source & DfMp3_PlaySources_Sd)
- {
- Serial.print("SD Card, ");
- }
- if (source & DfMp3_PlaySources_Usb)
- {
- Serial.print("USB Disk, ");
- }
- if (source & DfMp3_PlaySources_Flash)
- {
- Serial.print("Flash, ");
- }
- Serial.println(action);
- }
- static void OnError(uint16_t errorCode)
- {
- // see DfMp3_Error for code meaning
- Serial.println();
- Serial.print("Com Error ");
- Serial.println(errorCode);
- }
- static void OnPlayFinished(DfMp3_PlaySources source, uint16_t track)
- {
- Serial.print("Play finished for #");
- Serial.println(track);
- }
- static void OnPlaySourceOnline(DfMp3_PlaySources source)
- {
- PrintlnSourceAction(source, "online");
- }
- static void OnPlaySourceInserted(DfMp3_PlaySources source)
- {
- PrintlnSourceAction(source, "inserted");
- }
- static void OnPlaySourceRemoved(DfMp3_PlaySources source)
- {
- PrintlnSourceAction(source, "removed");
- }
- };
-
- VarSpeedServo barrier_1;
- VarSpeedServo barrier_2;
- // I/O PINS
- #define POWER_SERVO 4 //舵机供电控制引脚4,HIGH舵机可用,LOW舵机关闭
- #define ALARM_1 2
- #define ALARM_2 3
- #define SENSOR_LEFT A0
- #define SENSOR_RIGHT A1
- int IRthreshold = 500;
- unsigned int sen1_Val,sen2_Val;
- bool trigger_s1 = false, trigger_s2 = false, latch_s1, latch_s2;
- bool crossing_blocked;
- static unsigned long millisPower;
- bool alarmSignals, tick_blink;
- SoftwareSerial secondarySerial(7, 8); // RX, TX
- DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);
-
- void setup()
- {
- //MP3 初始化
- Serial.begin(115200);
-
- Serial.println("initializing...");
- mp3.begin();
- uint16_t volume = mp3.getVolume();
- Serial.print("volume ");
- Serial.println(volume);
- mp3.setVolume(24);
-
- uint16_t count = mp3.getTotalTrackCount(DfMp3_PlaySource_Sd);
- Serial.print("files ");
- Serial.println(count);
-
- Serial.println("starting...");
- // Initialize Servos 舵机初始化
- pinMode(LED_BUILTIN, OUTPUT);
- pinMode(ALARM_1, OUTPUT);
- pinMode(ALARM_2, OUTPUT);
- pinMode(POWER_SERVO, OUTPUT);
- pinMode(SENSOR_LEFT,INPUT_PULLUP);
- pinMode(SENSOR_RIGHT,INPUT_PULLUP);
- barrier_1.attach(5); //舵机控制引脚为5和6
- barrier_2.attach(6);
-
- /*栏杆初始动作-抬杆
- 舵机控制-“栏杆”动作说明:
- barrier_X.write(XXX-运动角度,XX-(移动速度,值从低到高为0~255,程序设定速度为32),bool值:Ture等待动作完成,false不等动作完成)
- SERVO 1 (HOUSE) UP: 165 | DOWN: 25
- SERVO 2 (CORNER) UP: 170 | DOWN: 35
- Speed 0 or 255: maximum, 1-254 from lower to higher speed */
- digitalWrite(POWER_SERVO, HIGH);
- digitalWrite(LED_BUILTIN, HIGH);
- delay(30);
- barrier_1.write(180,32,false);
- delay(30);
- barrier_2.write(180,32,false);
- millisPower = millis();
- }
-
- void loop()
- {
- mp3.loop();
- delay(1);
- if (analogRead(SENSOR_LEFT) <= IRthreshold) mp3.loopGlobalTrack(1);//这里,如果不加入条件语句,mp3.loopGlobalTrack(1)和mp3.stop()将被依次执行,而加入条件语句时,上述功能不能正常使用
- else mp3.stop();
- }
复制代码
|