VS1053 shield+ Pushbutton to start Playing

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
KERI
 
Posts: 13
Joined: Fri Jan 08, 2016 9:40 am

VS1053 shield+ Pushbutton to start Playing

Post by KERI »

Hi all.

I am working on a Halloween project. Short story: Stand-up coffin's lid opens, skeleton 'jumps' out, (skeleton speaks, jaw and LED eyes move on the sound-this part is done by Arduino mega and VS1053 shield), skeleton retracts into coffin. The coffin and skeleton movements are done by relays.
I had the next sketch working, based on the example 'player simple':

Code: Select all

/*18AUG21 
    Scope is: when skeleton is outside the coffin, the jaw moves with the spoken words.
    This will be activated when the end switch is made. (Have to implement this switch).

    jawservo works and eyeleds are working.

*/

/***************************************************

  Designed specifically to work with the Adafruit VS1053 Music Maker shield
  ----> https://www.adafruit.com/product/1788

 ****************************************************/

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

Servo jaw;                  // Creates a servo object called jaw
const int jawPin = 8;       // Connect jaw to Pin 8

const int ledPin = 9;       // Led's on Pin 9
int audioVal = 0;           // 
const int audioPin =  0;    // Connect to audio output to AI Pin 0
const int buttonPin = 2;    // Switch Case Open contact connect to Pin 2
int buttonState = 0;        // Variable for reading S(witch)C(coffin)O(pen) contact

int busy;

// See http://arduino.cc/en/Reference/SPI "Connections"


// 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 =
  
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

void setup()
{
  Serial.begin(9600);
  jaw.attach(jawPin);          // Attaches the jawservo on jawPin
  jaw.write(-90);                // Put servo on -90 degrees
  pinMode(jawPin, OUTPUT);
  pinMode (buttonPin, INPUT);
  pinMode (ledPin, OUTPUT);   // set pin as output
  pinMode (ledPin, HIGH);     // set pin HIGH or LOW

  Serial.println("Adafruit VS1053 Simple_servo 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"));

  musicPlayer.sineTest(0x88, 500);    // Make a tone to indicate VS1053 is working

  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);            // don't do anything more
  }
  Serial.println("SD OK!");

  //----list files----
  printDirectory(SD.open("/"), 0);

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

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int

  // Play one file, don't return until complete   //Important, track name: 8  letters dot 3 letters.
  //Serial.println(F("Playing track 001"));
  // musicPlayer.playFullFile("/track001.mp3");
  // Play another file in the background, REQUIRES interrupts!
  Serial.println(F("Playing track 002"));
  musicPlayer.startPlayingFile("/track002.mp3");

}


void loop() {

  musicplayer();

}


void musicplayer()
{
  int val = analogRead(audioPin);
  val = map(val, 250, 1023, 15, 45);    // Analog input between 0 and 1023 --> digital output between 0 and 255
  jaw.write(val);
  audioVal = analogRead(audioPin);      // Read the audioPin
  digitalWrite(ledPin, audioVal);   // Light up the LED's. (original: audioVal / 4)
  
  {
    busy = analogRead(audioPin);



   /* // File is playing in the background
    if (musicPlayer.stopped()) {
      Serial.println("Done playing music");
      while (1) {
        delay(10);  // we're done! do nothing...
      }
    }*/
    /*if (Serial.available()) {
      char c = Serial.read();

      // if we get an 's' on the serial console, stop!
      if (c == 's') {
        musicPlayer.stopPlaying();
      }

      // if we get an 'p' on the serial console, pause/unpause!
      if (c == 'p') {
        if (! musicPlayer.paused()) {
          Serial.println("Paused");
          musicPlayer.pausePlaying(true);
        } else {
          Serial.println("Resumed");
          musicPlayer.pausePlaying(false);
        }
      }
    }*/

    delay(100);
  }
}


/// File listing helper
void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      //Serial.println("**nomorefiles**");
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}
As the coffin's lid opens, a (micro-)switch (for now a pushbutton) has to activate the sketch.
I did found some info @ viewtopic.php?f=31&t=79556#wrap and tried to implement it at different positions. Most is now placed right under 'void loop()' in between the //////////// lines //////////////.

Code: Select all

[code]
/*18bAUG21
    Scope is: when skeleton is outside the coffin, the jaw moves with the spoken words.
    This will be activated when the end switch is made. (Have to implement this switch).

    jawservo works and eyeleds are working.

*/

