Music maker shield : musicPlayer.stopPlaying() not working

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

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Myriam_
 
Posts: 5
Joined: Wed Dec 14, 2022 11:34 am

Music maker shield : musicPlayer.stopPlaying() not working

Post by Myriam_ »

Hi everyone,

I have an issue using my music maker shield. I have an Arduino Mega with the music maker shield on it.
I want to play a sound (looping) when I press a button but I want this song to be over when a certain receptor is activated (here: LOW). However, I'm not succeeding to stop my song with the musicPlayer.stopPlaying() function.
Here is a part of my code that is not working :

Code: Select all

if (b_a == 1) {//if button is active
      eia = 0;  // something related with a reward (nothing important here)
      state = digitalRead(pinDepart);
      if (state == HIGH) { //if the detector is not active
        musicPlayer.startPlayingFile("errorsnd.wav");  //play the song
      }
      else if (state == LOW) {    // if the detector is active
        musicPlayer.stopPlaying();     //stop the song
        timing = millis(); 
        Serial.print("end");
        Serial.print(",");
        Serial.println(timing);
        move_door(1, dirPin2, stepPin2, 0); //door is closing
        s_p = 0;  //some parameters I need to change for my code, nothing important here
        sia = 1;  
        s_a = 0;
        b_a = 0;
      }
    }
I think that the problem is, since the song is playing, I can't detect the detector but I don't know how to deal with this.

Can someone help me with that?

Thanks a lot!

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

Re: Music maker shield : musicPlayer.stopPlaying() not working

Post by mikeysklar »

With a serial console open can you see the word end being printed out when you press the button?

Post your full code and button wiring.

User avatar
Myriam_
 
Posts: 5
Joined: Wed Dec 14, 2022 11:34 am

Re: Music maker shield : musicPlayer.stopPlaying() not working

Post by Myriam_ »

mikeysklar wrote: Thu Dec 15, 2022 5:55 pm With a serial console open can you see the word end being printed out when you press the button?

Post your full code and button wiring.
Hi mikeysklar,

Thanks for your answer. Here is my full code:

Code: Select all

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

// 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);  // these are all variables state above

//pins for the task

const int pinDepart = 22;  // pin departure detector
const int pinReward = 24;  // pin arrival detector

const int pinPompe = 26;   // pin arrival reward 
const int pinPompe2 = 28;  // pin departure reward

const int pinButton = 30;       //pin button error (pressed when there is an error)
const int pinButtonporte = 32;  //pin button departure door

const int pinBruit = 34; //pin light when there is an error (when the button is pressed)

// Door reward
const int stepPin = 31;  
const int dirPin = 33;   
const int enPin = 35;    

// Door reward 2
const int stepPin2 = 37;  
const int dirPin2 = 39;   
const int enPin2 = 35;    

// Declaring state variables
int s_a, e_a, b_a, s_p; //s_a : dectector departure activated, e_a : detector arrival activated
//s_p: door departure open, b_a : button error pressed 
int state;

unsigned long int timing;

// Allowance variables
int sia, eia; //sia : detector departure can be activated, eia: detector arrival can be detected

// Pump variables
const float delayPump = 30;  // ms

void setup() {
  Serial.begin(9600);
  musicPlayer.begin();
  SD.begin(CARDCS);
  musicPlayer.setVolume(40, 40);
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);
  pinMode(pinDepart, INPUT);
  pinMode(pinReward, INPUT);
  pinMode(pinButton, INPUT_PULLUP);
  pinMode(pinButtonporte, INPUT_PULLUP);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  pinMode(enPin2, OUTPUT);
  pinMode(pinPompe, OUTPUT);  
  pinMode(pinPompe2, OUTPUT);
  pinMode(pinBruit, OUTPUT);
  digitalWrite(enPin, LOW);
  digitalWrite(enPin2, LOW);
  s_a = 0;
  e_a = 0;
  b_a = 0;  
  s_p = 0; 
  sia = 1;
  eia = 0;
}


void release_reward(int delayPump, int pinPompe) {
  delay(delayPump);
  digitalWrite(pinPompe, HIGH);
  delay(delayPump / 2);
  digitalWrite(pinPompe, LOW);
}

void release_reward_loop(int delayPump, int pinPompe, int n_releases) {
  int i;
  for (i = 0; i < n_releases; i++) {
    release_reward(delayPump, pinPompe);
  }
}

void move_door(int closedoor, int dirPin, int stepPin, int port) {
  int d = 50;
  if (port == 0) {
    d = 55;
  }

  int msg = HIGH;
  if (closedoor) {
    msg = LOW;
  }
  for (int x = 0; x < 16.4 * 1600; x++) {
    digitalWrite(dirPin, msg);  // Enables the motor to move in a particular direction
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(d);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(d);
  }
}

