And contrary to the Serial Monitor, I do have three short wav files on the SD card.
Code: Select all
Starting setup...
SD Card initialized.
I2S output initialized.
Root directory opened.
No WAV files found.
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400d1be4 PS : 0x00060630 A0 : 0x800d63ec A1 : 0x3ffb2270
A2 : 0x3ffc2640 A3 : 0x3ffc2620 A4 : 0x00000000 A5 : 0x3ffc2664
A6 : 0x3ffb8188 A7 : 0x80000001 A8 : 0x800d1da0 A9 : 0x3ffb2210
A10 : 0x00000000 A11 : 0x00000013 A12 : 0x3f404e11 A13 : 0x3f40a4bf
A14 : 0x3f40a4bf A15 : 0x3ffc2620 SAR : 0x00000019 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000 LBEG : 0x40084551 LEND : 0x40084559 LCOUNT : 0x00000027
Backtrace: 0x400d1be1:0x3ffb2270 0x400d63e9:0x3ffb2290
ELF file SHA256: dd159258c1c8dc84
Rebooting...
Here is my full code:
Code: Select all
#include <Arduino.h>
#include <AudioFileSourceSD.h>
#include <AudioGeneratorWAV.h>
#include <AudioOutputI2S.h>
#include <SD.h> // Include the SD library
// Pin definitions for the I2S DAC
#define I2S_BCLK_PIN 26 // Bit clock pin for I2S
#define I2S_LRC_PIN 25 // Left/right channel clock pin for I2S
#define I2S_DOUT_PIN 22 // Data out pin for I2S
// Pin definitions for the SD card module
#define SD_CS_PIN 2 // Chip select (CS) pin
#define SD_MOSI_PIN 23 // MOSI pin for SPI communication
#define SD_MISO_PIN 19 // MISO pin for SPI communication
#define SD_SCK_PIN 18 // Clock pin for SPI communication
AudioGeneratorWAV *wav;
AudioOutputI2S *out;
AudioFileSourceSD *file;
File root;
bool openNextWavFile() {
File entry = root.openNextFile();
while (entry) {
// Check if the file is a WAV file based on the extension
if (entry.name() && strstr(entry.name(), ".WAV")) {
const char *filePath = entry.name();
Serial.print("Playing WAV file: ");
Serial.println(filePath);
// Open the WAV file
file = new AudioFileSourceSD();
if (file->open(filePath)) {
wav = new AudioGeneratorWAV();
if (wav->begin(file, out)) {
// Successfully opened the WAV file
Serial.println("WAV file playback started.");
return true;
} else {
Serial.println("Could not start WAV file playback");
delete wav;
delete file;
}
} else {
Serial.println("Could not open WAV file");
delete file;
}
}
entry = root.openNextFile();
}
// No more WAV files found
return false;
}
void setup() {
Serial.begin(115200);
Serial.println("Starting setup...");
// Set the pin modes manually
pinMode(SD_CS_PIN, OUTPUT);
pinMode(SD_MOSI_PIN, OUTPUT);
pinMode(SD_MISO_PIN, INPUT);
pinMode(SD_SCK_PIN, OUTPUT);
// Initialize the SD library
if (!SD.begin(SD_CS_PIN)) {
Serial.println("SD Card initialization failed");
while (1); // Halt program execution if SD initialization fails
}
Serial.println("SD Card initialized.");
// Initialize the I2S output
out = new AudioOutputI2S();
out->SetOutputModeMono(true); // Set to true for mono audio
Serial.println("I2S output initialized.");
// Open the root directory of the SD card
root = SD.open("/");
if (!root) {
Serial.println("Could not open root directory");
while (1); // Halt program execution if root directory cannot be opened
}
Serial.println("Root directory opened.");
// Try to open the first WAV file
if (openNextWavFile()) {
Serial.println("Playing WAV file...");
} else {
Serial.println("No WAV files found.");
root.close();
}
}
void loop() {
// Check if the WAV file is still playing
if (wav->isRunning()) {
if (!wav->loop()) {
// If the file has finished playing, close it and try the next one
wav->stop();
delete wav;
delete out;
delete file;
Serial.println("WAV file playback finished.");
// Try to open the next WAV file
if (!openNextWavFile()) {
Serial.println("No more WAV files found.");
root.close();
}
}
}
}