/***************************************************

  Designed specifically to work with the Adafruit VS1053 Music Maker shield
  ----> https://www.adafruit.com/product/1788

 ****************************************************/

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


// See http://arduino.cc/en/Reference/SPI "Connections"


// 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 shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

Servo jaw;                  // Creates a servo object called jaw
const int jawPin = 8;       // Connect jaw to Pin 8
const int ledPin = 9;       // Led's on Pin 9

const int audioPin =  0;    // Connect to audio output to AI Pin 0
int audioVal = 0;           //

const int c_s_o_Pin = 2;  // Cuffin Switch Open contact connect to Pin 2

uint8_t buttonState,        // Variable for reading S(witch)C(coffin)O(pen) contact
        prevButtonState;

int busy;


void setup()
{
  Serial.begin(9600);
  jaw.attach(jawPin);          // Attaches the jawservo on jawPin
  jaw.write(-90);                // Put servo on -90 degrees
  pinMode(jawPin, OUTPUT);
  pinMode (c_s_o_Pin, INPUT);   //maybe ...INPUT_PULLUP
  buttonState = HIGH;
  prevButtonState = HIGH;
  pinMode (ledPin, OUTPUT);   // set pin as output
  pinMode (ledPin, HIGH);     // set pin HIGH or LOW


  Serial.println("Adafruit VS1053 Simple_servo 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"));

  //musicPlayer.sineTest(0x88, 500);    // Make a tone to indicate VS1053 is working

  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);            // don't do anything more
  }
  Serial.println("SD OK!");

  //----list files----
  //printDirectory(SD.open("/"), 0);

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

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int

  // Play one file, don't return until complete   //Important, track name: 8  letters dot 3 letters.
  //Serial.println(F("Playing track 001"));
  // musicPlayer.playFullFile("/track001.mp3");
  // Play another file in the background, REQUIRES interrupts!
  Serial.println(F("Playing track 002"));
  musicPlayer.startPlayingFile("/track002.mp3");



}


void musicplayer()
{
  int val = analogRead(audioPin);
  val = map(val, 250, 1023, 15, 45);    // Analog input between 0 and 1023 --> digital output between 0 and 255
  jaw.write(val);
  audioVal = analogRead(audioPin);      // Read the audioPin
  digitalWrite(ledPin, audioVal);   // Light up the LED's. (original: audioVal / 4)

  {
    busy = analogRead(audioPin);
  }
}

void loop()
{
  ///////////////////////////////////////////////////////////////////////////////////////////
  buttonState = debounceRead(c_s_o_Pin);
  if ((LOW == buttonState) && (buttonState != prevButtonState))   //if button was just pressed
  {

    if (musicPlayer.stopped())
    {
      if (! musicPlayer.startPlayingFile("/track002.mp3")) {
        Serial.println("Could not open file track002.mp3");
        while (1);
      }
      Serial.println (F("Started playing"));
    }
    else
    {
      musicPlayer.stopPlaying();
    }
  }
  prevButtonState = buttonState;       //update prevButtonState
  ///////////////////////////////////////////////////////////////////////////////////////////
}

//Use like digitalRead. Incorporates button debouncing.
uint8_t debounceRead(int pin)
{
  uint8_t pinState = digitalRead(pin);
  uint32_t timeout = millis();
  while (millis() < timeout + 10)
  {
    if (digitalRead(pin) != pinState)
    {
      pinState = digitalRead(pin);
      timeout = millis();
    }
  }
  return pinState;


}

////////// tot hier okay, nog geen jaw functie
/*

  {
  musicplayer();
  }

*/
/*void musicplayer()
{
  int val = analogRead(audioPin);
  val = map(val, 250, 1023, 15, 45);    // Analog input between 0 and 1023 --> digital output between 0 and 255
  jaw.write(val);
  audioVal = analogRead(audioPin);      // Read the audioPin
  digitalWrite(ledPin, audioVal);   // Light up the LED's. (original: audioVal / 4)

  {
    busy = analogRead(audioPin);
  }
}
*/
/*
  {




    // File is playing in the background
    if (musicPlayer.stopped()) {
      Serial.println("Done playing music");
      while (1) {                             // original (1)
        delay(10);  // we're done! do nothing...
      }
    }

    if (Serial.available()) {
      char c = Serial.read();

      // if we get an 's' on the serial console, stop!
      if (c == 's') {
        musicPlayer.stopPlaying();
      }

      // if we get an 'p' on the serial console, pause/unpause!
      if (c == 'p') {
        if (! musicPlayer.paused()) {
          Serial.println("Paused");
          musicPlayer.pausePlaying(true);
        } else {
          Serial.println("Resumed");
          musicPlayer.pausePlaying(false);
        }
      }
    }

    delay(100);
  }
  }


  /// File listing helper
  void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      //Serial.println("**nomorefiles**");
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println(" / ");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
  }
*/
[/code]

