Download library
https://codeload.github.com/DFRobot/DFPlayer-Mini-mp3/zip/master
Set serial port permission on Ubuntu.
Arduino UNO
$ sudo chmod a+rw /dev/ttyACM0
Arduino NANO
$ sudo chmod a+rw /dev/ttyUSB0
Micro SD Card
16GB, FAT32
File: 0001.mp3, 0002.mp3, …
Resistors
1k
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include "Arduino.h" #include "SoftwareSerial.h" #include <DFPlayer_Mini_Mp3.h> #define ON 1 #define OFF 0 #define SERIAL_BAUD 9600 #define STATUS_LED 11 #define REED_SWITCH 2 #define MP3_TX 8 #define MP3_RX 9 #define SOUND_VOLUME 24 // 0-30 void setup() { // reed switch Serial.begin(SERIAL_BAUD) ; pinMode(STATUS_LED, OUTPUT); pinMode(STATUS_LED, OUTPUT); pinMode(REED_SWITCH, INPUT_PULLUP); // mp3 module SoftwareSerial mySoftwareSerial(MP3_TX, MP3_RX); // RX, TX mySoftwareSerial.begin(SERIAL_BAUD); mp3_set_serial(mySoftwareSerial); //set softwareSerial for DFPlayer-mini mp3 module mp3_set_volume(SOUND_VOLUME); } unsigned long prev = 0; int playFlag = OFF; int powerFlag = OFF; void loop() { if (powerFlag == OFF) { powerFlag = ON; digitalWrite(STATUS_LED, ON); delay(100); digitalWrite(STATUS_LED, OFF); delay(100); digitalWrite(STATUS_LED, ON); delay(100); digitalWrite(STATUS_LED, OFF); delay(100); digitalWrite(STATUS_LED, ON); } int x; unsigned long current = millis(); if (abs(current - prev) > 1000) { prev = current; x = digitalRead(REED_SWITCH); if (x == 0) { digitalWrite(STATUS_LED, OFF); delay(200); digitalWrite(STATUS_LED, ON); delay(200); if (playFlag == OFF) { playFlag = ON; mp3_random_play(); } } else { digitalWrite(STATUS_LED, ON); playFlag = OFF; mp3_stop(); } Serial.println(x); Serial.flush(); } } |