MusicMaker shield with touch sensor coding question

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
moonmansays
 
Posts: 34
Joined: Wed Sep 04, 2019 12:23 pm

Re: MusicMaker shield with touch sensor coding question

Post by moonmansays »

Thank you for that very helpful information. If I used Adafruit's Toggle version: Standalone Toggle Capacitive Touch Sensor Breakout - AT42QT1012, would the maximum ON period be longer than a minute. In other words, once switched ON, would it continue to play a sound file of any length until switched OFF by the user?

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

Post by adafruit_support_bill »

Yes. The toggle version defaults to an infinite timeout:
https://learn.adafruit.com/adafruit-cap ... ep-1035722

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

Re: MusicMaker shield with touch sensor coding question

Post by moonmansays »

Thanks for all this info. Very helpful!

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

Re: MusicMaker shield with touch sensor coding question

Post by moonmansays »

Hello! Thank you for your earlier code assistance. I am continuing to build up my project where a touch sensor is used to trigger an mp3 file in the Music Maker shield. Currently, if I want to access a different mp3 file, I have to upload a new sketch to the arduino. I would like to use a rotary encoder to accomplish switching to different stores mp3 files on the shield. I am going to use the rotary encoder on your site. Can you please help me with the new code for this and how to place it within my existing code? My current code should be embedded in this thread from my last post in September. Thank you! - Chris

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

Post by adafruit_support_bill »

I would suggest starting with Paul Stoffregan's rotary encoder library.
https://github.com/PaulStoffregen/Encoder

Start by wiring up and testing your encoder using the example code from the library. Once you have that working correctly, you can start integrating it into your Music Maker code.

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

Re: MusicMaker shield with touch sensor coding question

Post by moonmansays »

