playing mp3 sounds with a button press with some arguments.

Is this project feasible? I am a school teacher and I want to develop a sound matching game (like concentration). I have made 44 doors (22 pairs) and when the door opens it activates a momentary contact switch. I want the door when opened to play a mp3 sound. (like yippee). I can’t use a straight trigger because if the two doors do not match another sound like ‘oops’ plays or if the doors do match another sound like ‘congrats plays’. So some logic needs to apply. I have done a lot of research on the net but can not find a tutorial on exactly what I need.
Will this equipment do the job?
I have a mega 2560
A duinotech mp3 recording module (xc-4516) (To play the sounds)
A duinotech usb host expansion board (xc-4456) (To store the sound files)
A duinotech 2 x 3w amplifier module(xc-4448) To amplify the sounds.
My thinking is that on start up a variable called door default will hold the number 1.
When a child choose and then opens a door it plays its sound then checks the variable door default.
If it is 1 it means no other door has been opened. This door changes the variable door default to the number of the door opened say 5.
If it is 5 then it means a previous door 5 was opened so it is a match so the success sound is played and then the variable door default is changed back to 1 ready for the next round.
If it is not 5 then (it can’t be 1 due to the first statement) then it is not a match so play the sound ‘oops’ and change the variable door default back to 1.
Am I thinking straight???
Hope some one can help or point me in the right direction.

Adafruit makes an WAV shield that would allow you to play sounds directly, without having to go through a PC. Has a built-in amplifier. Plays audio files on an SD card. You do have to build (solder) it.

I think your biggest challenge is going to be interfacing with the doors. You have 44 doors? Does each have a separate switch already? If not, you might be able to build something using a reed switch and magnet for each door. The Arguino Mega has 50+ i/o pins, so 44 doors is probably doable (depending how many are used by your sound player). If not, there are ways to expand the number of ports available with external circuitry. I'm guessing the best way to sense each door might be to enable the pull up resitors on each of the input pins connected to the doors, then connect each door switch between an input pin and ground.

The programming is certainly doable. I would put the switch checking in a loop. Have a variable for each door, and check the doors sequentially in the code (by reading the input value of the associated pin). When you detect that a door has been opened in the loop, set that variable. Then look at what other doors have already been opened (based on the variables) and play the appropriate sound (congratulations or oops), then reset the variables for next time. If that sounds unclear, I can help you with the logic a bit more.

Thank you for your time. I think the mp3 player you suggested might be what I am looking for. I have built momentary contact switches into the bottom of the doors. The door opens in a down ward motion. When the door hits the switch contact is made and when the door is released there is enough force in the contacts spring to open the switch again as long as you take your hand off the door! I have started the sketch. I assume I will have to assign pins as out puts to send information about what sound to play to mp3 player. If I am on the wrong track here please let me know. Appreciate any help. /*

Reduce the risk. A road sign safety awareness concentration style game used to educate young cyclists. The board has 45 small doors. On front of each door is a safety sign particularly focused on warning cyclists. (Yes I have found 45 different ones!) Behind the door is a character. Students have to choose two doors to open and try and find the matching character. Each door paired set when opened, also shares a matching sound. When a door is opened it plays its own unique sound. Each pin has two doors attached (each with its own momentary contact switch )to it except pin 43 which has one door. If the doors match a reward sound plays. If the second door opened does not match the first door opened another sound plays. */ //variables will change: int doordefault = 0 // a variable on start up set to 0 so no door has been opened in a sequence. This will change when doors are opened and continually reset. // constants won't change. They're used here to // set pin numbers const int door1pin = 22 //name of doors, when door is open it pushes a contact switch. const int door2pin = 23 const int door3pin = 24 const int door4pin = 25 const int door5pin =26 const int door6pin =27 const int door7pin =28 const int door8pin =29 const int door9pin =30 const int door10pin =31 const int door11pin =32 const int door12pin =33 const int door13pin =34 const int door14pin =35 const int door15pin =36 const int door16pin =38 const int door17pin =39 const int door18pin =40 const int door19pin =41 const int door20pin =42 const int door21pin =43 const int door22pin =44 const int door23pin =45

