0

MusicMaker shield with touch sensor coding question
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: MusicMaker shield with touch sensor coding question

by adafruit_support_bill on Thu Sep 05, 2019 12:21 pm

The first number in the output is the reading from the A0 pin. The second number is the value of 'playingMusic '. According to the output, the music is playing (the value is 1) when you lift your finger and it calls pause.

Are you sure that the track001.mp3 is good? What happens if you play the full file?

adafruit_support_bill
 
Posts: 74334
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

by moonmansays on Thu Sep 05, 2019 12:41 pm

If I change startPlayingFile to playFullFile, the entire file plays. To be clear about the serial monitor numbers relative to my actions:

When I press the touch sensor, this is printed in the serial monitor:

670
0
Playing track 001

and when I release the touch sensor, this is printed in the serial monitor:

14
1
Paused

The first numbers of these groups are slightly different each time but the second numbers are consistent each time -- and 0 is always displayed with the playing of the file upon my touching the sensor, and 1 is always displayed after I release the sensor. It has never displayed a '1' upon touching the sensor.

moonmansays
 
Posts: 12
Joined: Wed Sep 04, 2019 12:23 pm

Re: MusicMaker shield with touch sensor coding question

by adafruit_support_bill on Thu Sep 05, 2019 1:44 pm

and 0 is always displayed with the playing of the file upon my touching the sensor, and 1 is always displayed after I release the sensor. It has never displayed a '1' upon touching the sensor.

Exactly as expected. Follow the logic of the loop:

670 - you are pressing the button
0 - the track is not playing yet
Playing track 001 - now you start playing the track. You will stay in the 'while' loop until you lift your finger.
14 - you are no longer pressing the button
1 - but the track is still playing
Paused - until you tell it to pause - You will stay in the 'while' loop until youpress teh button again.

Looking at the rest of your code, the problem seems to be that you are not enabling the interrupts. When using the asynchronous 'startPlayingFile' function, the library needs to use periodic interrupts to keep the music player supplied with fresh mp3 data to play.

See the example here:
https://github.com/adafruit/Adafruit_VS ... rrupts.ino

adafruit_support_bill
 
Posts: 74334
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

by moonmansays on Thu Sep 05, 2019 2:12 pm

Amazing! Yes, that's what was needed. It now works as I had hoped! Thank you so much for continuing to help me troubleshoot and for the lessons in code logic and reading the serial monitor. I now understand better how it reads the code and where code needs to be placed. I'm still not sure I understand why something called an interrupt is needed but I am happy to have working code. Next is figuring out how to provide an audio tail once the sensor is released so that the musical note doesn't end abruptly. Do you know if the MusicMaker is capable of jumping to the tail end of the sound file it is playing and playing just the end of the file when the sensor is released? Or alternatively, if once the sensor is released, the sound fades out instead of cutting off abruptly? Thank you very much Bill for helping me solve this challenge. I now have a working device! - Chris

moonmansays
 
Posts: 12
Joined: Wed Sep 04, 2019 12:23 pm

Re: MusicMaker shield with touch sensor coding question

by adafruit_support_bill on Thu Sep 05, 2019 2:40 pm

The interrupt is needed because the processor can only do one thing at a time. While it is sitting there in the 'while' loop, it is not feeding data to the sound chip. If you enable the interrupts, it will periodically interrupt your while loop and call a function in the library that will feed another buffer's worth of data before returning to your loop.

To make it jump to the end is probably possible, but not trivial. Fading out would probably be easier. There is a setvolume function in the library. You can call that in a loop to fade out the sound.

adafruit_support_bill
 
Posts: 74334
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

by moonmansays on Thu Sep 05, 2019 2:57 pm

Thanks for the explanation and the directions for finding the setvolume fade function. I will find and implement it. But here's something... I've been playing with this now and it works great but there is a little glitch. Once the program is uploaded, the file plays cleanly the first time I press and release the sensor. But all subsequent times I press the sensor, the file begins with a sonic glitch. It appears to be some leftover sonic material from the end of the last time it was played. Is there any way to reset each time so this doesn't happen? Or some other solution? Thank you again for all your help.

Update: I got the volume fade function to work. It sounds great. But the starting glitch is still there.
Last edited by moonmansays on Thu Sep 05, 2019 3:19 pm, edited 1 time in total.

moonmansays
 
Posts: 12
Joined: Wed Sep 04, 2019 12:23 pm

Re: MusicMaker shield with touch sensor coding question

by adafruit_support_bill on Thu Sep 05, 2019 3:18 pm

Not sure if it will help, but you can try calling musicPlayer.begin() again to see if that resets things sufficiently.

adafruit_support_bill
 
Posts: 74334
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

by moonmansays on Thu Sep 05, 2019 3:28 pm

It worked! Thanks. I put the line at the end of the final commands. I'm attaching my finalized code. Does it all look clean to you?
Code: Select all | TOGGLE FULL SIZE
/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// define the pins used
//#define CLK 13       // SPI Clock, shared with SD card
//#define MISO 12      // Input data, from VS1053/SD card
//#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins.
// See http://arduino.cc/en/Reference/SPI "Connections"

// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// 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)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer =
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
 
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  Serial.println("Adafruit VS1053 Simple Test");

  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"));
 
   if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }

 
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);
  if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
    Serial.println(F("DREQ pin is not an interrupt pin"));
}


// the loop routine runs over and over again forever:
        void loop()  {
       // read the input on analog pin 0:
       int sensorValue = analogRead(A0);
       Serial.println(sensorValue);
       Serial.println(musicPlayer.playingMusic);
       
       int threshold = 200;
       int v = 0; //volume

       // Only start playing if not already playing
       if(sensorValue > threshold)
       {
        //volume fade in
      for (v = 140; v > 19; v--){
        musicPlayer.setVolume(v,v);
        delay (0);}
          Serial.println(F("Playing Sound"));
          musicPlayer.startPlayingFile("BFSSUSC3.mp3");
          while(analogRead(A0) > threshold)
          {
             // do nothing
          }
       }
       
       else // stop playing
       
       {
        //volume fade out
      for (v = 20; v < 140; v++){
         musicPlayer.setVolume(v,v);
         delay (10);}
          Serial.println(F("Paused"));
          musicPlayer.pausePlaying("BFSSUSC3.mp3");
          while(analogRead(A0) <= threshold)
          musicPlayer.begin();
          {
             // do nothing
          }
       }
    } 

moonmansays
 
Posts: 12
Joined: Wed Sep 04, 2019 12:23 pm

Re: MusicMaker shield with touch sensor coding question

by adafruit_support_bill on Thu Sep 05, 2019 3:59 pm

Looks good! Thanks for the follow-up.

adafruit_support_bill
 
Posts: 74334
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.