adafruit-vs1053

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit-vs1053

Post by roadshark »

Hi, Made some progress. When I activate pin 15 & pin 26 I heard the homer sound play twice on each button push.. I used your debounce code and solved the problem. I removed all my logic and arguments to get back to 1 button push just plays the appropriate sound. (Yes the arrays are updating with the pin count)I tested each pin from 14 through to 49, 53 and A8 through to A15. The following pins did not play a sound, ( 21, 22, 30, 38, 39, 49, 53, and A11). The mega has female header pins already attached and I am using jumper leads for the testing. I thought maybe it was the contact between jumper pin and header that was the problem. I flipped the board and touched the jumper pin directly to the solder of the pin. Still no sound on the button push. Are these pins used by other functions or are they dodgy pins on the mega?

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit-vs1053

Post by roadshark »

Here is the your code with the debounce added. Compiles ok but when uploaded nothing in the serial monitor and no sounds. Also if the sounds match for example button pin 15 is activated a homer sound is played then pin 26 is activated a homer sound is played then excell00 needs to play. If pin 15 is activated a homer sound plays then if any pin other than 26 is activated, say pin 14 itchy000 sound then a failure0 sound needs to play.
I understand that this is beyond what you need to help with as the music player is working fine so I do really appreciate the time you have put into assisting me with the code. The game I am designing is a road safety game for children as I am a bike ed teacher. On the front of the doors are cycling safety road signs.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: adafruit-vs1053

Post by adafruit_support_rick »

My code is stand-alone. No integration required, other than creating the correct door pairs. I just put in a few place-holders.

I did some simple tests, and it worked for me. If it doesn't work for you, it's close to working. You should be able to get it going. It will save you a lot of headaches over trying to write separate code for each button pair.

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit-vs1053

Post by roadshark »

Hi Rick,
Found out the problem with the pins...Programmer problem not board problem as I had missing music files!! Yes it is mind boggling to write the code for each pair of pins. I would have to come up with an argument for every pairs variables eg
0 and 44 play success, 0 and 1 play failure, 0 and 2 play failure, o and 3 play failure all the way up to 43. I would need to repeat this for all the 22 pairs of pins!!!! So I need to work out how to do this a simpler way.
I have been studying your code. I have some questions. I do not see where if the two pins match say 0 and 44 a success sound plays? Each button has its own unique sound which plays. When the two sounds match a success sound sound then plays. I can see the reference to the failure sound.
Also in this piece of code does this [2] tell that each pair has two numbers? If I add more door pairs, 22 to be exact, do I have to change anything here other than listing all the correct pairs.

int doorPairs [][2] = {{0, 44}, {1, 12}, {3, 43}};
#define NUM_PAIRS (sizeof (doorPairs)/sizeof (int))/2

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: adafruit-vs1053

Post by adafruit_support_rick »

Ah - maybe I didn't understand the way this is supposed to work. So, when you open a door, you get a simpsonsSound. When you open the second door, you get either the success or failure sound? Do you also get the simpsonsSound?

When do you get he Homer sound?

Can you walk me through a typical scenario, with what sounds are generated at each step?

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit-vs1053

Post by roadshark »

Hi Rick.
I have attached photo's of the game board the children will use. It has 45 small doors and on each door there is a type of cycling sign a child may see when riding his/her bike in Melbourne Australia. Behind the doors I have 22 pairs of Simpson Characters and 1 extra = 45 doors. The idea of the game is children try and match the pairs of characters. It is a memory game. They choose two doors by having to name the sign on the door. When they open a door a Simpson Character is revealed and as the door opens it hits a switch that plays the characters individual sound. For example if a child opens the door and Bart Simpson is behind it, the sound will say "Eat my Shorts!"
Senario.. A child randomly chooses the door with a stop sign. It is opened to reveal Homer Simpson, then a sound plays "homer000.mp3", " WhoooHooo!" The child then has one more chance to find the other door hiding Homer Simpson. Lets say the kid opens the door with the Give Way Sign and it does reveal Homer Simpson then the sound is played, "homer000,mp3","WhoooHooo!" But because it is a match with the first door, "excell00.mp3", "excellent!" is also played immediately after the homer sound. If however the second door open reveals Bart Simpson then Bart's sound, "bart0000.mp3" "Eat my shorts!" is played and then because the characters (doors) do not match the "failure0.mp3" " Doh!" sound is played. Hope this makes sense. The first part of, if it is a matching pair that is easy.
It is the other stuff when the two arrays do not match that is difficult.
Door hiding homer is opened. Which say is pin 15 which updates array [0]