void setup() { // initialize the pushbutton pin as an input: pinMode(door1pin, INPUT); pinMode(door2pin, INPUT); pinMode(door3pin, INPUT); pinMode(door4pin, INPUT); pinMode(door5pin, INPUT); pinMode(door6pin, INPUT); pinMode(door7pin, INPUT); pinMode(door8pin, INPUT); pinMode(door9pin, INPUT); pinMode(door10pin, INPUT); pinMode(door11pin, INPUT); pinMode(door12pin, INPUT); pinMode(door13pin, INPUT); pinMode(door14pin, INPUT); pinMode(door15pin, INPUT); pinMode(door16pin, INPUT); pinMode(door17pin, INPUT); pinMode(door18pin, INPUT); pinMode(door19pin, INPUT); pinMode(door20pin, INPUT); pinMode(door21pin, INPUT); pinMode(door22pin, INPUT); pinMode(door23pin, INPUT); pinMode(door24pin, INPUT); pinMode(door25pin, INPUT); pinMode(door26pin, INPUT); pinMode(door27pin, INPUT); pinMode(door28pin, INPUT); pinMode(door29pin, INPUT); pinMode(door30pin, INPUT); pinMode(door31pin, INPUT); pinMode(door32pin, INPUT); pinMode(door33pin, INPUT); pinMode(door34pin, INPUT); pinMode(door35pin, INPUT); pinMode(door36pin, INPUT); pinMode(door37pin, INPUT); pinMode(door38pin, INPUT); pinMode(door39pin, INPUT); pinMode(door40pin, INPUT); pinMode(door41pin, INPUT); pinMode(door42pin, INPUT); pinMode(door43pin, INPUT); pinMode(door44pin, INPUT); pinMode(door45pin, INPUT); //This pin has only one door attatched to hence one momentary contact switch, it as it does not have a pair.

}

void loop() { // put your main code here, to run repeatedly:

}

That code is just silly. Use for loops and arrays to reduce all that to about six lines tops.

The project is too much for your current skill set. Try learning more code first.

Also how are you wiring your switches? You can save on lots of resistors if you use the internal pull up resistors

Giving a rating out of 10 my skill level in this area is not even 1 (Newbie)! Am working through the tutorials. Came across the Knight rider example. Looks like I am following the number 1 example. To get to example 2 which is about loops and arrays is a huge leap in understanding for sure. I shall preserver! Yep know about the internal pull up resistors. Thx.

Here is the code I have come up with assistance for the door game so far. It compiles but when uploaded nothing happens no matter what button I push. Instead of sounds I am using coloured LEDs.

/*I have 4 momentary contact switches connected to pins 22, 23, 24, 25. (in the game there will be 45)I have three LEDs (later I want to replace these with a sounds) white, green and red.

  • Pin 22 is a pair with 24 and pin 23 is a pair with 25. On any switch push the first time I want the white led to light then turn off. (Have not coded this yet!) Then when another switch
  • is pressed I want the white led to come on then if the first switch pressed was 22 and the 2nd switch pressed was 24 the green led comes on else if it it was
  • 23 or 25 the red led with come on. After a two press sequence I want to reset the switches again.
    */
int switchPins[] = {22, 23, 24,25}; 
int pinCount = 4;
int ledPins[] = {44, 45, 46};//ledPin 44 white LED, a switch had opened. ledPin 45 a green LED, matching switch. ledPin 46 a red LED, non matching switch.
int ledpinCount = 3;
bool arr[4];//variable to hold what switches were pressed
int switchOpenedCount = 4;



void setup() {

for (int thisPin = 0; thisPin < pinCount; thisPin++) {
  pinMode(switchPins[thisPin], INPUT_PULLUP);
}
for (int doorPin = 0; doorPin < ledpinCount; doorPin++){
  pinMode (ledPins[doorPin], OUTPUT);
  digitalWrite(ledPins[doorPin],LOW);//sets all LEDs to low.
}
//turns all bool arr to false.
for (int switchOpen = 0; switchOpen < switchOpenedCount;switchOpen++) {
 arr[switchOpen] = false;
}
}

void loop(){

for (int thisPin = 0; thisPin < pinCount; thisPin++)
{
  if(digitalRead(thisPin) == LOW)
    arr[thisPin] = true;
   
}

//Repeat this logic for every pair of switches
//Switches 1 and 3 correct
if(arr[0] == true)
{
  if (arr[2] == true)
  {digitalWrite (45, HIGH);//correct combination of switches, so turn on green led or play success tune.
   delay (1000);
   digitalWrite (45, LOW);
      }
  else
  {
    for (int thisPin = 0; thisPin < pinCount; thisPin++)
    {
      if(thisPin != 0 && thisPin != 2 && arr[thisPin] == true)
      {digitalWrite (46, HIGH);//Wrong combination of switches, turn on red led or play failure tune
       delay (1000);           
       digitalWrite (46, LOW);
        
      
      }
    }
  } 
}

//Repeat this logic for every pair of switches
//Switches 2 and 4 correct
if(arr[1] == true)
{
  if (arr[3] == true)
  {
   digitalWrite (45, HIGH);//correct combination of switches, so turn on green led or play success tune.
   delay (1000);
   digitalWrite (45, LOW);
  }
  else
  {
    for (int thisPin = 0; thisPin < pinCount; thisPin++)
    {
      if(thisPin != 1 && thisPin != 3 && arr[thisPin] == true)
      {
        digitalWrite (46, HIGH);//Wrong combination of switches, turn on red led or play failure tune
       delay (1000);           
       digitalWrite (46, LOW);
      }
    }
  } 
}

}

