0

VS1053 can't stop playback
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

VS1053 can't stop playback

by boringuser on Sat May 15, 2021 2:28 pm

Hey folks, I'm trying to use the MPR121 capacitive touch sensor breakout along with the VS1053 to control starting and stopping of sound file playback. I want to start playing when a pin is touched, and stop playing when it's released. I can't seem to get this to work. The audio file starts playing, but plays through to the end even if the pin is released. Also, no output is written to the serial monitor until after the file is completely played, and the output indicates that I never get to the part of the code where stopPlaying is called. Here's my code... any ideas would be much appreciated. Thanks!

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_VS1053.h>
#include "Adafruit_MPR121.h"

// These are the pins used
#define VS1053_RESET   -1     // VS1053 reset pin (not used!)

// Feather ESP8266
#if defined(ESP8266)
  #define VS1053_CS      16     // VS1053 chip select pin (output)
  #define VS1053_DCS     15     // VS1053 Data/command select pin (output)
  #define CARDCS          2     // Card chip select pin
  #define VS1053_DREQ     0     // VS1053 Data request, ideally an Interrupt pin

// Feather ESP32
#elif defined(ESP32)
  #define VS1053_CS      32     // VS1053 chip select pin (output)
  #define VS1053_DCS     33     // VS1053 Data/command select pin (output)
  #define CARDCS         14     // Card chip select pin
  #define VS1053_DREQ    15     // VS1053 Data request, ideally an Interrupt pin

// Feather Teensy3
#elif defined(TEENSYDUINO)
  #define VS1053_CS       3     // VS1053 chip select pin (output)
  #define VS1053_DCS     10     // VS1053 Data/command select pin (output)
  #define CARDCS          8     // Card chip select pin
  #define VS1053_DREQ     4     // VS1053 Data request, ideally an Interrupt pin

// WICED feather
#elif defined(ARDUINO_STM32_FEATHER)
  #define VS1053_CS       PC7     // VS1053 chip select pin (output)
  #define VS1053_DCS      PB4     // VS1053 Data/command select pin (output)
  #define CARDCS          PC5     // Card chip select pin
  #define VS1053_DREQ     PA15    // VS1053 Data request, ideally an Interrupt pin

#elif defined(ARDUINO_NRF52832_FEATHER )
  #define VS1053_CS       30     // VS1053 chip select pin (output)
  #define VS1053_DCS      11     // VS1053 Data/command select pin (output)
  #define CARDCS          27     // Card chip select pin
  #define VS1053_DREQ     31     // VS1053 Data request, ideally an Interrupt pin

// Feather M4, M0, 328, nRF52840 or 32u4
#else
  #define VS1053_CS       6     // VS1053 chip select pin (output)
  #define VS1053_DCS     10     // VS1053 Data/command select pin (output)
  #define CARDCS          5     // Card chip select pin
  // DREQ should be an Int pin *if possible* (not possible on 32u4)
  #define VS1053_DREQ     9     // VS1053 Data request, ideally an Interrupt pin

#endif

#ifndef _BV
  #define _BV(bit) (1 << (bit))
#endif

Adafruit_VS1053_FilePlayer musicPlayer =
  Adafruit_VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);

Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;

void setup() {
  Serial.begin(115200);

  while (!Serial) { // needed to keep leonardo/micro from starting too fast!
    delay(10);
  }

  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
 
  // initialise the music player
  if (!musicPlayer.begin()) {
    Serial.println(F("Couldn't find VS1053"));
    while (1);
  }

  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);
  }

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

void loop() {
  // Get the currently touched pads
  currtouched = cap.touched();
 
  for (uint8_t i=0; i<1; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" touched");
      if (i == 0 && musicPlayer.stopped()) {
        Serial.println("   starting");
        musicPlayer.startPlayingFile("/inevit.wav");
      }
    }
   
    // if it *was* touched and now *isnt*, alert!
    if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" released");
      if (i == 0 && !musicPlayer.stopped()) {
        Serial.println("   stopping");
        musicPlayer.stopPlaying();
      }
    }
  }
 
  // reset our state
  lasttouched = currtouched;
}

boringuser
 
Posts: 2
Joined: Fri Nov 10, 2017 2:59 am

Re: VS1053 can't stop playback

by mikeysklar on Tue May 18, 2021 1:05 pm

@boringuser,

If you add a delay of a few thousand milliseconds and manually call musicPlayer.stopped() does that work? I'm trying to determine if this is more an issue with capacitive touch/remove recognition than anything to do with the musicPlayer library.

mikeysklar
 
Posts: 4609
Joined: Mon Aug 01, 2016 8:10 pm

Re: VS1053 can't stop playback

by boringuser on Sun May 30, 2021 2:28 pm

@mikeysklar,

Apologies, I haven't been able to get back to this project in a couple weeks. I tried adding the delay as you said, but it didn't seem to have an effect:

Code: Select all | TOGGLE FULL SIZE
  musicPlayer.startPlayingFile("/inevit.wav");
  delay(1000);
  musicPlayer.stopPlaying();


The goal I have for this project is to be able to start and stop playing the sound file based on some input (capacitive touch in this case, but could be anything). Wondering if maybe the VS1053 isn't the right board for the task. Any ideas much appreciated. Thank you!

boringuser
 
Posts: 2
Joined: Fri Nov 10, 2017 2:59 am

Re: VS1053 can't stop playback

by mikeysklar on Mon May 31, 2021 2:36 pm

This sounds like an interrupt issue you need to enable them and some processors do not support them. Which processor are you using with your VS1053? VS1053_DREQ is the macro to look at.

We also have a particularly good thread from the forums where someone was trying to achieve the same as you except with pushbuttons so they were using a code block this:

Code: Select all | TOGGLE FULL SIZE
if (musicPlayer.stopped())
  {
    do {
    int trackNumber = random(0, NUM_TRACKS);
    bool pressed = false;
    musicPlayer.startPlayingFile(music[trackNumber]);
    Serial.print(F("Playing Random Track ")); Serial.println(trackNumber);
    while (!musicPlayer.stopped()) && (!pressed)) {
      pressed = buttonPressed();
    }
    delay(100);
    } while (!pressed);
  }


viewtopic.php?f=31&t=79556

I think you options are

1) Choose a different controller with interrupt support.

2) Consider something like this Audio FX mini.

https://www.adafruit.com/product/2341

mikeysklar
 
Posts: 4609
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.