When the arduino is powered, it waits till the pushbutton is pressed, the MP3 will play, but no jaw movement or LED's are powered.

Can someone send me in the right direction? Or should I use a different 'example sketch'?

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: VS1053 shield+ Pushbutton to start Playing

Post by mikeysklar »

Which model of servo are you using?

It looks like you have some LEDs on pin 9 are those the ones not lighting up?

User avatar
KERI
 
Posts: 13
Joined: Fri Jan 08, 2016 9:40 am

Re: VS1053 shield+ Pushbutton to start Playing

Post by KERI »

Thanks for you quick reply.
It's a standard servo "Tower Pro 9G SG 90'.
On pin 9 are the LED's for the eyes connected. Pin 8 is for the jawservo.
As mentioned before, it all worked with the first sketch.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: VS1053 shield+ Pushbutton to start Playing

Post by mikeysklar »

Thank you for the clarification.

Comparing both files shows quite a bit of changes with pins, variable naming, music settings and the main loop.

I would create a third sketch (18c) that starts with the original code and slowly adds in one change at a time. It might help to make a small fourth sketch for something like the coffin opening to make sure that works standalone then paste that into the third sketch as you go.

User avatar
KERI
 
Posts: 13
Joined: Fri Jan 08, 2016 9:40 am

Re: VS1053 shield+ Pushbutton to start Playing

Post by KERI »

I compaired both sketches and see nothing strange except the order. I do understand that the order doesn't realy matters as long as what belongs in the 'void setup()' stays in the setup, etc,. etc. I did change the 'buttonPin'name in the whole sketch into 'c_s_o_Pin' (Cuffin Switch Open) because that's what it is when operating. No pin numbers are changed.

After that I added:

Code: Select all

uint8_t buttonState,        // Variable for reading S(witch)C(coffin)O(pen) contact
        prevButtonState;
and at the start of the 'void loop()' I added:

Code: Select all

  buttonState = debounceRead(c_s_o_Pin);
  if ((LOW == buttonState) && (buttonState != prevButtonState))   //if button was just pressed
  {

    if (musicPlayer.stopped())
    {
      if (! musicPlayer.startPlayingFile("/track002.mp3")) {
        Serial.println("Could not open file track002.mp3");
        while (1);
      }
      Serial.println (F("Started playing"));
    }
    else
    {
      musicPlayer.stopPlaying();
    }
  }
  prevButtonState = buttonState;       //update prevButtonState
  ///////////////////////////////////////////////////////////////////////////////////////////
}

//Use like digitalRead. Incorporates button debouncing.
uint8_t debounceRead(int pin)
{
  uint8_t pinState = digitalRead(pin);
  uint32_t timeout = millis();
  while (millis() < timeout + 10)
  {
    if (digitalRead(pin) != pinState)
    {
      pinState = digitalRead(pin);
      timeout = millis();
    }
  }
  return pinState;


}
I also 'scratched' some lines out by using /* and */ because the had no value at the moment.

Anyways, I will try the next couple of day your advise.

User avatar
KERI
 
Posts: 13
Joined: Fri Jan 08, 2016 9:40 am

Re: VS1053 shield+ Pushbutton to start Playing

Post by KERI »

Hi @mikeysklar,
Back again. I end up using the following code for use a button (switch in my case) to start the sound:

Code: Select all

int run;
int buttonPin;

void setup()
{
   run = 0; //starts stopped
   buttonPin = 7; //whatever pin your button is plugged into

   pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
   //code you always run here; you can leave this section blank if you want the entire program to stop and start, or add code here if you want it to always run

   //check button press here and if it is pressed then toggle run variable between 0 and 255; REQUIRED!
   if(digitalRead(buttonPin) == LOW) //funcitons based off of button pulling input pin LOW
   {
      if(run == 0)
      {
          run = 255;
      }
      else
      {
          run = 0;
      }
   }

   if(run > 0)
   {
      //code you only run if button was pressed, stops running when button pressed again, so forth...
   }
}
This is the best I came up with in my sketch:

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"

// 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 shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

int run;        //31AUG
int buttonPin;  //31AUG or line below incl line in void setup()