Here is part of the updated code for my game. It compiles ok and I have used LED’s to simulate sounds to make sure the logic works. So far so good. I have an adafruit vs-1053 to store and play the sounds. I am now trying to work out how to play /code the sounds (have downloaded the adafruit library) I need to play when the buttons are pushed and when they match or do not match. I am sure I have not done this the most efficient or logical way so any suggestions would be appreciated.

/*In a game I have 45 small wooden doors that  have 45 momentary contact switches attached. These switches are connected to pins 2 through to 55.
 * Hidden behind the doors are Simpsons Characters. 22 pairs plus one extra. The object of the game is to find matching pairs of characters for example two Mr  Burns (pin 11 and 44) or two Homers etc.
 * When a switch is pressed (door opened) I want the characters sound to play eg 'Excellent" for Mr Burns.
 * When another door is opened, again the character's sound is played but if the character matches the 1st door opened a success sound then plays. If not matching, another sound plays.
 * The switches then need to be reset back to 0 to indicate no doors opened for the next round.
*/
int switchPins[] = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55}; //Each pin has a switch to activate it.
int pinCount = 45;
bool arr[45];//variable to hold what switches were pressed
int switchOpenedCount = 45;



void setup() {
  Serial.begin(9600);
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(switchPins[thisPin], INPUT_PULLUP);
  }
  //turns all bool arr to false.
  for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++) {
    arr[switchOpen] = false;
  }
}

void loop() {
  int sensorValue = arr[1];
  Serial.println(sensorValue);

  for (int thisPin = 0; thisPin < pinCount; thisPin++)
  {
    if (digitalRead(switchPins[thisPin]) == LOW)
    { arr[thisPin] = true;
      //play individual character's sound here.


    }
  }
  //Repeat this logic for every pair of switches
  if (arr[0] == true)
  {
    if (arr[45] == true)
    { //doors match so play success sound here.
      //set doors/arr back to false
      for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++) {
        arr[switchOpen] = false;
      }
    }
    else
    {
      for (int thisPin = 0; thisPin < pinCount; thisPin++)
      {
        if (thisPin != 0 && thisPin != 45 && arr[thisPin] == true)
        { //doors do not match so play 'wrong' sound here.
          //set arr/doors back to false
          for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++) {
            arr[switchOpen] = false;


          }
        }
      }
    }
  }

  if (arr[1] == true)
  {
    if (arr[12] == true)
    {
      //Doora match so play success sound
      for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++) {
        arr[switchOpen] = false;
      }
    }
    else
    {
      for (int thisPin = 0; thisPin < pinCount; thisPin++)
      {
        if (thisPin != 1 && thisPin != 12 && arr[thisPin] == true)
        {
          //doors do not match so play 'wrong' sound.
          //set arr/doors back to false
          for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++) {
            arr[switchOpen] = false;
          }
        }
      }
    }
  }

  if (arr[2] == true)
  {
    if (arr[35] == true)
    {
      //Doora match so play success sound
      for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++) {
        arr[switchOpen] = false;
      }
    }
    else
    {
      for (int thisPin = 0; thisPin < pinCount; thisPin++)
      {
        if (thisPin != 2 && thisPin != 35 && arr[thisPin] == true)
        {
          //doors do not match so play 'wrong' sound.
          //set arr/doors back to false
          for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++) {
            arr[switchOpen] = false;
          }
        }
      }
    }
  }
  if (arr[3] == true)
  {
    if (arr[44] == true)
    {
      //Doora match so play success sound
      for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++) {
        arr[switchOpen] = false;
      }
    }
    else
    {
      for (int thisPin = 0; thisPin < pinCount; thisPin++)
      {
        if (thisPin != 3 && thisPin != 44 && arr[thisPin] == true)
        {
          //doors do not match so play 'wrong' sound.
          //set arr/doors back to false
          for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++) {
            arr[switchOpen] = false;
          }
        }
      }
    }
    if (arr[4] == true)
    {
      if (arr[17] == true)
      {
        //Doora match so play success sound
        for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++) {
          arr[switchOpen] = false;
        }
      }
      else
      {
        for (int thisPin = 0; thisPin < pinCount; thisPin++)
        {
          if (thisPin != 4 && thisPin != 17 && arr[thisPin] == true)
          {
            //doors do not match so play 'wrong' sound.
            //set arr/doors back to false
            for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++) {
              arr[switchOpen] = false;
            }
          }
        }
      }
      if (arr[5] == true)

Well done on your progress so far.

You need an array of file names, then you extract the one you want to play by using the thisPin variable as an array index. You then pass that name to the play file function.

