Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: DFPlayer Control
- - Source Code NOT compiled for: Arduino Nano
- - Source Code created on: 2024-09-22 21:15:57
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The Haunted Radio project utilizes the DFRobot */
- /* DFPlayer Mini for audio playback, controlled via */
- /* SoftwareSerial on pins A0 and A1, enabling */
- /* seamless integration of sound effects in a haunted */
- /* theme. Play track 1. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <SoftwareSerial.h>
- #include <DFRobotDFPlayerMini.h> // https://github.com/DFRobot/DFRobotDFPlayerMini
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF Software Serial *****/
- const uint8_t DFP_DFPlayerMini_Serial_PIN_SERIAL_TX_A0 = A0; // TX pin for SoftwareSerial
- const uint8_t DFP_DFPlayerMini_Serial_PIN_SERIAL_RX_A1 = A1; // RX pin for SoftwareSerial
- SoftwareSerial DFP_DFPlayerMini_Serial(DFP_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, DFP_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- DFRobotDFPlayerMini myDFPlayer; // Instance of the DFPlayer Mini class
- void setup(void)
- {
- // Initialize the software serial port for DFPlayer Mini
- DFP_DFPlayerMini_Serial.begin(9600);
- // Initialize the serial monitor for debugging
- Serial.begin(115200);
- // Wait for a moment to allow the DFPlayer to initialize
- Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
- if (!myDFPlayer.begin(DFP_DFPlayerMini_Serial, true, true)) { // Use serial to communicate with the DFPlayer
- 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) {
- delay(0); // Infinite loop to halt execution
- }
- }
- Serial.println(F("DFPlayer Mini online."));
- myDFPlayer.volume(10); // Set volume (0-30)
- myDFPlayer.play(1); // Play the first mp3
- }
- void loop(void)
- {
- static unsigned long timer = millis();
- // Play next mp3 every 3 seconds
- if (millis() - timer > 3000) {
- timer = millis();
- myDFPlayer.next(); // Play next mp3
- }
- // Check if the DFPlayer has available data to read
- if (myDFPlayer.available()) {
- printDetail(myDFPlayer.readType(), myDFPlayer.read()); // Print details of the DFPlayer status
- }
- }
- // Function to print details about the DFPlayer status
- void printDetail(uint8_t type, int value) {
- switch (type) {
- case DFPlayerCardInserted:
- Serial.println(F("Card Inserted!"));
- break;
- case DFPlayerCardRemoved:
- Serial.println(F("Card Removed!"));
- break;
- case DFPlayerPlayFinished:
- Serial.print(F("Number:"));
- Serial.print(value);
- Serial.println(F(" Play Finished!"));
- break;
- case DFPlayerError:
- switch (value) {
- case Busy:
- Serial.println(F("Card not found"));
- break;
- case Sleeping:
- Serial.println(F("Sleeping"));
- break;
- case FileIndexOut:
- Serial.println(F("File Index Out of Bound"));
- break;
- default:
- break;
- }
- break;
- default:
- break;
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement