pleasedontcode

Bluetooth DFPlayer rev_01

Dec 7th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Bluetooth DFPlayer
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-12-07 11:36:17
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Create a project for controlling media playback */
  21.     /* with DFRobotDFPlayerMini connected to Arduino Uno, */
  22.     /* triggered through Bluetooth commands received from */
  23.     /* HC-05, with data stored or retrieved from the SD */
  24.     /* card. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. // Project for controlling media playback with Bluetooth commands via Bluetooth HC-05, with SD card support
  31. // using DFRobotDFPlayerMini, BluetoothSerial, and SoftwareSerial libraries
  32. #include <SoftwareSerial.h>
  33. #include <DFRobotDFPlayerMini.h>
  34. #include <BluetoothSerial.h>
  35. #include <SPI.h>
  36. #include <SD.h>
  37.  
  38. // Define pins for Bluetooth HC-05 module
  39. const uint8_t BT_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  40. const uint8_t BT_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  41. SoftwareSerial bluetoothSerial(BT_HC05_mySerial_PIN_SERIAL_RX_A1, BT_HC05_mySerial_PIN_SERIAL_TX_A0);
  42.  
  43. // Create DFPlayer Mini object
  44. DFRobotDFPlayerMini mediaPlayer;
  45.  
  46. // Bluetooth Serial object
  47. BluetoothSerial btSerial(Serial1, true);
  48.  
  49. // SD card chip select pin
  50. const int chipSelect = 4;
  51.  
  52. // Media control commands
  53. enum MediaCommand {
  54.   PLAY = '1',
  55.   PAUSE = '2',
  56.   STOP = '3',
  57.   NEXT = '4',
  58.   PREVIOUS = '5',
  59.   VOLUME_UP = '6',
  60.   VOLUME_DOWN = '7'
  61. };
  62.  
  63. // Buffer for incoming Bluetooth data
  64. String incomingData = "";
  65.  
  66. void setup() {
  67.   // Initialize serial communication for debugging
  68.   Serial.begin(115200);
  69.  
  70.   // Initialize Bluetooth serial
  71.   bluetoothSerial.begin(9600);
  72.   Serial.println("Bluetooth serial started at 9600") ;
  73.  
  74.   // Initialize SD card
  75.   if (!SD.begin(chipSelect)) {
  76.     Serial.println("SD card initialization failed!");
  77.     while (true); // halt if SD not found
  78.   }
  79.   Serial.println("SD card initialized.");
  80.  
  81.   // Initialize DFPlayer Mini
  82.   delay(1000); // Allow some time for initialization
  83.   mediaPlayer.begin(Serial);
  84.   delay(100); // Wait for the library to initialize
  85.   mediaPlayer.volume(20); // Set initial volume
  86.   Serial.println("DFPlayer Mini initialized.");
  87. }
  88.  
  89. void loop() {
  90.   // Check for Bluetooth data
  91.   if (bluetoothSerial.available()) {
  92.     char c = bluetoothSerial.read();
  93.     if (c == '\n') {
  94.       // Process command
  95.       processCommand(incomingData);
  96.       incomingData = ""; // Reset buffer
  97.     } else {
  98.       // Append character to buffer
  99.       incomingData += c;
  100.     }
  101.   }
  102. }
  103.  
  104. // Function to process received command
  105. void processCommand(String command) {
  106.   if (command.length() == 0) return;
  107.   char cmd = command.charAt(0);
  108.   switch (cmd) {
  109.     case MediaCommand.PLAY:
  110.       mediaPlayer.play(1); // Play first track, or implement specific logic
  111.       Serial.println("Play command received") ;
  112.       break;
  113.     case MediaCommand.PAUSE:
  114.       mediaPlayer.pause();
  115.       Serial.println("Pause command received") ;
  116.       break;
  117.     case MediaCommand.STOP:
  118.       mediaPlayer.stop();
  119.       Serial.println("Stop command received") ;
  120.       break;
  121.     case MediaCommand.NEXT:
  122.       mediaPlayer.next();
  123.       Serial.println("Next command received") ;
  124.       break;
  125.     case MediaCommand.PREVIOUS:
  126.       mediaPlayer.previous();
  127.       Serial.println("Previous command received") ;
  128.       break;
  129.     case MediaCommand.VOLUME_UP:
  130.       volumeUp();
  131.       Serial.println("Volume Up command received") ;
  132.       break;
  133.     case MediaCommand.VOLUME_DOWN:
  134.       volumeDown();
  135.       Serial.println("Volume Down command received") ;
  136.       break;
  137.     default:
  138.       Serial.print("Unknown command: ");
  139.       Serial.println(command);
  140.   }
  141. }
  142.  
  143. // Volume up function
  144. void volumeUp() {
  145.   int vol = mediaPlayer.readVolume();
  146.   if (vol < 30) { // Max volume
  147.     mediaPlayer.volume(vol + 1);
  148.   }
  149. }
  150.  
  151. // Volume down function
  152. void volumeDown() {
  153.   int vol = mediaPlayer.readVolume();
  154.   if (vol > 0) { // Min volume
  155.     mediaPlayer.volume(vol - 1);
  156.   }
  157. }
  158.  
  159. /* END CODE */
  160.  
Advertisement
Add Comment
Please, Sign In to add comment