Just curious, if it is for children, then they tend to beat the system quite often. What if they just leave a few doors open and keep opening more doors? I see it looks like one of those computer games of matching graphics or images but in a computer game, you can always force close all doors. Do you have any way to make sure a child closes the non-matching doors before opening more? Otherwise the game is kind of pointless with physical objects. Just my two cents.

Your IO can go up to 69. A0 to A15 are 54 to 69. Leave 0,1 free for Serial Monitor IO for debugging. Leave Serial1 pins free for talking to MP3 player (or whatever it uses). Why did you start at 1 here instead of 0?

void loop() {
  int sensorValue = arr[1];

Here

  if (arr[0] == true)
  {
    if (arr[45] == true)

you have the pairs to match up hard coded. You could look into using rand() during setup() to randomly create the pairing.

Thank you every one for the assistance. To answer your queries. Yes kids like to beat the system as this is human nature as I have discovered through teaching. The game is an actual physical game built out of wood. On the doors are cycling road safety signs. When a child chooses a door to open they have to describe the purpose of the sign. When a child has opened two doors if the doors match in terms of the character behind them, the doors stay open otherwise the doors are physically closed ready for the next child. (It is a huge memory game.)

void loop() { int sensorValue = arr[1];

I was having issues with the code not working as expected, so I used this to see if the switch presses were updating the arr. I started with arr[0] and worked my way through the arr. At the time the switches were not updating as the arr values were always = 0 when switches were pressed. I found out the issue I had this if (digitalRead [thisPin]) == LOW) { arr[thisPin] = true; instead of this if (digitalRead(switchPins[thisPin]) == LOW) { arr[thisPin] = true; and the arr value change to 1 when the button was pushed. I was unclear how to monitor all the arrays at the same time using the serial monitor so I did one at a time. Just so happens I left int sensorValue looking at arr[1]. I hard wired the matching pairs because the characters behind the doors would generally not move and the pairs are physically set to match the characters. (Hope this makes sense) I tried to use a photo but when I tried to upload an image it requested a url. I have the photo on my computer not on a web site.

Thank you for the help on the audio files. 'You need an array of file names, then you extract the one you want to play by using the thisPin variable as an array index. You then pass that name to the play file function.' This looks like my homework for the following weeks.

You can Reply and Attach a photo also, vs a link to a photo, example:

Thx for the info. Just did not see the attachment option at the bottom of the reply post box!!!
Here is the game thus far. The holes at the bottom of the door will hold the momentary contact switch. When the door opens it pushes the button in.

Hi, Is this how I would set an array to hold my sound file names ?

simpsonSounds myArray[8] = {excellent.mp3, homer.mp3, success.mp3, willy.mp3, wrong.mp3, bartman.mp3, krusty.mp3, itchy.mp3};

Is this how I would set an array to hold my sound file names ?

No.

What is the file type simpsonSounds ?

I would expect to use:-

char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",
"This is string 4", "This is string 5","This is string 6"};

See :-
https://www.arduino.cc/en/Reference/String

I studied the arrays on the learning reference but did not see the char arrays above it. To my understanding the arrays are actually pointers. So it should read like this?

char* simpsonSounds[]= {"excellent.mp3", "homer.mp3", "success.mp3", "willy.mp3", "wrong.mp3", "bartman.mp3", "krusty.mp3", "itchy.mp3"};

Yes that is what I said.

Thank you. Still a long way to go and a start of a new learning curve with the adafruit.

I want to play a particular sound with a particular button press. For instance if button on input pin 22 was pressed it needs to play the arpu mp3 sound. Should the arr number of pin 22 be the same arr number of the arpu sound?

char* simpsonSounds [] = {"excellent.mp3", "failure.mp3","nelson.mp3","arpu.mp3","bartman.mp3","bart.mp3"};

void loop() {
  

  for (int thisPin = 0; thisPin < pinCount; thisPin++)
  {
    if (digitalRead(switchPins[thisPin]) == LOW)
    { arr[thisPin] = true;
//Is this where I would need to call the playFile function? And the function itself needs to be outside the curly brackets of the loop?
[code]

Should the arr number of pin 22 be the same arr number of the arpu sound?

Not sure I understand that but my guess is no.

You have an array of names, you should also have an array with the input pins you want to use.

Then you look at each pin in turn and when you find an array index that is pressed, use that same index in the name array to trigger the playing.

char* simpsonSounds [] = {"excellent.mp3", "failure.mp3","nelson.mp3","arpu.mp3","bartman.mp3","bart.mp3"};
const byte inputPins [] = { 5,6, 10, 22, 14, 12 } ; // the only one you told me had arpu on pin 22

void loop() {
  

  for (int i = 0; i < pinCount; i++)
  {
    if (digitalRead(inputPins[i]) == LOW)
    { 
//Is this where I would need to call the playFile function? And the function itself needs to be outside the curly brackets of the loop?
// yes it is using simpsonSounds [i] as the file name
}
[code]