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: Bluetooth DFPlayer
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-12-07 11:36:17
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Create a project for controlling media playback */
- /* with DFRobotDFPlayerMini connected to Arduino Uno, */
- /* triggered through Bluetooth commands received from */
- /* HC-05, with data stored or retrieved from the SD */
- /* card. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- // Project for controlling media playback with Bluetooth commands via Bluetooth HC-05, with SD card support
- // using DFRobotDFPlayerMini, BluetoothSerial, and SoftwareSerial libraries
- #include <SoftwareSerial.h>
- #include <DFRobotDFPlayerMini.h>
- #include <BluetoothSerial.h>
- #include <SPI.h>
- #include <SD.h>
- // Define pins for Bluetooth HC-05 module
- const uint8_t BT_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
- const uint8_t BT_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
- SoftwareSerial bluetoothSerial(BT_HC05_mySerial_PIN_SERIAL_RX_A1, BT_HC05_mySerial_PIN_SERIAL_TX_A0);
- // Create DFPlayer Mini object
- DFRobotDFPlayerMini mediaPlayer;
- // Bluetooth Serial object
- BluetoothSerial btSerial(Serial1, true);
- // SD card chip select pin
- const int chipSelect = 4;
- // Media control commands
- enum MediaCommand {
- PLAY = '1',
- PAUSE = '2',
- STOP = '3',
- NEXT = '4',
- PREVIOUS = '5',
- VOLUME_UP = '6',
- VOLUME_DOWN = '7'
- };
- // Buffer for incoming Bluetooth data
- String incomingData = "";
- void setup() {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- // Initialize Bluetooth serial
- bluetoothSerial.begin(9600);
- Serial.println("Bluetooth serial started at 9600") ;
- // Initialize SD card
- if (!SD.begin(chipSelect)) {
- Serial.println("SD card initialization failed!");
- while (true); // halt if SD not found
- }
- Serial.println("SD card initialized.");
- // Initialize DFPlayer Mini
- delay(1000); // Allow some time for initialization
- mediaPlayer.begin(Serial);
- delay(100); // Wait for the library to initialize
- mediaPlayer.volume(20); // Set initial volume
- Serial.println("DFPlayer Mini initialized.");
- }
- void loop() {
- // Check for Bluetooth data
- if (bluetoothSerial.available()) {
- char c = bluetoothSerial.read();
- if (c == '\n') {
- // Process command
- processCommand(incomingData);
- incomingData = ""; // Reset buffer
- } else {
- // Append character to buffer
- incomingData += c;
- }
- }
- }
- // Function to process received command
- void processCommand(String command) {
- if (command.length() == 0) return;
- char cmd = command.charAt(0);
- switch (cmd) {
- case MediaCommand.PLAY:
- mediaPlayer.play(1); // Play first track, or implement specific logic
- Serial.println("Play command received") ;
- break;
- case MediaCommand.PAUSE:
- mediaPlayer.pause();
- Serial.println("Pause command received") ;
- break;
- case MediaCommand.STOP:
- mediaPlayer.stop();
- Serial.println("Stop command received") ;
- break;
- case MediaCommand.NEXT:
- mediaPlayer.next();
- Serial.println("Next command received") ;
- break;
- case MediaCommand.PREVIOUS:
- mediaPlayer.previous();
- Serial.println("Previous command received") ;
- break;
- case MediaCommand.VOLUME_UP:
- volumeUp();
- Serial.println("Volume Up command received") ;
- break;
- case MediaCommand.VOLUME_DOWN:
- volumeDown();
- Serial.println("Volume Down command received") ;
- break;
- default:
- Serial.print("Unknown command: ");
- Serial.println(command);
- }
- }
- // Volume up function
- void volumeUp() {
- int vol = mediaPlayer.readVolume();
- if (vol < 30) { // Max volume
- mediaPlayer.volume(vol + 1);
- }
- }
- // Volume down function
- void volumeDown() {
- int vol = mediaPlayer.readVolume();
- if (vol > 0) { // Min volume
- mediaPlayer.volume(vol - 1);
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment