DFPlayer mp3 player with TFT LCD 2.4 inch 240x320p

I am trying to make a mp3 player using DFPlayer Mini module and a TFT LCD 2.4inch 240x320p, I am trying to make 4 buttons on the LCD for the player ( play , pause, next and previous). After I press any button (that works) the second time I am trying to press a button to do a specific instruction, the screen freezes. I will post below the code that I am using:

#include <DFRobotDFPlayerMini.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <SoftwareSerial.h>
MCUFRIEND_kbv tft;   

#include <TouchScreen.h>
#include <FreeDefaultFonts.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000

const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x9595
const int TS_LEFT=876,TS_RT=139,TS_TOP=390,TS_BOT=769;

static const uint8_t PIN_MP3_TX = 19; // Connects to module's RX 
static const uint8_t PIN_MP3_RX = 18; // Connects to module's TX 

SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

Adafruit_GFX_Button play_button;
Adafruit_GFX_Button pause_button;
Adafruit_GFX_Button next_button;
Adafruit_GFX_Button prev_button;

DFRobotDFPlayerMini player;

int Cursor_X, Cursor_Y;
bool Touch_getXY(void)
{
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        Cursor_X = map(p.y, 130, 937, 0, 320);
        Cursor_Y = map(p.x, 920, 118, 0, 240);
    }
    return pressed;
}

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF


void setup(void)
{
    Serial.begin(9600);
    softwareSerial.begin(9600);
    if (player.begin(softwareSerial)) 
    {
    Serial.println("OK");
    }
    else {
    Serial.println("Connecting to DFPlayer Mini failed!");
    }
    player.volume(2);
    uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    Serial.println("Calibrate for your Touch Panel");
    if (ID == 0xD3D3) ID = 0x9486; // write-only shield
    tft.begin(ID);
    tft.setRotation(1);            //LANDSCAPE
    tft.fillScreen(BLACK);
    
  prev_button.initButton(&tft,50,220,60,40,WHITE, RED, WHITE, "PREV", 2); 
          prev_button.drawButton();
  play_button.initButton(&tft, 260,220,60,40,WHITE, RED, WHITE,"PLAY", 2); 
          play_button.drawButton();
  pause_button.initButton(&tft,120,220,60,40,WHITE, RED, WHITE, "PAUSE", 2); 
          pause_button.drawButton();  
  next_button.initButton(&tft,190,220,60,40,WHITE, RED, WHITE, "NEXT", 2); 
          next_button.drawButton();
}

void loop()
{
  int var=0;
  bool down = Touch_getXY();
    prev_button.press(down && prev_button.contains(Cursor_X, Cursor_Y));
    play_button.press(down && play_button.contains(Cursor_X, Cursor_Y));
    pause_button.press(down && pause_button.contains(Cursor_X, Cursor_Y));
    next_button.press(down && next_button.contains(Cursor_X, Cursor_Y));
    if (prev_button.justReleased())
        prev_button.drawButton();
    if (play_button.justReleased())
        play_button.drawButton(); 
    if (pause_button.justReleased())
        pause_button.drawButton();
    if (next_button.justReleased())
        next_button.drawButton();
  
    if (prev_button.justPressed())
      {
        var = 1 ;
      }
    else if (play_button.justPressed())
       {
        var = 2 ;
       }
    else if (pause_button.justPressed())
      {
       var = 3 ;
      }
       
    else if (next_button.justPressed())
      {  
       var = 4 ;
      }
      
   switch (var) 
   {
    case 1:
    {
      prev_button.drawButton(true);
      player.previous();
      if (prev_button.justReleased())
        prev_button.drawButton();
    }
      break;
    case 2:
    {
      play_button.drawButton(true);
      player.start();
      if (play_button.justReleased())
        play_button.drawButton();
    }
      break;
    case 3:
    {
      pause_button.drawButton(true);
      player.pause();
      if (pause_button.justReleased())
        pause_button.drawButton();
    }
      break;
    case 4:
    {
      next_button.drawButton(true);
      player.next();
      if (next_button.justReleased())
        next_button.drawButton();
    }
      break;
    default: 
    {
      Serial.println("default");
      if (prev_button.justReleased())
          prev_button.drawButton();
      if (play_button.justReleased())
          play_button.drawButton(); 
      if (pause_button.justReleased())
          pause_button.drawButton();
      if (next_button.justReleased())
          next_button.drawButton();
    }
   }
}

This might not be the answer, but there is no "break;" after "default".

The "freezing" sounds like a "nested command" that is using memory but not releasing memory after finishing.

I tried adding "break" after default and the same thing happens. Also I put some "Serial.print" for each cases and the first time it goes into a case it exits again to the main loop, but after I press a second button it gets stuck in that case.

Excellent. What did you see? You can only copy the visible part of the Serial Monitor, so - expand the Serial Monitor by grabbing the window bar and dragging... then copy the "serial prints" and paste them here using the < CODE > block button. Paste your most recent code, too.

Pardon this next question, but is this code produced by ChatGPT? I can see its fingerprints.

Chatgpt is useless at this kind of thing I didn't even bother trying .

OK
TFT ID = 0x9341
Calibrate for your Touch Panel
 default 
 default 
 default 
 default 
 default 
 default 
 default 
 Before start  after start  default 
 default 
 default 
 default 
 default 
 Before pause 

And this is the code as it is right now:

#include <DFRobotDFPlayerMini.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <SoftwareSerial.h>
MCUFRIEND_kbv tft;   

#include <TouchScreen.h>
#include <FreeDefaultFonts.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000

