Hi all,
I'm doing a school project where touch paint sensors on a painting trigger a wave file to be played from a speaker. I'm using an Elegoo Mega with an SD card reader and small GIKFUN 3W speaker.
Grumpy_Mike if you are out there this is very similar to one you solved for someone else.
My code plays the tone through the speaker. The SD card read works, and It says it's playing the tes2.wav... and even the loop that checks if audio is playing comes back true. But I get no sound (aside from the test tone)
I feel like I am missing something obvious!
Below is my code, any suggestions are welcome! (Apologies for the bad photos!)
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 53
TMRpcm tmrpcm;
void setup() {
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD card initialization failed");
return;
}
tmrpcm.speakerPin = 9; // Use pin 9 for audio playback
tmrpcm.setVolume(5); // Set the volume to a mid-high level
// Now play a WAV file
if (SD.exists("tes2.wav")) {
// First, play a tone
tone(9, 1000, 1000); // Play 1000 Hz tone for 1000 ms
delay(1500); // Wait for the tone to finish plus a buffer
noTone(9); // Stop any ongoing tones
pinMode(9, OUTPUT); // Reinitialize pin mode
tmrpcm.play("tes2.wav"); // Play the WAV file
delay(3000); // Optional: delay while audio plays
} else {
Serial.println("File tes2.wav not found.");
}
}
void loop() {
// If you need to continuously check or react to the audio state, do it here.
if (tmrpcm.isPlaying()) {
Serial.println("Audio is still playing...");
// Additional logic if needed while audio is playing
} else {
// Actions to perform once audio playback is complete
Serial.println("Playback has completed or file was not found.");
// You might want to loop playback, handle errors, etc.
}
delay(1000); // Adjust timing or actions according to your application needs
}