Code: Select all

 {
    if (debounceRead(switchPins[thisPin]) == LOW)
    { arr[thisPin] = true;
      musicPlayer.playFullFile(simpsonSounds[thisPin]);
 }
so Homer sound is played. As no other door is opened the program keeps looping.
The other door hiding homer is opened, Pin 26 which updates array [12]

Code: Select all

 {
    if (debounceRead(switchPins[thisPin]) == LOW)
    { arr[thisPin] = true;
      musicPlayer.playFullFile(simpsonSounds[thisPin]);
}
so homer sound is played again but now because two arrays are now true

Code: Select all

if (arr[0] == true && arr [12] == true)
//there is a door match so excellent sound needs to be played.
  { musicPlayer.playFullFile("excell00.mp3");
    //set doors/arr back to false ready for next door opening sequence.
    for (int switchOpen = 0; switchOpen < switchOpenedCount; switchOpen++)
      arr[switchOpen] = false;
  }
Once there is a character match the doors remain open (as these characters can not be chosen again) but do not activate the switches.
Thanks again for your fast responses, time commitment and willingness to help to make cycling safer for children.
Attachments
2nd door opened Krusty sound played then failure sound played as it does not match 1st character. Milhouse.
2nd door opened Krusty sound played then failure sound played as it does not match 1st character. Milhouse.
IMG_0695.JPG (168.29 KiB) Viewed 973 times
Ist door opened character plays individula sound for Milhouse.
Ist door opened character plays individula sound for Milhouse.
IMG_0694.JPG (183.33 KiB) Viewed 973 times
The game have got the speakers in place. Could not find grills though.
The game have got the speakers in place. Could not find grills though.
IMG_0692.JPG (330.48 KiB) Viewed 973 times

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: adafruit-vs1053

Post by adafruit_support_rick »

roadshark wrote:Once there is a character match the doors remain open (as these characters can not be chosen again) but do not activate the switches.
How does this work?
You do this in hardware? That is, the switch only makes contact while the door is being opened, then doesn't make contact anymore? If you're playing a sound, you'll miss the switch activation. If the switch does not remain closed long enough for loop() to come around to it, you may also miss the event.

If the switch stays closed while the door is open, you have to handle the exclusion in software.

The code will be very different depending on how you resolve this condition. Right now, I'm having a hard time thinking of how to do it properly either way. I as assuming earlier that you close the doors after each match, so that there are only two doors open at a time. That's an easier case to solve.

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit-vs1053

Post by roadshark »

Hi there,
Yep its the hardware. If you look at the photos you will see below each door a small hole. This is where the push button switch is. I have the ability to adjust the switch further from or closer to the door to make sure the switch is able to close.. The door opens in a downwards fashion. The bottom of the door, when fully opened hits the switch by the force of your hand opening. You press the door into the switch so it makes contact and you hear a click.. You then release the door. There is enough force in the switch spring to force the door open slightly and open the switch. So the door action is open, press, release. The door then remains opened until another door is opened then both doors are closed if they do not match. If the doors do match, the doors remain opened but because of the springs in the switches, they keep the doors from making contact. So opening the door fully is like pressing a switch. You have to press it long enough for the code to pick it up.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: adafruit-vs1053

Post by adafruit_support_rick »

OK. Try this. I tested it as well as I can. I think it will do what you want:

Code: Select all

/***************************************************
  This is an example for the Adafruit VS1053 Codec Breakout

  Designed specifically to work with the Adafruit VS1053 Codec Breakout
  ----> https://www.adafruit.com/products/1381

  Written by Limor Fried/Ladyada for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
#define CARDCS 4     // Card chip select pin
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer =
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
//Each pin has a switch to activate it. switchPin 15 (arr 1) makes a pair with switch 26 (arr 12). When each is pressed music player plays "homer000.mp3" and
//if both pressed music player than plays "excell00.mp3.
//If the switches do not match musicPlayer needs to play "failure01.mp3".
int switchPins[] = {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,53, A8, A9, A10, A11, A12, A13, A14, A15};
#define pinCount (sizeof(switchPins)/sizeof(int))
bool arr[pinCount];  //variable to hold what switches were pressed

char* simpsonSounds [] = {"itchy000.mp3", "homer000.mp3", "willy000.mp3", "arpu0000.mp3", "itchy000.mp3", "mrburns0.mp3", "bart0000.mp3", "wigam000.mp3", "wigam000.mp3", "flanders.mp3", "lisa0000.mp3", "marg0000.mp3", "homer000.mp3", "marg0000.mp3", "lisa0000.mp3", "krusty00.mp3", "snowball.mp3", "itchy000.mp3", "maggie00.mp3", "nelson00.mp3", "milhouse.mp3", "milhouse.mp3", "nelson00.mp3",
                          "grandpa0.mp3", "snowball.mp3", "santa000.mp3", "bart0000.mp3", "maggie00.mp3", "milhouse.mp3", "krusty00.mp3", "grandpa0.mp3", "bartman0.mp3", "bartman0.mp3", "flanders.mp3", "santa000.mp3", "willy000.mp3", "fink0000.mp3", "mrburns0.mp3", "ralph000.mp3", "ralph000.mp3", "fink0000.mp3", "hans0000.mp3", "hans0000.mp3", "arpu0000.mp3", "itchy000.mp3",
                         };

int doorPairs[][2] = {{0, 11}, {1, 44}, {2, 5}};
#define NUM_PAIRS (sizeof(doorPairs)/sizeof(int))/2


//There are two other files on the card   excell00.mp3 and failure0.mp3 not part of the above array.
void setup() {
  Serial.begin(9600);
  
  SD.begin(CARDCS);    // initialise the SD card

   if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  Serial.println(F("VS1053 found"));

  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20, 20);

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(switchPins[thisPin], INPUT_PULLUP);
  }

}

int doorCount = 0;  //number of open doors
int door1 = -1;
int door2 = -1;

void loop() {
  

  for (int thisPin = 0; thisPin < pinCount; thisPin++)
  {
    if (debounceRead(switchPins[thisPin]) == LOW)
    { 
      if (door1 == -1)
      {
        door1 = thisPin;
        doorCount++;
        musicPlayer.playFullFile(simpsonSounds[thisPin]);
        Serial.print("door1 = "); Serial.println(thisPin);
      }
      else
      {
        if (thisPin != door1)
        {
          door2 = thisPin;
          doorCount++;
          musicPlayer.playFullFile(simpsonSounds[thisPin]);
          Serial.print("door2 = "); Serial.println(thisPin);
        }
      }
    }
  }

  if (doorCount == 2)
  {
    bool success = false;  //assume the worst
    
    for (int pair = 0; pair < NUM_PAIRS; pair++)
    {
      //check pairing
      if (((door1 == doorPairs[pair][0]) && (door2 == doorPairs[pair][1]))
        || ((door2 == doorPairs[pair][0]) && (door1 == doorPairs[pair][1])))
      {
        success = true;  //valid pair found
      }
    }
    
    if (success)
    {
      musicPlayer.playFullFile("excell00.mp3");
      Serial.print("paired! ( "); Serial.print(door1); Serial.print(", "); Serial.print(door2); Serial.println(")");
    }
    else
    {
      musicPlayer.playFullFile("failure01.mp3");
      Serial.print("failed! ( "); Serial.print(door1); Serial.print(", "); Serial.print(door2); Serial.println(")");
    }
    door1 = -1;
    door2 = -1;
    doorCount = 0;
  }
}

uint8_t debounceRead(int pin)
{
  uint8_t pinState = digitalRead(pin);
  uint32_t timeout = millis();
  while ((millis() - timeout) < 10)
  {
    if (digitalRead(pin) != pinState)
    {
      pinState = digitalRead(pin);
      timeout = millis();
    }
  }

  return pinState;
}

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit-vs1053

Post by roadshark »

Hi Rick,
WhoooHOoooo! It works perfectly so far. I am truly thankful for your patience and expertise and especially taking the time to help me out. You are a credit to your organisation! Wishing you a very happy and prosperous 2016. I look forward to purchasing more adafruit electronics for my game projects. I will let you know how it goes when all the switches are connected and checked. Again many thanks on a job well done! If you ever come to Melbourne Australia let me know.
Cheers!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: adafruit-vs1053

Post by adafruit_support_rick »

Awesome! I knew we'd get it right!

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit-vs1053

Post by roadshark »

It's called synergy! Thx heaps.

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit-vs1053

Post by roadshark »

Hi Rick,
Just to let you know, the game works fine with all the logic and arguments you created working as expected. All 22 pairs play the appropriate sounds in the correct sequence. Wish you a safe and the adafruit team a prosperous and safe 2016.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: adafruit-vs1053

Post by adafruit_support_rick »

Thanks for the update!

Any chance of a video?

User avatar
roadshark
 
Posts: 652
Joined: Sun Nov 29, 2015 5:53 am

Re: adafruit-vs1053

Post by roadshark »

will try and arrange. Kids on school holidays here for 3 more weeks!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”