void loop() {
  if (s_p == 0) {
    if (digitalRead(pinButtonporte) == HIGH) {  //If I opened the door then:
      move_door(0, dirPin2, stepPin2, 0);
      timing = millis();
      Serial.print("Detecteur_depart_aller, debut trial");
      Serial.print(",");
      Serial.println(timing);
      s_p = 1;  //door is open
      s_a = 1;  //departure detector has been activated
      eia = 1;  // arrival detector can be detected
      sia = 0;  // departure detector cannot be detected anymore
    }
  } else {
    if (b_a == 0) {  //if no error
      state = digitalRead(pinReward);
      if (state == LOW) {  //if arrival detector activated
        if (s_a == 1 && e_a == 0 && eia == 1) {
          timing = millis();
          Serial.print("Detecteur_arrivee, fin aller");
          Serial.print(",");
          Serial.println(timing);
          s_a = 1;
          e_a = 1;
          eia = 0;
          sia = 1;
          musicPlayer.startPlayingFile("rewardsd.wav"); //a sound cause the trial is correct
          move_door(1, dirPin, stepPin, 1); //door is moving
          delay(2000);
          release_reward_loop(delayPump, pinPompe, 10); //reward is released 
          delay(1000);
          move_door(0, dirPin, stepPin, 1); //door is moving again
          timing = millis();
          Serial.print("Detecteur_arrivee, debut retour");
          Serial.print(",");
          Serial.println(timing);
        }
      }
      state = digitalRead(pinDepart);
      if (state == LOW) {                        //if departure dectector is activated
        if (s_a == 1 && e_a == 1 && sia == 1) {  // if the task has been completed :
          s_a = 0;
          e_a = 0;
          timing = millis();
          Serial.print("Detecteur_depart_retour, fin trial");
          Serial.print(",");
          Serial.println(timing);
          move_door(1, dirPin2, stepPin2, 0);  //door is closing
          delay(1000);
          release_reward_loop(delayPump, pinPompe2, 10);  //reward 
          s_p = 0;     //door closed                                   
        }
      }

      if (digitalRead(pinButton) == HIGH) {  //if there is an error (I pressed the button)
        b_a = 1; //button is pressed
        digitalWrite(pinBruit, HIGH);  //light is ON
        timing = millis();
        Serial.print("Debut erreur");
        Serial.print(",");
        Serial.println(timing);
      }
    } else if (b_a == 1) {  //if button is active
      eia = 0;              // arrival detector can't be detected anymore
      state = digitalRead(pinDepart);
      if (state == HIGH) {
        musicPlayer.startPlayingFile("errorsnd.wav");  //play error sound until detector is low 1 time
      } else if (state == LOW) {         // if the depature detector has been activated once            
        musicPlayer.stopPlaying(); //stop the sound
        digitalWrite(pinBruit, LOW);  //the light is OFF now
        timing = millis();
        Serial.print("end");
        Serial.print(",");
        Serial.println(timing);
        move_door(1, dirPin2, stepPin2, 0); //door is closing
        s_p = 0;  //door is closed
        sia = 1;  //everything is set to do a new trial 
        s_a = 0;
        b_a = 0;
      }
    }
  }
}
When I press the button I have "Debut erreur" printed and then my sound is playing (in loop). But with my code when the departure detector are supposed to be detected (here is LOW) the sound is supposed to stop but it's not the case and nothing is printed, the "end" is not printed. I think there is a bug and my detector cannot be detected right if the sound is playing! Cause if i let my hand in front of the detector at some point they can be detected, but this should not work like this. When I put my hand once it supposed to be detected and the sound is supposed to stop to allow the door to be closing.

Can you help me with that?

Thanks again!!

User avatar
Myriam_
 
Posts: 5
Joined: Wed Dec 14, 2022 11:34 am

Re: Music maker shield : musicPlayer.stopPlaying() not working

Post by Myriam_ »

Ok... I solve my issue... I didn't plugged my pin detector on the pin 3 of the Adafruit shield that allow to interrrupt the sound to play!
But with this I have another issue... When my sound is playing again (when there is more than 2 errors), the sound play just a bit, few ms, do a micropause and play again without pausing until the detector is LOW. I don't know why I have this pause..

Any suggestion where the error can be?

Thanks a lot!

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

Re: Music maker shield : musicPlayer.stopPlaying() not working

Post by mikeysklar »

Glad you figured out this was a wiring issue with stopPlaying not getting triggered.

Can you start a new issue in regards to the micropause you are experiencing. Go ahead and include details about your controller board, code and a photo with that thread.

User avatar
Myriam_
 
Posts: 5
Joined: Wed Dec 14, 2022 11:34 am

Re: Music maker shield : musicPlayer.stopPlaying() not working

Post by Myriam_ »

mikeysklar wrote: Mon Dec 19, 2022 7:15 pm Glad you figured out this was a wiring issue with stopPlaying not getting triggered.

Can you start a new issue in regards to the micropause you are experiencing. Go ahead and include details about your controller board, code and a photo with that thread.
Hi,

Ok I will start a new issue!

Thanks!

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

Return to “Arduino Shields from Adafruit”