DFPlayer Mini gets stuck randomly

Hi guys

A couple of years ago I built a Laser Tag System based on an existig code. The system worked pretty good back then. Unfortunately I lost the latest source code because of a hard drive fail (I know I should have clouded it).
In the recent weeks I tried to put everything back together from older code versions. Everything works fine, except from the sounds I play to the DFPlayer Mini, which is wired to the core, an Arduino Uno SMD (Rev3). Software-wise I communicate via the AltSoftSerial- and the DFPlayerMini_Fast.
The problem I get is that the DFPlayer gets stuck after like 60, 70 "shots" fired or so. After a couple of seconds it recovers and starts working again.
I'm a 100 percent sure that it must work with my hardware setup and software approach, since I have the old compiled code that works without problems.
Is anyone experienced with the DFPlayer Mini? I guess it must be some kind of buffer issue on the Player or something.
Any help is very much appreciated!

I'll post snippets since the entire code has 650 lines :wink:
Here I initialize the Player:

AltSoftSerial altSerial; // Receive = 8, Transmit = 9
SoftwareSerial HC12(4, 10);

DFPlayerMini_Fast myDFPlayer;
//
void setup() {
  // Serial coms set up to help with debugging.
  altSerial.begin(9600);
  Serial.begin(9600);
  HC12.begin(9600);              
  Serial.println("Startup...");

  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  
  if (!myDFPlayer.begin(altSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true){
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }
  Serial.println(F("DFPlayer Mini online."));

  
  myDFPlayer.volume(25);  //Set volume value. From 0 to 30

And here is very the shooting happens:

void shoot() {
  if(FIRE == 1){ // Has the trigger been pressed?
    //Serial.println("FIRE 1");
    sendPulse(IRtransmitPin, 4); // Transmit Header pulse, send pulse subroutine deals with the details
    delayMicroseconds(IRpulse);
 
    for(int i = 0; i < 8; i++) { // Transmit Byte1
      if(byte1[i] == 1){
        sendPulse(IRtransmitPin, 1);
        //Serial.print("1 ");
      }
      //else{Serial.print("0 ");}
      sendPulse(IRtransmitPin, 1);
      delayMicroseconds(IRpulse);
    }

    for(int i = 0; i < 8; i++) { // Transmit Byte2
      if(byte2[i] == 1){
        sendPulse(IRtransmitPin, 1);
       // Serial.print("1 ");
      }
      //else{Serial.print("0 ");}
      sendPulse(IRtransmitPin, 1);
      delayMicroseconds(IRpulse);
    }
    
    if(myParity == 1){ // Parity
      sendPulse(IRtransmitPin, 1);
    }
    sendPulse(IRtransmitPin, 1);
    delayMicroseconds(IRpulse);
    //Serial.println("");
    //Serial.println("DONE 1");

    myDFPlayer.play(1);
    
  }

It is best to post the complete, compilable, code.

Hmm...

Hmm...

Hmm...

If everything works and everything works, except... my guess is; wiring.

What is a "shot?"

Describe the sequence of events leading to the issue, including vernacular. Include the wiring of the DFPlayerMini in question.

I didn't change the wiring. The DFPlayer is supplied by clean 5 Volts and RX and TX are correctly connected to the UNO. Otherwise it wouldn't play any sounds at all.
A "shot" is an infrared pulse with information triggered by a Laser Tag "weapon".

Here is the entire code:


#include <DFPlayerMini_Fast.h>
#include "SoftwareSerial.h"
#include <AltSoftSerial.h>


AltSoftSerial altSerial; // Receive = 8, Transmit = 9
SoftwareSerial HC12(4, 10);

DFPlayerMini_Fast myDFPlayer;


// Digital IO's
int triggerPin             = 3;      // Push button for primary fire. Low = pressed
int trigger2Pin            = 16;     // Push button for secondary fire. Low = pressed
int speakerPin             = 14;      // Direct output to piezo sounder/speaker
int HitVibration           = 13;      // Audio Trigger. Can be used to set off sounds recorded in the kind of electronics you can get in greetings card that play a custom message.
int lifePin                = 6;      // An analogue output (PWM) level corresponds to remaining life. Use PWM pin: 3,5,6,9,10 or 11. Can be used to drive LED bar graphs. eg LM3914N
int ammoPin                = 5;      // An analogue output (PWM) level corresponds to remaining ammunition. Use PWM pin: 3,5,6,9,10 or 11.
int hitPin                 = 7;      // LED output pin used to indicate when the player has been hit.
int IRtransmitPin          = 2;      // Primary fire mode IR transmitter pin: Use pins 2,4,7,8,12 or 13. DO NOT USE PWM pins!! More info: http://j44industries.blogspot.com/2009/09/arduino-frequency-generation.html#more
//int IRtransmit2Pin         = 8;      // Secondary fire mode IR transmitter pin:  Use pins 2,4,7,8,12 or 13. DO NOT USE PWM pins!!
int IRreceivePin           = 12;     // The pin that incoming IR signals are read from
int IRreceive2Pin          = 15;     // Allows for checking external sensors are attached as well as distinguishing between sensor locations (eg spotting head shots)

//Minimum gun requirements: trigger, receiver, IR led, hit LED.

// Player and Game details
int myTeamID               = 1;      // 1-7 (0 = system message)
int myPlayerID             = 2;     // Player ID
int myGameID               = 1;      // Interprited by configureGane subroutine; allows for quick change of game types.
int myWeaponID             = 0;      // Deffined by gameType and configureGame subroutine.
int myWeaponHP             = 0;      // Deffined by gameType and configureGame subroutine.
int maxAmmo                = 0;      // Deffined by gameType and configureGame subroutine.
int maxLife                = 0;      // Deffined by gameType and configureGame subroutine.
int automatic              = 0;      // Deffined by gameType and configureGame subroutine. Automatic fire 0 = Semi Auto, 1 = Fully Auto.
int automatic2             = 0;      // Deffined by gameType and configureGame subroutine. Secondary fire auto?

//Incoming signal Details
int received[18];                    // Received data: received[0] = which sensor, received[1] - [17] byte1 byte2 parity (Miles Tag structure)
int check                  = 0;      // Variable used in parity checking

// Stats
int ammo                   = 0;      // Current ammunition
int magazine               = 15;     // Magazine capacity
int life                   = 0;      // Current life

// Code Variables
int timeOut                = 0;      // Deffined in frequencyCalculations (IRpulse + 50)
int FIRE                   = 0;      // 0 = don't fire, 1 = Primary Fire, 2 = Secondary Fire
int TR                     = 0;      // Trigger Reading
int LTR                    = 0;      // Last Trigger Reading
int T2R                    = 0;      // Trigger 2 Reading (For secondary fire)
int LT2R                   = 0;      // Last Trigger 2 Reading (For secondary fire)

// Signal Properties
int IRpulse                = 600;    // Basic pulse duration of 600uS MilesTag standard 4*IRpulse for header bit, 2*IRpulse for 1, 1*IRpulse for 0.
int IRfrequency            = 36;     // Frequency in kHz Standard values are: 38kHz, 40kHz. Choose dependant on your receiver characteristics
int IRt                    = 0;      // LED on time to give correct transmission frequency, calculated in setup.
int IRpulses               = 0;      // Number of oscillations needed to make a full IRpulse, calculated in setup.
int header                 = 4;      // Header lenght in pulses. 4 = Miles tag standard
int maxSPS                 = 10;     // Maximum Shots Per Seconds. Not yet used.
int TBS                    = 0;      // Time between shots. Not yet used.

// Transmission data
int byte1[8];                        // String for storing byte1 of the data which gets transmitted when the player fires.
int byte2[8];                        // String for storing byte1 of the data which gets transmitted when the player fires.
int myParity               = 0;      // String for storing parity of the data which gets transmitted when the player fires.

// Received data
int memory                 = 10;     // Number of signals to be recorded: Allows for the game data to be reviewed after the game, no provision for transmitting / accessing it yet though.
int hitNo                  = 0;      // Hit number
// Byte1
int player[10];                      // Array must be as large as memory
int team[10];                        // Array must be as large as memory
// Byte2
int weapon[10];                      // Array must be as large as memory
int hp[10];                          // Array must be as large as memory
int parity[10];                      // Array must be as large as memory
int HitPlayer               = 0;
byte incomingByte;
const unsigned int READ_BUFFER_SIZE = 11;    //Platz für 10 Zeichen + Terminator

unsigned long previousMillis;
const long Delay = 50;



void setup() {
  // Serial coms set up to help with debugging.
  altSerial.begin(9600);
  Serial.begin(9600);
  HC12.begin(9600);              
  Serial.println("Startup...");

  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  
  if (!myDFPlayer.begin(altSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true){
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }
  Serial.println(F("DFPlayer Mini online."));

  
  myDFPlayer.volume(25);  //Set volume value. From 0 to 30
 



  
  // Pin declarations
  pinMode(triggerPin, INPUT);
  pinMode(trigger2Pin, INPUT);
  pinMode(speakerPin, OUTPUT);
  //pinMode(audioPin, OUTPUT);
  pinMode(lifePin, OUTPUT);
  pinMode(ammoPin, OUTPUT);
  pinMode(hitPin, OUTPUT);
  pinMode(IRtransmitPin, OUTPUT);
  //pinMode(IRtransmit2Pin, OUTPUT);
  pinMode(IRreceivePin, INPUT);
  pinMode(IRreceive2Pin, INPUT);
  pinMode(HitVibration, OUTPUT);

    int pin;
    pin = digitalRead(triggerPin);
 
    if(pin == 0) {    // if trigger is pressed during startup, switch to max Ammo (GameID2)
    myGameID = 2;
    }
    else if(pin == 1) {
    myGameID = 1;
    }


 
  frequencyCalculations();   // Calculates pulse lengths etc for desired frequency
  configureGame();           // Look up and configure game details
  tagCode();                 // Based on game details etc works out the data that will be transmitted when a shot is fired

 
  digitalWrite(triggerPin, HIGH);      // Not really needed if your circuit has the correct pull up resistors already but doesn't harm
  digitalWrite(trigger2Pin, HIGH);     // Not really needed if your circuit has the correct pull up resistors already but doesn't harm
  digitalWrite(HitVibration, LOW);
  myDFPlayer.play(3);                  //"Get ready to play.."
 
  for (int i = 1;i < 254;i++) { // Loop plays start up noise
    analogWrite(ammoPin, i);
    playTone((3000-9*i), 2);
    
  } 

    

 
  // Next 4 lines initialise the display LEDs
  analogWrite(ammoPin, ((int) ammo));
  analogWrite(lifePin, ((int) life));
  lifeDisplay();
  ammoDisplay();



  Serial.println("Ready....");
}


// Main loop most of the code is in the sub routines
void loop(){

  receiveIR();
  if(FIRE != 0){
    shoot();
    ammoDisplay();
  }
  char* str = readLine(HC12);
  if(str != nullptr)
  {
    Serial.print("Eingelesen: ");
    Serial.println(str);
    compare(str);
  }
  triggers();
}


// SUB ROUTINES


void ammoDisplay() { // Updates Ammo LED output
  float ammoF;
  ammoF = (260/maxAmmo) * ammo;
  if(ammoF <= 0){ammoF = 0;}
  if(ammoF > 255){ammoF = 255;}
  analogWrite(ammoPin, ((int) ammoF));
}


void lifeDisplay() { // Updates Life LED output
  float lifeF;
  lifeF = (260/maxLife) * life;
  if(lifeF <= 0){lifeF = 0;}
  if(lifeF > 255){lifeF = 255;}
  analogWrite(lifePin, ((int) lifeF));
} 


char* readLine(Stream& stream)
  {
    static byte index;
    static char buffer[READ_BUFFER_SIZE];

    while (stream.available())
    {
      char c = stream.read();
      //Serial.println("Stream:");
      //Serial.println(c);

      if (c == '\n')  //wenn LF eingelesen
      {
        buffer[index] = '\0';  //String terminieren
        index = 0;
        return buffer;  //melden dass String fertig eingelesen wurde

      }
      else if (c >= 32 && index < READ_BUFFER_SIZE - 1) //solange noch Platz im Puffer ist
      {
        buffer[index++] = c; //Zeichen abspeichern und Index inkrementieren
      }
    }
    return nullptr;
}

void compare(char* str){
      int ShooterID;
      Serial.println("compare running!");
      if(strstr(str, "Shooter")) {
      Serial.println("Shooter received!");
      char* ptr = strchr(str, ':');
      ShooterID = atoi(ptr +1);
      Serial.println("ShooterID:");
      Serial.println(ShooterID);
      if(ShooterID == myPlayerID) {
      Serial.println("Shooter comparison true!");
      myDFPlayer.play(4);
      

      }
   }
}

void receiveIR() { // Void checks for an incoming signal and decodes it if it sees one.
  int error = 0;
 
  if(digitalRead(IRreceivePin) == LOW){    // If the receive pin is low a signal is being received.
    digitalWrite(hitPin,HIGH);
    if(digitalRead(IRreceive2Pin) == LOW){ // Is the incoming signal being received by the head sensors?
      received[0] = 1;
    }
    else{
      received[0] = 0;
    }
   
    while(digitalRead(IRreceivePin) == LOW){
    }
    for(int i = 1; i <= 17; i++) {                        // Repeats several times to make sure the whole signal has been received
      received[i] = pulseIn(IRreceivePin, LOW, 15000);  // pulseIn command waits for a pulse and then records its duration in microseconds.
    }
   
    Serial.print("sensor: ");                            // Prints if it was a head shot or not.
    Serial.print(received[0]); 
    Serial.print("...");
   
    for(int i = 1; i <= 17; i++) {  // Looks at each one of the received pulses
      int receivedTemp[18];
      receivedTemp[i] = 2;
      if(received[i] > (IRpulse - 200) &&  received[i] < (IRpulse + 200)) {receivedTemp[i] = 0;}                      // Works out from the pulse length if it was a data 1 or 0 that was received writes result to receivedTemp string
      if(received[i] > (IRpulse + IRpulse - 200) &&  received[i] < (IRpulse + IRpulse + 200)) {receivedTemp[i] = 1;}  // Works out from the pulse length if it was a data 1 or 0 that was received  
      received[i] = 3;                   // Wipes raw received data
      received[i] = receivedTemp[i];     // Inputs interpreted data
     
      Serial.print(" ");
      Serial.print(received[i]);         // Print interpreted data results
    }
    Serial.println("");                  // New line to tidy up printed results


   
    // Parity Check. Was the data received a valid signal?
    check = 0;
    for(int i = 1; i <= 16; i++) {
      if(received[i] == 1){check = check + 1;}
      if(received[i] == 2){error = 1;}
    }
    check = check >> 0 & B1;
    // Serial.println(check);
    if(check != received[17]){error = 1;}
    if(error == 0){Serial.println("Valid Signal");}
    else{Serial.println("ERROR");}
    if(error == 0){interpritReceived();}
    digitalWrite(hitPin,LOW);
  }
}


void interpritReceived(){  // After a message has been received by the ReceiveIR subroutine this subroutine decidedes how it should react to the data
  if(hitNo == memory){hitNo = 0;} // hitNo sorts out where the data should be stored if statement means old data gets overwritten if too much is received
  team[hitNo] = 0;
  player[hitNo] = 0;
  weapon[hitNo] = 0;
  hp[hitNo] = 0;
  // Next few lines Effectivly converts the binary data into decimal
  // Im sure there must be a much more efficient way of doing this
  if(received[1] == 1){team[hitNo] = team[hitNo] + 4;}
  if(received[2] == 1){team[hitNo] = team[hitNo] + 2;}
  if(received[3] == 1){team[hitNo] = team[hitNo] + 1;} 

  if(received[4] == 1){player[hitNo] = player[hitNo] + 16;}
  if(received[5] == 1){player[hitNo] = player[hitNo] + 8;}
  if(received[6] == 1){player[hitNo] = player[hitNo] + 4;}
  if(received[7] == 1){player[hitNo] = player[hitNo] + 2;}
  if(received[8] == 1){player[hitNo] = player[hitNo] + 1;}
   
  if(received[9] == 1){weapon[hitNo] = weapon[hitNo] + 4;}
  if(received[10] == 1){weapon[hitNo] = weapon[hitNo] + 2;}
  if(received[11] == 1){weapon[hitNo] = weapon[hitNo] + 1;} 

  if(received[12] == 1){hp[hitNo] = hp[hitNo] + 16;}
  if(received[13] == 1){hp[hitNo] = hp[hitNo] + 8;}
  if(received[14] == 1){hp[hitNo] = hp[hitNo] + 4;}
  if(received[15] == 1){hp[hitNo] = hp[hitNo] + 2;}
  if(received[16] == 1){hp[hitNo] = hp[hitNo] + 1;}
   
  parity[hitNo] = received[17];


  Serial.print("Hit No: ");
  Serial.print(hitNo);
  Serial.print("  Player: ");
  Serial.print(player[hitNo]);
  Serial.print("  Team: ");
  Serial.print(team[hitNo]);
  Serial.print("  Weapon: ");
  Serial.print(weapon[hitNo]);
  Serial.print("  HP: ");
  Serial.print(hp[hitNo]);
  Serial.print("  Parity: ");
  Serial.println(parity[hitNo]);

  HC12.print("Shooter:");
  HC12.println(player[hitNo]);
  //HC12.write('\r');
  
 
 
  //This is probably where more code should be added to expand the game capabilities at the moment the code just checks that the received data was not a system message and deducts a life if it wasn't.
  if (player[hitNo] != 0){hit();}
  hitNo++ ;
}



void shoot() {
  if(FIRE == 1){ // Has the trigger been pressed?
    //Serial.println("FIRE 1");
    sendPulse(IRtransmitPin, 4); // Transmit Header pulse, send pulse subroutine deals with the details
    delayMicroseconds(IRpulse);
 
    for(int i = 0; i < 8; i++) { // Transmit Byte1
      if(byte1[i] == 1){
        sendPulse(IRtransmitPin, 1);
        //Serial.print("1 ");
      }
      //else{Serial.print("0 ");}
      sendPulse(IRtransmitPin, 1);
      delayMicroseconds(IRpulse);
    }

    for(int i = 0; i < 8; i++) { // Transmit Byte2
      if(byte2[i] == 1){
        sendPulse(IRtransmitPin, 1);
       // Serial.print("1 ");
      }
      //else{Serial.print("0 ");}
      sendPulse(IRtransmitPin, 1);
      delayMicroseconds(IRpulse);
    }
    
    if(myParity == 1){ // Parity
      sendPulse(IRtransmitPin, 1);
    }
    sendPulse(IRtransmitPin, 1);
    delayMicroseconds(IRpulse);
    //Serial.println("");
    //Serial.println("DONE 1");

    myDFPlayer.play(1);
    
  }


FIRE = 0;
ammo = ammo - 1;

magazine = magazine - 1;

if(magazine == 0) {
  myDFPlayer.play(6);
  magazine = 20;
  
}


}

void sendPulse(int pin, int length){ // importing variables like this allows for secondary fire modes etc.
// This void genertates the carrier frequency for the information to be transmitted
  int i = 0;
  int o = 0;
  while( i < length ){
    i++;
    while( o < IRpulses ){
      o++;
      digitalWrite(pin, HIGH);
      delayMicroseconds(IRt);
      digitalWrite(pin, LOW);
      delayMicroseconds(IRt);
    }
  }
}


void triggers() { // Checks to see if the triggers have been presses
  LTR = TR;       // Records previous state. Primary fire
  LT2R = T2R;     // Records previous state. Secondary fire
  int Delay = 200;
  int currentMillis;
  int previousMillis;
  
  currentMillis = millis();
  if(currentMillis - previousMillis >= Delay) {
  TR = digitalRead(triggerPin);      // Looks up current trigger button state
  previousMillis = currentMillis;
  }

  //T2R = digitalRead(trigger2Pin);    // Looks up current trigger button state
  // Code looks for changes in trigger state to give it a semi automatic shooting behaviour
  if(TR != LTR && TR == LOW){
    FIRE = 1;
  }
  if(T2R != LT2R && T2R == LOW){
    FIRE = 2;
  }
  if(TR == LOW && automatic == 1){
    FIRE = 1;
  }
  if(T2R == LOW && automatic2 == 1){
    FIRE = 2;
  }
  if(FIRE == 1 || FIRE == 2){
    if(ammo < 1){FIRE = 0; noAmmo();}
    if(life < 1){FIRE = 0; dead();}
    // Fire rate code to be added here  
  }
 
}


void configureGame() { // Where the game characteristics are stored, allows several game types to be recorded and you only have to change one variable (myGameID) to pick the game.
  if(myGameID == 0){
    myWeaponID = 1;
    maxAmmo = 30;
    ammo = 30;
    maxLife = 3;
    life = 3;
    myWeaponHP = 1;
  }
  if(myGameID == 1){
    myWeaponID = 1;
    maxAmmo = 100;
    ammo = 100;
    maxLife = 10;
    life = 10;
    myWeaponHP = 1;
  }
    
  if(myGameID == 2){
    myWeaponID = 1;
    maxAmmo = 1000;
    ammo = 100;
    maxLife = 100;
    life = 100;
    myWeaponHP = 1;
  }
}


void frequencyCalculations() { // Works out all the timings needed to give the correct carrier frequency for the IR signal
  IRt = (int) (500/IRfrequency);  
  IRpulses = (int) (IRpulse / (2*IRt));
  IRt = IRt - 4;
  // Why -4 I hear you cry. It allows for the time taken for commands to be executed.
  // More info: http://j44industries.blogspot.com/2009/09/arduino-frequency-generation.html#more

  Serial.print("Oscilation time period /2: ");
  Serial.println(IRt);
  Serial.print("Pulses: ");
  Serial.println(IRpulses);
  timeOut = IRpulse + 50; // Adding 50 to the expected pulse time gives a little margin for error on the pulse read time out value
}


void tagCode() { // Works out what the players tagger code (the code that is transmitted when they shoot) is
  byte1[0] = myTeamID >> 2 & B1;
  byte1[1] = myTeamID >> 1 & B1;
  byte1[2] = myTeamID >> 0 & B1;

  byte1[3] = myPlayerID >> 4 & B1;
  byte1[4] = myPlayerID >> 3 & B1;
  byte1[5] = myPlayerID >> 2 & B1;
  byte1[6] = myPlayerID >> 1 & B1;
  byte1[7] = myPlayerID >> 0 & B1;


  byte2[0] = myWeaponID >> 2 & B1;
  byte2[1] = myWeaponID >> 1 & B1;
  byte2[2] = myWeaponID >> 0 & B1;

  byte2[3] = myWeaponHP >> 4 & B1;
  byte2[4] = myWeaponHP >> 3 & B1;
  byte2[5] = myWeaponHP >> 2 & B1;
  byte2[6] = myWeaponHP >> 1 & B1;
  byte2[7] = myWeaponHP >> 0 & B1;

  myParity = 0;
  for (int i=0; i<8; i++) {
   if(byte1[i] == 1){myParity = myParity + 1;}
   if(byte2[i] == 1){myParity = myParity + 1;}
   myParity = myParity >> 0 & B1;
  }

  // Next few lines print the full tagger code.
  Serial.print("Byte1: ");
  Serial.print(byte1[0]);
  Serial.print(byte1[1]);
  Serial.print(byte1[2]);
  Serial.print(byte1[3]);
  Serial.print(byte1[4]);
  Serial.print(byte1[5]);
  Serial.print(byte1[6]);
  Serial.print(byte1[7]);
  Serial.println();

  Serial.print("Byte2: ");
  Serial.print(byte2[0]);
  Serial.print(byte2[1]);
  Serial.print(byte2[2]);
  Serial.print(byte2[3]);
  Serial.print(byte2[4]);
  Serial.print(byte2[5]);
  Serial.print(byte2[6]);
  Serial.print(byte2[7]);
  Serial.println();

  Serial.print("Parity: ");
  Serial.print(myParity);
  Serial.println();
}


void playTone(int tone, int duration) { // A sub routine for playing tones like the standard arduino melody example
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}


void dead() { // void determines what the tagger does when it is out of lives
  // Makes a few noises and flashes some lights
  for (int i = 1;i < 254;i++) {
    analogWrite(ammoPin, i);
    playTone((1000+9*i), 2);
  } 
  analogWrite(ammoPin, ((int) ammo));
  analogWrite(lifePin, ((int) life));
  Serial.println("DEAD");
 
  for (int i=0; i<10; i++) {
   analogWrite(ammoPin, 255);
   digitalWrite(hitPin,HIGH);
   delay (500);
   analogWrite(ammoPin, 0);
   digitalWrite(hitPin,LOW);
   delay (500);
  }
}


void noAmmo() { // Make some noise and flash some lights when out of ammo
  digitalWrite(hitPin,HIGH);
  playTone(500, 100);
  playTone(1000, 100);
  digitalWrite(hitPin,LOW);
  myDFPlayer.play(5);
}


void hit() { // Make some noise and flash some lights when you get shot
  digitalWrite(hitPin,HIGH);
  digitalWrite(HitVibration, HIGH);
  life = life - hp[hitNo];
  Serial.print("Life: ");
  Serial.println(life);
  playTone(500, 500);
  if(life <= 0){dead();}
  digitalWrite(hitPin,LOW);
  myDFPlayer.play(2);
  digitalWrite(HitVibration, LOW);
  lifeDisplay();
}

"shot" is not used in the sketch.

Describe "stuck" and not "stuck" and "starts working again."

Do you have a wiring diagram?

void hit() { // Make some noise and flash some lights when you get shot
  digitalWrite(hitPin, HIGH);
  digitalWrite(HitVibration, HIGH);
  life = life - hp[hitNo];
  Serial.print("Life: ");
  Serial.println(life);
  playTone(500, 500);
  if (life <= 0) {
    dead();
  }
  digitalWrite(hitPin, LOW);
  myDFPlayer.play(2);
  digitalWrite(HitVibration, LOW);
  lifeDisplay();
}

Try reducing the duration of playTone(); in this function.

"shot" is not used in the sketch.

That's correct. The function is called "Shoot".

Describe "stuck" and not "stuck" and "starts working again."

By stuck I mean that the DFPlayer does not play anything although the Shoot-trigger is pressed. (The red LED is dark). After a couple of seconds it usually recovers.

Do you have a wiring diagram?

No, but the DFPlayer is connected to Pin 7 and 8 of the Uno with a 1k resistor between the RX-line. And it's powered by 5 Volts from a clean voltage regulator.

Try reducing the duration of playTone(); in this function.

The "hit"-function has nothing to do with the "shoot" function. The hit-function is called when a player gets hit by another player and flashes LEDs, starts a vibration and plays a "hit"-sound.

It sounds like "blocking" is occurring as (you mention) buffering counts through while() loops in shoot(). I noticed the original Instructables does not use AltSoftSerial() or DFPlayerMini but a commenter had lots to say. In this code... to lessen processor load, have you tried commenting--out the Serial.print() lines in frequencyCalculations()?

It does in a race condition (and the serial link to the DFPlayerMini). Keep possibilities open, like you have never seen this code before... but because this is on your mind, I suspect a minor oversight (this happens when you stare at the same code for too long). As you know, the code compiles and "works..." but for a traffic jam on a number of shots, hits or fast shooter.

The original code seems to be linked in the code... maybe you can resurrect "the latest source code" from that? https://www.instructables.com/Duino-Tagger/

Or... show how you resurrected the code, with references, explaining changes, and maybe someone will catch the miss.

Hi y'all

I found out that the Player doesn't seem to like shots that come too close together. So I added a software debounce in the triggers-function. That seem to do the trick.
Thank you all for the support!

Here's where I added the debounce:

void triggers() {  // Checks to see if the triggers have been presses
  LTR = TR;        // Records previous state. Primary fire
  LT2R = T2R;      // Records previous state. Secondary fire

  unsigned long currentMillis;


  currentMillis = millis();
  if (currentMillis - previousMillis >= Delay) {

    TR = digitalRead(triggerPin);  // Looks up current trigger button state
    previousMillis = currentMillis;
  }

  //T2R = digitalRead(trigger2Pin);    // Looks up current trigger button state
  // Code looks for changes in trigger state to give it a semi automatic shooting behaviour
  if (TR != LTR && TR == LOW && needsReload == false) {
    FIRE = 1;
  }
  if (T2R != LT2R && T2R == LOW) {
    FIRE = 2;
  }
  if (TR == LOW && automatic == 1) {
    FIRE = 1;
  }
  if (T2R == LOW && automatic2 == 1) {
    FIRE = 2;
  }
  if (FIRE == 1 || FIRE == 2) {
    if (ammo < 1) {
      FIRE = 0;
      noAmmo();
    }
    if (life < 1) {
      FIRE = 0;
      dead();
    }
    // Fire rate code to be added here
  }
}

Is DFPlayerMini_Fast the same library you used originally in your working project? And with AltSoftSerial?

As you may have seen from the many threads in this forum, most users of that DFRobot module use the DFRobotDFPlayerMini.h library; with SoftwareSerial.h on a UNO.

EDIT, Thu 12 Dec 2024 1121: But I missed your last post reporting success - congrats. It might help other users of DFPlayerMini_Fast if you could share your working sketch.

No, orginally I used DFRobotDFPlayerMini.h. I switched to DFPlayeMini_Fast while trying to solve my problem with the player.
To find out whether that made a difference I'll switch back to the old library and see whether that makes a difference. I'll post the result soon here.

If that doesn’t work try reverting to version 1.05, which I use, having found obscure bugs in 1.06.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.