void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit VS1053 Simple Test");

  run = 0;          //31AUG Starts stopped
  buttonPin = 3;    //31AUG
  pinMode(buttonPin, INPUT_PULLUP); //31AUG


  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
  }

  // list files
  printDirectory(SD.open("/"), 0);

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

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int

  // Play one file, don't return until complete
  Serial.println(F("Playing track 001"));
  musicPlayer.playFullFile("/track001.mp3");
  // Play another file in the background, REQUIRES interrupts!
  Serial.println(F("Playing track 002"));
  musicPlayer.startPlayingFile("/track002.mp3");
}

void loop()

{

  //check button press here and if it is pressed then toggle run variable between 0 and 255; REQUIRED!
  if (digitalRead(buttonPin) == LOW) //functions based off of button pulling input pin LOW
     {
        if(run == 0)
        {
            run = 255;
        }
        else
        {
            run = 0;
        }
      }
   
   if(run > 1)

    // File is playing in the background
    if (musicPlayer.stopped()) {
      Serial.println("Done playing music");
      while (1) {
        delay(10);  // we're done! do nothing...
      }
    }


  delay(100);
}


/// File listing helper
void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      //Serial.println("**nomorefiles**");
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}
The results are, when powered up the files will be played. Push the button will pause the sound, release it the sound will play on. After another push, in the serial monitor the line: "Done playing music" appears. Another push won't do anything.
I think that the file playing will start in the setup() and maybe therefore I seemed not be able to start again.
Even if I take the next line out:

Code: Select all

// File is playing in the background
    if (musicPlayer.stopped()) {
      Serial.println("Done playing music");
      while (1) {
        delay(10);  // we're done! do nothing...
      }
    }
I did also changed the button pin from 2 to 3, this is DREQ and therefore it reacts to the pushbutton ;)
It won't start over after another push. So I think that the sound have to start in void loop() instead of the void setup(), but how do I do that.
Can you help me further?
Many thanks.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: VS1053 shield+ Pushbutton to start Playing

Post by mikeysklar »

Looks like you are getting close.

You can initialize sound from setup(). I don't see an issue with that.

Code: Select all

  if (! musicPlayer.begin()) { // initialise the music player
while (1);
  }

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

  // play song in background using interrupts
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);
In my usage of the VS1053 I had manually stopped the music when a button was press then restarted it.

Code: Select all

switch (mode) {
case INTRO:
musicPlayer.stopPlaying();
musicPlayer.startPlayingFile("atmos.mp3");
This was my button press logic when I was waiting for a keypress:

Code: Select all

// show starts when magic green button is pressed
// until then we just fade into green
currKeyState = digitalRead(BUTTON_PIN);
if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
start_show = 1;
}
prevKeyState = currKeyState;

User avatar
KERI
 
Posts: 13
Joined: Fri Jan 08, 2016 9:40 am

Re: VS1053 shield+ Pushbutton to start Playing

Post by KERI »

FIrst it's hard to find out where to place your 2 parts of code. I do get all the time an error: 'printDirectory'was not declared in this scope', and more.

Code: Select all

D:\Documenten\Hobby's\Halloween projects\2021 Talking skull\31 aug button works sort of\Button_works_sort_of\Button_works_sort_of.ino: In function 'void setup()':
Button_works_sort_of:68:3: error: 'printDirectory' was not declared in this scope
   printDirectory(SD.open("/"), 0);
   ^~~~~~~~~~~~~~
