Arduino soundboard with pushbutton

The project I’m trying to develop is quite simple, I have an SD shield and card containing twenty audio files and a pushbutton. Every time the button is pushed, it generates a random number and uses that to play one of the files at random. The problem I am having is that pushing the button it prints several lines to the serial and no sound is played. The SD card is recognized and read, individual sounds can be played and the push button works but it can get stuck in a loop. Is there something wrong with my code?

randomNumber.ino (1.87 KB)

#include <SD.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 11//4 
const int speakerPin=9;
char* file="file1.wav";
const int buttonPin = 8;

unsigned long previousTime=0;
unsigned long interval=1000;
int select=1;
int buttonState = 0;

TMRpcm tmrpcm;

//random number
int randomNumber = 3;

void setup() {

  //if (!SD.begin(SD_ChipSelectPin)) {Serial.println("SD fail"); return; } //check SD shield

  tmrpcm.speakerPin = speakerPin;


void loop() {

    case 1: file = "file1.wav"; break;
    case 2: file = "file2.wav"; break;
    case 3: file = "file3.wav"; break;
    case 4: file = "file4.wav"; break;
    case 5: file = "file5.wav"; break;
    case 6: file = "file6.wav"; break;
    case 7: file = "file7.wav"; break;
    case 8: file = "file8.wav"; break;
    case 9: file = "file9.wav"; break;
    case 10: file = "file10.wav"; break;
    case 11: file = "file11.wav"; break;
    case 12: file = "file12.wav"; break;
    case 13: file = "file13.wav"; break;
    case 14: file = "file14.wav"; break;
    case 15: file = "file15.wav"; break;
    case 16: file = "file16.wav"; break;
    case 17: file = "file17.wav"; break;
    case 18: file = "file18.wav"; break;
    case 19: file = "file19.wav"; break;
    case 20: file = "file20.wav"; break;

  buttonState = digitalRead(buttonPin);
  if(buttonState == LOW){
    randomNumber = random(1,20);
    Serial.print("Playing:  ");

void activateOutput(){

/* function activateOutput */

Serial.println("- is playing");;

//while(tmrpcm.isPlaying()){}//wait until file is played

tmrpcm.stopPlayback(); // or wait 1sec and stop music


This is the code

  1. The lines that might play the file are commented out,
  2. random(1,20) will never generate the value 20,
  3. it's a little odd testing the value randomNumber at the top of loop and only generating it later
  4. you have a function activateOutput() that is never called from anywhere.


This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.