ESP32-S3 - No playing sound
Posted: Thu Sep 21, 2023 12:51 pm
Hello,
I don't understand why my code is not working.
The mp3 is not played and the name of the musics are displayed on the screen but very very fast.
Do you have any idea?
here my code :
I don't understand why my code is not working.
The mp3 is not played and the name of the musics are displayed on the screen but very very fast.
Do you have any idea?
here my code :
Code: Select all
#include "Arduino.h"
#include "Audio.h"
#include "FS.h"
#include "SD_MMC.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// Pin definitions
#define SD_MMC_CMD 38
#define SD_MMC_CLK 39
#define SD_MMC_D0 40
#define I2S_BCLK 14
#define I2S_DOUT 13
#define I2S_LRC 12
#define SDA 19 // Define SDA pins
#define SCL 20 // Define SCL pins
#define PIN_BUTTON 5
Audio audio;
LiquidCrystal_I2C lcd(0x27,16,2);
File root;
File currentFile;
void setup() {
Serial.begin(115200);
// Initialize the I2C LCD
Wire.begin(SDA, SCL);
if (!i2CAddrTest(0x27)) {
lcd = LiquidCrystal_I2C(0x3F, 16, 2);
}
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Croc Blanc");
// Initialize the SD card
SD_MMC.setPins(SD_MMC_CLK, SD_MMC_CMD, SD_MMC_D0);
if (!SD_MMC.begin("/sdcard", true, true, SDMMC_FREQ_DEFAULT, 5)) {
Serial.println("Card Mount Failed");
return;
}
root = SD_MMC.open("/");
if (!root) {
Serial.println("Failed to open root directory");
return;
}
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(9); // Adjust the volume as needed
playNextMP3();
}
void loop() {
audio.loop();
if (digitalRead(PIN_BUTTON) == LOW) {
delay(50); // Debounce
playNextMP3();
}
}
void playNextMP3() {
if (currentFile) {
currentFile.close();
}
File entry;
while ((entry = root.openNextFile())) {
if (!entry.isDirectory() && entry.name()) {
currentFile = entry;
// Display the file name on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Now Playing:");
lcd.setCursor(0, 1);
lcd.print(currentFile.name());
// Play the MP3 file
audio.connecttoFS(SD_MMC, currentFile.name());
return;
}
}
// No more MP3 files to play, rewind to the beginning
root.rewindDirectory();
}
bool i2CAddrTest(uint8_t addr) {
Wire.begin();
Wire.beginTransmission(addr);
if (Wire.endTransmission() == 0) {
return true;
}
return false;
}