const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x9595
const int TS_LEFT=876,TS_RT=139,TS_TOP=390,TS_BOT=769;

static const uint8_t PIN_MP3_TX = 19; // Connects to module's RX 
static const uint8_t PIN_MP3_RX = 18; // Connects to module's TX 

SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

Adafruit_GFX_Button play_button;
Adafruit_GFX_Button pause_button;
Adafruit_GFX_Button next_button;
Adafruit_GFX_Button prev_button;

DFRobotDFPlayerMini player;

int Cursor_X, Cursor_Y;
bool Touch_getXY(void)
{
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        Cursor_X = map(p.y, 130, 937, 0, 320);
        Cursor_Y = map(p.x, 920, 118, 0, 240);
    }
    return pressed;
}

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF


void setup(void)
{
    Serial.begin(9600);
    softwareSerial.begin(9600);
    if (player.begin(softwareSerial)) 
    {
    Serial.println("Connecting to DFPlayer Mini failed!");
    }
    else {
    Serial.println("OK");
    }
    player.volume(2);
    uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    Serial.println("Calibrate for your Touch Panel");
    if (ID == 0xD3D3) ID = 0x9486; // write-only shield
    tft.begin(ID);
    tft.setRotation(1);            //LANDSCAPE
    tft.fillScreen(BLACK);
    
  prev_button.initButton(&tft,50,220,60,40,WHITE, RED, WHITE, "PREV", 2); 
          prev_button.drawButton();
  play_button.initButton(&tft, 260,220,60,40,WHITE, RED, WHITE,"PLAY", 2); 
          play_button.drawButton();
  pause_button.initButton(&tft,120,220,60,40,WHITE, RED, WHITE, "PAUSE", 2); 
          pause_button.drawButton();  
  next_button.initButton(&tft,190,220,60,40,WHITE, RED, WHITE, "NEXT", 2); 
          next_button.drawButton();
}

void loop()
{
  int var=0;
  bool down = Touch_getXY();
    prev_button.press(down && prev_button.contains(Cursor_X, Cursor_Y));
    play_button.press(down && play_button.contains(Cursor_X, Cursor_Y));
    pause_button.press(down && pause_button.contains(Cursor_X, Cursor_Y));
    next_button.press(down && next_button.contains(Cursor_X, Cursor_Y));
    if (prev_button.justReleased())
        prev_button.drawButton();
    if (play_button.justReleased())
        play_button.drawButton(); 
    if (pause_button.justReleased())
        pause_button.drawButton();
    if (next_button.justReleased())
        next_button.drawButton();
  
    if (prev_button.justPressed())
      {
        var = 1 ;
      }
    else if (play_button.justPressed())
       {
        var = 2 ;
       }
    else if (pause_button.justPressed())
      {
       var = 3 ;
      }
       
    else if (next_button.justPressed())
      {  
       var = 4 ;
      }
      
   switch (var) 
   {
    case 1:
    {
      prev_button.drawButton(true);
      Serial.print(" Before prev ");
      player.previous();
      Serial.print(" after prev ");
      if (prev_button.justReleased())
        prev_button.drawButton();
    }
      break;
    case 2:
    {
      play_button.drawButton(true);
      Serial.print(" Before start ");
      player.start();
      Serial.print(" after start ");
      if (play_button.justReleased())
        play_button.drawButton();
    }
      break;
    case 3:
    {
      pause_button.drawButton(true);
      Serial.print(" Before pause ");
      player.pause();
      Serial.print(" after pause ");
      if (pause_button.justReleased())
        pause_button.drawButton();
    }
      break;
    case 4:
    {
      next_button.drawButton(true);
      player.next();
      if (next_button.justReleased())
        next_button.drawButton();
    }
   
      break;
    default: 
    { delay(1000);
      Serial.println(" default ");
      if (prev_button.justReleased())
          prev_button.drawButton();
      if (play_button.justReleased())
          play_button.drawButton(); 
      if (pause_button.justReleased())
          pause_button.drawButton();
      if (next_button.justReleased())
          next_button.drawButton();
          break;
          
    }
    
   }
}

I agree. Sorry for asking. Nice code formatting.

Looks like you found the pause button... do you have an "unpause" or "play" button?

In the code, "pause" is "case: 3"... so this would need to see why every second button press causes a "case:3"

the "player.start" is the function that resumes playing , or "play_button". The problem as you can see is that after "Before pause" in the serial monitor, if I try to press another button it doesn't work. The button that I try to press afterwards just remains pressed and nothing happens.

This is probably monitoring a pin or a variable (isPlaying?) and that pin/variable state is not letting the library exit the function back to your program... (I have no answer at the moment)

A thought... could your "pause/play" button wire not be wired correctly?

the buttons are not physical, they are displayed on the tft.

thanks for trying, I appreciate it.

How does the DFPlayerMini get commands? Serial? Is the command being sent?

You can verify your hardware in I/O mode here...

Why redrawing the buttons three times in the loop?

Why do you need a default case with a delay? Isn't it better to return to the beginning of loop as soon as possible?

i put the delay so i can follow what is in the serial monitor, otherwise it would have just fill it with "default". Even if I delete the don't redraw the buttons the still bug still persists.

Yeah the command is being sent, otherwise it wouldn't work the first time I press a button. I checked it out 100 times.

Why do you need the "default" case at all?

to try and debug the problem

Have you used the suggestions and made progress?

Which dev board is used here ?

I tried, but without any luck.

Arduino Mega 2560 Rev3