Sound playing: micro pause before playing in loop

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

Sound playing: micro pause before playing in loop

Post by Myriam_ »

Hi everyone,

I work with an Arduino mega and an adafruit music maker.
I posted few days ago for a problem with the sound that wouldn't stop and I figured out why, but know I have another issue with my set up.

With my code I play a song when there is an error in my task (more precisely, when I push a button, cf picture in links below and code below) until a detector is LOW once (departure detector, at the end of the code).
But there is a problem while playing the error sound, when I press the button in the first trial it works properly but then if another trial begins and there is another error and I press the button, the sound play, make a micropause, and then continue.

Here is my 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;
      }
    }
  }
}
Here are the pictures of my set-up:

https://upload.wikimedia.org/wikipedia/ ... om-min.jpg
https://upload.wikimedia.org/wikipedia/ ... no-min.jpg

I don’t understand why I have this pause!

Can you help me with that?
Thanks!!

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

Return to “Arduino Shields from Adafruit”