D:\Documenten\Hobby's\Halloween projects\2021 Talking skull\31 aug button works sort of\Button_works_sort_of\Button_works_sort_of.ino: In function 'void loop()':
Button_works_sort_of:103:9: error: 'mode' was not declared in this scope
 switch (mode) {
         ^~~~
D:\Documenten\Hobby's\Halloween projects\2021 Talking skull\31 aug button works sort of\Button_works_sort_of\Button_works_sort_of.ino:103:9: note: suggested alternative: 'modf'
 switch (mode) {
         ^~~~
         modf
Button_works_sort_of:104:6: error: 'INTRO' was not declared in this scope
 case INTRO:
      ^~~~~
D:\Documenten\Hobby's\Halloween projects\2021 Talking skull\31 aug button works sort of\Button_works_sort_of\Button_works_sort_of.ino:104:6: note: suggested alternative: 'INT0'
 case INTRO:
      ^~~~~
      INT0
Button_works_sort_of:139:44: error: a function-definition is not allowed here before '{' token
 void printDirectory(File dir, int numTabs) {
                                            ^
I did checked all the {} and(), these seems to be okay, but have to make lot of changes, like 'mode' and 'INTRO'.

I get more or less lost in it all. Can you be a bit more specific to me?

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: VS1053 shield+ Pushbutton to start Playing

Post by mikeysklar »

Here is a full button playback example you can build and work with to slowly integrate with your code base. I just modified the VS1053_* vars to match our player.

https://github.com/tigoe/SoundExamples/ ... ayback.ino

Code: Select all

/*
  VS1053 example
  using SparkFun MP3 shield
  and Adafruit VS1053 library
  in beatiful harmony
  
  A potentiometer controls the volume.
  A pushbutton plays/pauses the sound

  circuit:
  * VS1053 module attached to the pins described below
  * Potentiometer attached to pin A0
  * Pushbutton on pin 1, connected to ground
  
   note: on the MKR Zero, use the SD card on the MKR rather than 
   one on the sound player module. To do this, set CARDCS to SDCARD_SS_PIN
   
  created 30 Nov 2018
  modified 14 Dec 2018
  by Tom Igoe

*/

#include <SPI.h>
#include <SD.h>
#include <Adafruit_VS1053.h>

// the VS1053 chip and SD card are both SPI devices.
// Set their respective pins:

#define VS1053_RESET    9     // VS1053 reset pin
#define VS1053_CS       10     // VS1053 chip select pin 
#define VS1053_DCS      8     // VS1053 Data/command select pin 
#define CARDCS          4     // SD card chip select pin
#define VS1053_DREQ     3     // VS1053 Data request

const int buttonPin = 1;      // pin for the pushbutton
int lastButtonState = HIGH;   // previous state of the pushbutton

// make an instance of the MP3 player library:
Adafruit_VS1053_FilePlayer mp3Player =
  Adafruit_VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);

// sound file name must be 8 chars .3 chars:
const char soundFile[] = "SOUND001.MP3";

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

  // set the pushbutton pin as an input, using internal pullups:
  pinMode(buttonPin, INPUT_PULLUP);
  
  // reset the VS1053 by taking reset low, then high:
  pinMode(VS1053_RESET, OUTPUT);
  digitalWrite(VS1053_RESET, LOW);
  delay(10);
  digitalWrite(VS1053_RESET, HIGH);

  // initialize the MP3 player:
  if (!mp3Player.begin()) {
    Serial.println("VS1053 not responding. Check to see if the pin numbers are correct.");
    while (true); // stop
  }

  // initialize the SD card on the module:
  if (!SD.begin(CARDCS)) {
    Serial.println("SD failed, or not present");
    while (true);  // stop
  }

  // Set volume for left and right channels.
  // 0 = loudest, 100 = silent:
  mp3Player.setVolume(10, 10);
  
  // use the VS1053 interrrupt pin so it can
  // let you know when it's ready for commands.
  mp3Player.useInterrupt(VS1053_FILEPLAYER_PIN_INT);

  // play file:
  mp3Player.startPlayingFile(soundFile);
  Serial.println("playing");
}

void loop() {
  // read a potentiometer (0-1023 readings) and
  // map to a range from 100 to 0:
  int loudness = map(analogRead(A0), 0, 1023, 100, 0);
  // set the volume:
  mp3Player.setVolume(loudness, loudness);

  // loop the player:
  if (mp3Player.stopped()) {
    mp3Player.startPlayingFile(soundFile);
  }

  // read a pushbutton for play/pause:
  int buttonState = digitalRead(buttonPin);
  // if the button has changed:
  if (buttonState != lastButtonState) {
    // if the button is low:
    if (buttonState == LOW) {
      // switch play/pause state:
      if (mp3Player.paused()) {
        mp3Player.pausePlaying(false);
      } else {
        mp3Player.pausePlaying(true);
      }
    }
  }
  // save current button state for comparison next time:
  lastButtonState = buttonState;
}

User avatar
KERI
 
Posts: 13
Joined: Fri Jan 08, 2016 9:40 am

Re: VS1053 shield+ Pushbutton to start Playing

Post by KERI »

Reading your code, I think I can work with it.
One more question, I noticed that in your code the reset function is activated (pin 9). In the code I used (belongs with the board it says SHIELD_RESET -1 // vs1053 reset pin (unused!). Is it necessary that the reset is active?
Thanks again, will keep you posted with the result.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: VS1053 shield+ Pushbutton to start Playing

Post by mikeysklar »

Keri,

There are two reset pins. A breakout and a reset. You will want them set as follows to stick with the Adafruit examples.

Code: Select all

#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)

User avatar
KERI
 
Posts: 13
Joined: Fri Jan 08, 2016 9:40 am

Re: VS1053 shield+ Pushbutton to start Playing

Post by KERI »

mikeysklar wrote:Keri,

There are two reset pins. A breakout and a reset. You will want them set as follows to stick with the Adafruit examples.

Code: Select all

#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
Mikeysklar,
I did see these lines and as I work with the shield, I left everything for the breakout board away. Still wonder where pin '-1' is?

For now the good news.... I have my sketch working.

Code: Select all

[code]

/*03sept21
    IT WORKS. now some fine-tuning.
*/

/***************************************************

  Designed specifically to work with the Adafruit VS1053 Music Maker shield
  ----> https://www.adafruit.com/product/1788

 ****************************************************/

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


// See http://arduino.cc/en/Reference/SPI "Connections"


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

#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 shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

Servo jaw;                       // Creates a servo object called jaw
const int jawPin = 8;       // Connect Pin8 to jaw
const int ledPin = 9;       // Connect Pin 9 to Led's

const int audioPin =  0;    // Connect to Pin A0 to audio output
int audioVal = 0;               //

const int scoPin = 2;               // Connect Pin 2 to s(witch) c(ase) o(pen) contact
int lastButtonState = HIGH;   // privious state of the pushbutton

const char soundFile[] = "track002.mp3";

int busy;

void setup()
{
  Serial.begin(9600);
  jaw.attach(jawPin);            // Attaches the jawservo on jawPin
  jaw.write(-90);                    // Put servo on -90 degrees
  pinMode(jawPin, OUTPUT);
  pinMode(scoPin, INPUT_PULLUP); // set this pin as an input, using internal pullups
  pinMode(ledPin, OUTPUT);             // set pin as output
  pinMode(ledPin, HIGH);                  // set pin HIGH or LOW

  pinMode (SHIELD_RESET, OUTPUT);
  digitalWrite (SHIELD_RESET, LOW);
  delay (10);
  digitalWrite (SHIELD_RESET, HIGH);

  Serial.println("Adafruit VS1053 Simple_servo Test");

  if (! musicPlayer.begin()) { // initialise the music player
    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1);            // stop
  }
  Serial.println(F("VS1053 found"));

  musicPlayer.sineTest(0x88, 500);    // Make a tone to indicate VS1053 is working

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

  //----list files----
  //printDirectory(SD.open("/"), 0);

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


  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int

  // Play one file, don't return until complete   //Important, track name: 8 chrs.3 chars
  //Serial.println(F("Playing track 001"));
  // musicPlayer.playFullFile("/track001.mp3");
  // Play another file in the background, REQUIRES interrupts!
  Serial.println(F("Playing track 002"));
  musicPlayer.startPlayingFile("/soundFile");

}


void loop() {

  musicplayer();

}

void musicplayer()


{

  int val = analogRead(audioPin);
  val = map(val, 250, 1023, 15, 45);    // Analog input between 0 and 1023 --> digital output between 0 and 255
  jaw.write(val);
  audioVal = analogRead(audioPin);      // Read the audioPin
  digitalWrite(ledPin, audioVal);       // Light up the LED's. (original: audioVal / 4)

  {
    busy = analogRead(audioPin);

    //loop the player:
    if (musicPlayer.stopped()) {
      musicPlayer.startPlayingFile(soundFile);
    }
    //read a pushbutton for play/pause:
    int buttonState = digitalRead(scoPin);
    //if the button has changed:
    if (buttonState != lastButtonState)  {
      //if the buttton is low:
      if (buttonState == LOW) {
        //switch play/pause state:
        if (musicPlayer.paused()) {
          musicPlayer.pausePlaying(false);
        } else {
          musicPlayer.pausePlaying(true);
        }
      }
    }
    // save current button state for comparison next time:
    lastButtonState = buttonState;
  }
}

[/code]

Now some fine tuning for the jaw movement and the 'flashing' eye led's.
It will be awesome.
Thanks again for your input and for later.... HAPPY HALLOWEEN

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: VS1053 shield+ Pushbutton to start Playing

Post by mikeysklar »

The pin '-1' setting is equivalent to saying UNUSED (non-existent). There is no pin -1.

Congratulations on getting your project going. Good work!

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

Return to “Other Arduino products from Adafruit”