I'm attempting to make my Arduino Uno play different songs from an MicroSD Card when I press different buttons. However, when I upload the code to the Arduino, the speaker starts playing music. What could be the issue with my code? Or is it possibly a problem with the Arduino or the speaker? Additionally, the Arduino doesn't seem to detect button presses.
To read the Micro SD Card I use a MicroSD Card Module and I use the TMRpcm Library.
This is my code:
#include <SD.h>
#include <TMRpcm.h>
TMRpcm tmrpcm; // Creating a TMRpcm object
// Buttons for the animals using analog pins A0 to A4
const int apeButton = A0; // Button for ape
const int elephantButton = A1; // Button for elephant
const int lionButton = A2; // Button for lion
const int parrotButton = A3; // Button for parrot
const int tigerButton = A4; // Button for tiger
// File names
const char *apeFiles[] = { "Ape.wav", "Julius.wav", "FemAper.wav" };
const char *elephantFiles[] = { "MasjerendeElefant.wav", "Elefant.wav" };
const char *lionFiles[] = { "Love.wav", "Lovesangen.wav" };
const char *parrotFiles[] = { "Papegoye.wav" };
const char *tigerFiles[] = { "Tiger.wav" };
// Number of animal songs
const int numApeSongs = 3;
const int numElephantSongs = 2;
const int numLionSongs = 2;
const int numTigerSongs = 1;
const int numParrotSongs = 1;
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
// Initialize SD card
if (!SD.begin(10)) {
Serial.println("Failed to initialize SD card!");
while (1);
}
Serial.println("SD card initialized.");
// Pin configuration for the buttons
pinMode(apeButton, INPUT_PULLUP);
pinMode(elephantButton, INPUT_PULLUP);
pinMode(lionButton, INPUT_PULLUP);
pinMode(tigerButton, INPUT_PULLUP);
pinMode(parrotButton, INPUT_PULLUP);
// Initialize speaker
tmrpcm.speakerPin = 9; // Set speaker pin
tmrpcm.setVolume(5); // Set volume (0 to 7)
}
void loop() {
// Read the state of the buttons
int apeButtonState = digitalRead(apeButton);
int elephantButtonState = digitalRead(elephantButton);
int lionButtonState = digitalRead(lionButton);
int parrotButtonState = digitalRead(parrotButton);
int tigerButtonState = digitalRead(tigerButton);
// Wait until a button is pressed
while (apeButtonState == HIGH && elephantButtonState == HIGH && lionButtonState == HIGH && parrotButtonState == HIGH && tigerButtonState == HIGH) {
delay(100); // Add a small delay to avoid CPU overload
apeButtonState = digitalRead(apeButton);
elephantButtonState = digitalRead(elephantButton);
lionButtonState = digitalRead(lionButton);
parrotButtonState = digitalRead(parrotButton);
tigerButtonState = digitalRead(tigerButton);
}
// Select a random song and play it depending on which button was pressed
if (apeButtonState == LOW) {
playRandomSong(apeFiles, numApeSongs);
} else if (elephantButtonState == LOW) {
playRandomSong(elephantFiles, numElephantSongs);
} else if (lionButtonState == LOW) {
playRandomSong(lionFiles, numLionSongs);
} else if (parrotButtonState == LOW) {
playRandomSong(parrotFiles, numParrotSongs);
} else if (tigerButtonState == LOW) {
playRandomSong(tigerFiles, numTigerSongs);
}
// Wait until the button is released before continuing
while (apeButtonState == LOW || elephantButtonState == LOW || lionButtonState == LOW || parrotButtonState == LOW || tigerButtonState == LOW) {
delay(100);
apeButtonState = digitalRead(apeButton);
elephantButtonState = digitalRead(elephantButton);
lionButtonState = digitalRead(lionButton);
parrotButtonState = digitalRead(parrotButton);
tigerButtonState = digitalRead(tigerButton);
}
}
// Function to play a random song from a given list of songs
void playRandomSong(const char **fileList, int numSongs) {
int songIndex = random(0, numSongs);
const char* filename = fileList[songIndex];
if (!SD.exists(filename)) {
Serial.println("File not found.");
return;
}
tmrpcm.play(filename); // Play the sound from the file
}