#include <SD.h>
#include <TMRpcm.h>
// Define the pins for the buttons and SD card module
const int buttonPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3};
const int numButtons = 16;
// Define the file names for recorded audio
const char* audioFiles[] = {"audio1.wav", "audio2.wav", /* Add more file names as needed */};
TMRpcm tmrpcm; // Create an instance of the TMRpcm library
void setup() {
Serial.begin(9600);
// Initialize SD card
if (!SD.begin(10)) {
Serial.println("SD card initialization failed!");
return;
}
// Set up the audio module
tmrpcm.speakerPin = 9; // Change to the pin connected to your speaker module
tmrpcm.setVolume(5); // Adjust the volume if needed
// Set up button pins
for (int i = 0; i < numButtons; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
}
void loop() {
// Check each button for a press
for (int i = 0; i < numButtons; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
playAudio(i);
delay(500); // Debounce delay
}
}
}
void playAudio(int buttonIndex) {
if (buttonIndex < sizeof(audioFiles) / sizeof(audioFiles[0])) {
Serial.print("Playing: ");
Serial.println(audioFiles[buttonIndex]);
tmrpcm.play(audioFiles[buttonIndex]);
while (tmrpcm.isPlaying()) {
delay(100);
}
} else {
Serial.println("Invalid button index");
}
}