0

I am a performing musician and quite new to the Arduino community. Just learning as I go. I am interested in using a momentary touch sensor with Adafruit's MP3 shield or Wave Shield. I have successfully connected a touch sensor to the MusicMaker MP3 shield (which is sitting on the Adafruit Metro). I combined some code from two independently working code sequences: one, to play MP3s and, two, to trigger with the sensor. This works successfully except that the music starts but does not stop when I lift my finger off the sensor. I want the music to stop when the touch sensor is not being touched. When the sensor is touched again, I want the music file to start again from the beginning. I'm printing the code IDE is running below and attaching an image of the touch sensor I am using. I appreciate any advice on the code! Thank you.

/*
  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);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  if(sensorValue > 100) {
    Serial.println(F("Playing track 001"));
  musicPlayer.playFullFile("/track001.mp3");
  }
 else(sensorValue < 100); {
    Serial.println(F("Paused"));
  musicPlayer.pausePlaying("/track001.mp3");
 }      }
  • you do it the same way as when you detect the keypress ... when the value of the pin indicates that the button has been released, stop the music – jsotola Sep 4 at 6:00
  • I don't know the mp3 lib and its methods, but playFullFile() sounds like it may return after the entire file has been played. In that case the button wouldn't be polled while the file is being played – Sim Son Sep 4 at 14:48
1

You have a semicolon that doesn't belong in your code. Your current code (With the indentation cleaned up a little)

void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  if(sensorValue > 100) {
    Serial.println(F("Playing track 001"));
    musicPlayer.playFullFile("/track001.mp3");
  }
  else(sensorValue < 100); { // <<<--semicolon does not belong here.
    Serial.println(F("Paused"));
    musicPlayer.pausePlaying("/track001.mp3");
  }
}

Should be:

void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  if(sensorValue > 100) {
    Serial.println(F("Playing track 001"));
    musicPlayer.playFullFile("/track001.mp3");
  }
  else(sensorValue < 100) { // <<<--semicolon removed from this line
    Serial.println(F("Paused"));
    musicPlayer.pausePlaying("/track001.mp3");
  }
}

With the semicolon after the else, your code reads "...else do nothing" and then I would expect it to execute the pause logic no matter what value is in sensorValue.

You should pick an indentation style and then be absolutely consistent with it. Most C/C++ programmers use K&R style, which is what I used in my edit. (Opening braces are on the same line as the if statement/function that begins the scope, code inside braces is indented, and closing brace is at the same indentation level as the line that contains the opening brace.)

0

As Sim Son have pointed it out, you need to use the proper function. According to your library's documentation:

boolean playFullFile(char *trackname) - Play the complete file. This function will not return until the playback is complete.

You want to check your touch sensor, so this won't be enough for you.

boolean startPlayingFile(char *trackname) - Begin playing the specified file from the SD card using interrupt-driven playback. This allows your program to perform other tasks as the file is playing.

That documentation missed it, but in the source there is a function that stops your music:

void stopPlaying(void);

You can find this within your Arduino folder: Arduino/libraries/Adafruit_VS1053_Library's Adafruit_VS1053.h and Adafruit_VS1053.cpp files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.