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 »

Ok thanks. I have taken your direction and applied it to the code. I'm also now using the Digital Pins instead of the GPIOs. After the sketch is uploaded, the touch sensor triggers the Pin 3 mp3 file consistently. But when I press either button 2 or button 3 nothing happens. The file doesn't change according to the buttons pressed. Only when I press one of the buttons while the sound file has not yet faded out will it change to the sound file associated with pin 2. To be sure it is not my wiring, I have tested my breadboard wiring set up with a "serial digital read-out only" program and seen it behaves as expected when each button is pressed. Can you see anything further that is messing with the logic? Thanks again for all of your help. Code attached.

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

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin2 = 2;     // the number of the pushbutton pin
// variables will change:
int buttonState2 = 02;         // variable for reading the pushbutton status
const int buttonPin3 = 3;     // the number of the pushbutton pin
// variables will change:
int buttonState3 = 03;         // variable for reading the pushbutton status

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");
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);

  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 state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);
  // Show the state of pushbutton on serial monitor
  Serial.println(buttonState2);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState2 == 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
      }
    }
  }
  { // read the state of the pushbutton value:
    buttonState3 = digitalRead(buttonPin3);
    // Show the state of pushbutton on serial monitor
    Serial.println(buttonState3);
    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState3 == 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: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: MusicMaker shield with touch sensor coding question

Post by adafruit_support_bill »

the touch sensor triggers the Pin 3 mp3 file consistently. But when I press either button 2 or button 3 nothing happens.
That seems consistent with the way you have it coded:

In your setup, you have pins 2 and 3 pulled high:

Code: Select all

  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
So, in your loop, this will evaluate to TRUE by default:

Code: Select all

  if (buttonState2 == HIGH)
  {
So you can continue on and check the touch sensor here;

Code: Select all

  if (buttonState2 == 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)
    {
When you press the button on pin 2, it gets pulled LOW, so you don't look at the touch sensor anymore.

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

Return to “Arduino Shields from Adafruit”