Thank you for that direction. But I am now thinking that using push buttons (these: https://www.adafruit.com/product/1119) might be the best method for my purposes in getting to NEXT and PREVIOUS tracks in the Musicmaker MP3 shield. As I understand it, I would use two buttons and assign each to an Analog In (say A2 and A3) on the shield. Would they need to be powered in any other way? My 5 volt is being used by a touch sensor. I have the 3v and one more ground available.

Also, can you direct me to some coding that would accomplish 'selecting' NEXT or PREVIOUS tracks stored on the SD card of the Music Maker MP3 shield? I want to 'select' the track only and still only produce sound when the touch sensor is activated. I am reprinting my current working code below for reference. Thank you.

Code: Select all

        /*
          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(001,001);
          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 1:
               int sensorValue = analogRead(A1);
               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 > 4; v--){
                musicPlayer.setVolume(v,v);
                delay (0);}
                  Serial.println(F("Playing Sound"));
                  musicPlayer.startPlayingFile("ARDUINO4.mp3");
                  while(analogRead(A1) > threshold)
                  {
                     // do nothing
                  }
               }
               
               else // stop playing
               
               {
                //volume fade out
              for (v = 4; v < 140; v++){
                 musicPlayer.setVolume(v,v);
                 delay (40);}
                  Serial.println(F("Paused"));
                  musicPlayer.pausePlaying("ARDUINO4.mp3");
                  while(analogRead(A1) <= threshold)
                  musicPlayer.begin();
                  {
                     // do nothing
                  }
               }
            }  

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

Post by adafruit_support_bill »

As I understand it, I would use two buttons and assign each to an Analog In (say A2 and A3) on the shield. Would they need to be powered in any other way?
There is no need to use an analog pin. The simplest way to read a button is using digitalRead. See lesson 6 for details on how to connect and read digital inputs.
https://learn.adafruit.com/adafruit-ard ... tal-inputs
Also, can you direct me to some coding that would accomplish 'selecting' NEXT or PREVIOUS tracks stored on the SD card of the Music Maker MP3 shield?
I don't know of any specific examples that do that. The best way to start is by making a list of all the tracks that you want to play.

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

Re: MusicMaker shield with touch sensor coding question

Post by moonmansays »

Thank you. Lesson 6 was helpful. My buttons are reading on the serial monitor. Still I am not sure how to get a button to 'load' an mp3 from my SD card onboard the Music Maker shield to then be played by the touch sensor. As I mentioned, my touch sensor programming works great when I upload the sketch specifying one particular sound file, but I'd like to be able to direct the shield to 'load another track' without having to re-upload a different sketch specifying that new track. Someone recommended a rotary encoder and then I saw a simple button might do it, but I'm having trouble finding the right commands to accomplish this. I thought a rotary encoder could just cycle through my mp3 list notch by notch. Or a button could be programmed to advance to the next track down the list. I'm even open to dedicating one button for each track.

Since the Music Maker is a dedicated mp3 player, I am imagining switching from one track to another is a very common use of it. But no matter how I search for code that already exists for this on the internet, I can't find the solution.

Maybe I'm thinking about it all wrong and there is an easier way to do this. I only want to be able to access my mp3 files on the SD card and 'load' them into some kind of 'current song in play' status so that my touch sensor programming will work with them. Any advice would be greatly appreciated. Thank you!

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

Post by adafruit_support_bill »

You need to start by making a list of all the tracks you want to play. Once you have that, you can start writing code to select from the list.

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

Re: MusicMaker shield with touch sensor coding question

Post by moonmansays »

Thank you for your continued help, Bill. I have constructed a code that I thought should work combining code from Lesson Six with my working code. Once I set up button definitions and pinmode in Void Setup, I created if statements for the buttons and nested the code that has proven to work inside those conditions. Nested each time individually for each button. I plan to use all Digital pins eventually but am now trying to get this to work with two different buttons, one for each of two mp3s on my SD card. The code compiles and uploads fine but doesn't react the way I expect it to. Which is... Code loads, then a button is selected, then the program works as it has worked heretofore. But accesses a different sound file for each button. Instead, the touch sensor reacts (lights up) but nothing sounds. I have included the code in question. Thanks again for your help! - Chris

Code: Select all

        /*
          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

        int Button01pin = 1;
        int Button02pin = 2;

        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(001,001);
          if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
            Serial.println(F("DREQ pin is not an interrupt pin"));
        {
        
          pinMode(Button01pin, INPUT_PULLUP);
          pinMode(Button02pin, INPUT_PULLUP);
        }
        }

        // the loop routine runs over and over again forever:
                void loop()  {
                  if (digitalRead(Button01pin) == LOW)
                  {
                 
               // 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 > 4; v--){
                musicPlayer.setVolume(v,v);
                delay (0);}
                  Serial.println(F("Playing Sound"));
                  musicPlayer.startPlayingFile("12ATMOS1.mp3");
                  while(analogRead(A0) > threshold)
                  {
                     // do nothing
                  }
               }
               
               else // stop playing
               
               {
                //volume fade out
              for (v = 4; v < 140; v++){
                 musicPlayer.setVolume(v,v);
                 delay (10);}
                  Serial.println(F("Paused"));
                  musicPlayer.pausePlaying("12ATMOS1.mp3");
                  while(analogRead(A0) <= threshold)
                  musicPlayer.begin();
                  {
                     // do nothing
                  }
               }
            } 
                
                  if (digitalRead(Button02pin) == LOW)
                  {
             
               // 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 > 4; v--){
                musicPlayer.setVolume(v,v);
                delay (0);}
                  Serial.println(F("Playing Sound"));
                  musicPlayer.startPlayingFile("12HLOG#4.mp3");
                  while(analogRead(A0) > threshold)
                  {
                     // do nothing
                  }
               }
               
               else // stop playing
               
               {
                //volume fade out
              for (v = 4; v < 140; v++){
                 musicPlayer.setVolume(v,v);
                 delay (10);}
                  Serial.println(F("Paused"));
                  musicPlayer.pausePlaying("12HLOG#4.mp3");
                  while(analogRead(A0) <= threshold)
                  musicPlayer.begin();
                  {
                     // do nothing
                  }
               }
            } 
                } 

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

Post by adafruit_support_bill »

You are not reading the analog pin unless the button on pin 01 is pressed.

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

Re: MusicMaker shield with touch sensor coding question

Post by moonmansays »

I have something else here. A little different - using a button program I found on this forum. But it appears what I need after the first command in the void loop section, is a 'don't play but wait until further instructions from A0 (touch sensor). Can you ascertain what I'm missing? Thank you!

Code: Select all

// 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"
        #define DEBOUNCE 100 // button debouncer
        // 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(001,001);
          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()  
                {
                if (musicPlayer.GPIO_digitalRead(2) == HIGH)
             
              { 
               // 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 > 4; v--){
                musicPlayer.setVolume(v,v);
                delay (0);}
                  Serial.println(F("Playing Sound"));
                  musicPlayer.startPlayingFile("12HLOG#4.mp3");
                  while(analogRead(A0) > threshold)
                  {
                     // do nothing
                  }
               }
               
               else // stop playing
               
               {
                //volume fade out
              for (v = 4; v < 140; v++){
                 musicPlayer.setVolume(v,v);
                 delay (10);}
                  Serial.println(F("Paused"));
                  musicPlayer.pausePlaying("12HLOG#4.mp3");
                  while(analogRead(A0) <= threshold)
                  musicPlayer.begin();
                  {
                     // do nothing
                  }
               }
            } 
{            if (musicPlayer.GPIO_digitalRead(3) == HIGH)
              {
             // 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 > 4; v--){
                musicPlayer.setVolume(v,v);
                delay (0);}
                  Serial.println(F("Playing Sound"));
                  musicPlayer.startPlayingFile("12ATMOS1.mp3");
                  while(analogRead(A0) > threshold)
                  {
                     // do nothing
                  }
               }
               
               else // stop playing
               
               {
                //volume fade out
              for (v = 4; v < 140; v++){
                 musicPlayer.setVolume(v,v);
                 delay (10);}
                  Serial.println(F("Paused"));
                  musicPlayer.pausePlaying("12ATMOS1.mp3");
                  while(analogRead(A0) <= threshold)
                  musicPlayer.begin();
                  {
                     // do nothing
                  }
               }
              }            } }

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

Post by adafruit_support_bill »

The code is very difficult to follow due to the inconsistent indenting. https://www.baldengineer.com/one-click- ... -code.html

But from what I can see, the logic looks similar to before - except that you don't read A0 unless GPIO #2 on the music player is HIGH.

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

Re: MusicMaker shield with touch sensor coding question

Post by moonmansays »

Thanks for the formatting tip, Bill. I've applied the autoformat and repost code here.

You say that you don't read A0 unless GPIO #2 is HIGH, but GPIO #2 is HIGH in my code. So why doesn't it read A0?

I've also heard that the GPIO's automatically pull up high. Is it in error then for me to specify "== HIGH" in relation to GPIOs?

Maybe a better question is, would I have better luck just using the digital pins and a ground? Thank you!

Code: Select all

// 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"
#define DEBOUNCE 100 // button debouncer
// 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(001, 001);
  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()
{
  if (musicPlayer.GPIO_digitalRead(2) == HIGH)

  {
    // 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 > 4; v--) {
        musicPlayer.setVolume(v, v);
        delay (0);
      }
      Serial.println(F("Playing Sound"));
      musicPlayer.startPlayingFile("12HLOG#4.mp3");
      while (analogRead(A0) > threshold)
      {
        // do nothing
      }
    }

    else // stop playing

    {
      //volume fade out
      for (v = 4; v < 140; v++) {
        musicPlayer.setVolume(v, v);
        delay (10);
      }
      Serial.println(F("Paused"));
      musicPlayer.pausePlaying("12HLOG#4.mp3");
      while (analogRead(A0) <= threshold)
        musicPlayer.begin();
      {
        // do nothing
      }
    }
  }
  { if (musicPlayer.GPIO_digitalRead(3) == HIGH)
    {
      // 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 > 4; v--) {
          musicPlayer.setVolume(v, v);
          delay (0);
        }
        Serial.println(F("Playing Sound"));
        musicPlayer.startPlayingFile("12ATMOS1.mp3");
        while (analogRead(A0) > threshold)
        {
          // do nothing
        }
      }

      else // stop playing

      {
        //volume fade out
        for (v = 4; v < 140; v++) {
          musicPlayer.setVolume(v, v);
          delay (10);
        }
        Serial.println(F("Paused"));
        musicPlayer.pausePlaying("12ATMOS1.mp3");
        while (analogRead(A0) <= threshold)
          musicPlayer.begin();
        {
          // do nothing
        }
      }
    }
  }
}

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

Post by adafruit_support_bill »

You say that you don't read A0 unless GPIO #2 is HIGH, but GPIO #2 is HIGH in my code. So why doesn't it read A0?
There is nowhere in your code that you set any music player GPIO pin to any value. The only place you reference GPIO 2 is where you read it here in line 62:

Code: Select all

  if (musicPlayer.GPIO_digitalRead(2) == HIGH)

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

Return to “Arduino Shields from Adafruit”