macca-nz

test_df_player

Nov 7th, 2026
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.77 KB | Source Code | 0 0
  1. // This is a simple test sketch to see if your DF Player works and see what checksum it uses
  2.  
  3. #include <SoftwareSerial.h>
  4.  
  5. #define TX_PIN 9
  6. #define RX_PIN 8
  7.  
  8. const size_t RX_PACKET_SIZE = 10;
  9. const uint8_t startByte = 0x7E, ver = 0xFF, len = 0x06, endByte = 0xEF;
  10. int rxBuffer[RX_PACKET_SIZE], rxIndex = 0;
  11.  
  12.  
  13. SoftwareSerial dfSerial(RX_PIN, TX_PIN);    // (RX, TX) pins
  14.  
  15. void sendPacket(byte cmd, byte feedBack, byte folder, byte file){
  16.   uint8_t txBuffer[]{startByte, ver, len, cmd, feedBack, folder, file, endByte};
  17.   Serial.print("Setting bytes :");
  18.   for(byte i = 0; i < 8; i++){
  19.     dfSerial.write(txBuffer[i]);
  20.     Serial.print("0x");
  21.     Serial.print(txBuffer[i], HEX);
  22.     Serial.print("  ");
  23.     delay(10);
  24.   }
  25.   Serial.println("\n");
  26. }
  27.  
  28. void setup() {
  29.   // Initialize serial communication at 115200 baud
  30.   Serial.begin(115200);
  31.   while (!Serial) {;}
  32.   Serial.println("Setting up DFPlayer Serial Connection\n");
  33.   dfSerial.begin(9600);
  34.   delay(1000);
  35.   while (!dfSerial) {;}
  36.   Serial.println("DFPlayer Serial Up\n");
  37.   sendPacket(0x0C, 0, 0, 0);    //This is the reset command
  38.   delay(500);
  39.   sendPacket(0x09, 0, 0, 2);    //This set the source to the TF card = 2, USB = 1
  40.   delay(500);
  41.   sendPacket(0x06, 0, 0, 25);    //This sets the Volume (0 - 30)
  42.   delay(500);
  43.   sendPacket(0x03, 1, 0, 1);    //This plays file 001.mp3 in the root of the SD card and will reply to confirm
  44. }
  45.  
  46. void loop() {
  47.   while (dfSerial.available() > 0) {
  48.     uint8_t incomingByte = dfSerial.read();
  49.  
  50.     // STEP 1: If we are not actively tracking a packet, wait until we see 0x7E
  51.     if (rxIndex == 0) {
  52.       if (incomingByte == startByte) {
  53.         rxBuffer[0] = incomingByte;
  54.         rxIndex = 1; // Move to the next slot for the next byte
  55.       }
  56.       // If it's not 0x7E, we do nothing and let the loop check the next byte
  57.     }
  58.    
  59.     // STEP 2: We are actively collecting the packet
  60.     else if (rxIndex < RX_PACKET_SIZE) {
  61.       rxBuffer[rxIndex] = incomingByte;
  62.      
  63.       // STEP 3: If we just grabbed the last byte (index 9), validate the marker
  64.       if (rxIndex == RX_PACKET_SIZE -1) {
  65.         if (rxBuffer[RX_PACKET_SIZE - 1] == endByte) {
  66.           // Success! Valid 10-byte frame found.
  67.           for(byte i = 0; i < RX_PACKET_SIZE; i++){
  68.             Serial.print("0x");
  69.             Serial.print(rxBuffer[i], HEX);
  70.             Serial.print("  ");
  71.           }
  72.           Serial.println("\n");
  73.         } else {
  74.           // Error: The 10th byte wasn't 0xEF. Frame is corrupt.
  75.           // Optional: handle error here
  76.           Serial.println("Data receive ERROR\n");
  77.         }
  78.        
  79.         // Reset the index so we start looking for a new 0x7E on subsequent bytes
  80.         rxIndex = 0;
  81.       } else {
  82.         rxIndex++;
  83.       }
  84.     }
  85.   }
  86. }
  87.  
Tags: DFPlayer
Advertisement
Add Comment
Please, Sign In to add comment