[Nextion] Listing, selecting, and managing SD files.

Hi everyone. First of all, happy new year and a better 2021 for all of us :slight_smile:

So I am working on a project that uses a SD shield attached to an arduino mega 2560, and a Nextion 5" basic display. What I need to do is list all the files in a specific folder, show them in Nextion, and be able to select two of them to either switch their names with each other, or delete both. I am using Greiman's SdFat library to manage the SD files, and Seithan's EasyNextion library to manage the Nextion display.

So far I was able to get the file names and store them in a char array, then rename each two-state button in Nextion with a file name. When one of these buttons is pressed, it triggers a function called selectFilesCascata() that is supposed to copy that file name to another array called SDCascataSelected[].
The problem I'm having now is to unselect a file, or remove its name from SDCascataSelected[] when a two-button state is unselect in Nextion.

Below are the functions I use to list and select the files trough Nextion. Probably not the best way to do it, but so far it works (almost. some times selectFilesCascata() gives unexpected results).

void listFilesCascata(){
    uint8_t n = 0;
    dir.open("/cascata");
    while (file.openNext(&dir, O_RDONLY)) {
    file.getSFN(SDfilesCascata[n]);
    myNex.writeStr(CascataButtonsNex[n],SDfilesCascata[n]);
    file.close();
    n++;
  }
    dir.close();
}
void selectFilesCascata()
{  
  index=myNex.readNumber("ind.val"); // this is the number corresponding to the last two-state button pressed in 
                                     // Nextion. First button is index 0, second button is 1, and so on...
  int i=0;
  while(i<50)
  {
    if (SDCascataSelected[i][0]==0)
    {
      index=myNex.readNumber("ind.val");
      if (index == 777777)
      {
        index=myNex.readNumber("ind.val");
      }
      memcpy(SDCascataSelected[i],SDfilesCascata[index],sizeof(SDCascataSelected[i]));
      break;
    }
    else {
    i++;
    }
  }
    myNex.writeStr("t4.txt",SDCascataSelected[0]);
    myNex.writeStr("t5.txt",SDCascataSelected[1]);  
}

Now this is the function I can't get it working. I am calling it when a two-state button is unselected in Nextion but nothing happens.

void unselectFilesCascata()
{
  index=myNex.readNumber("ind.val");
  int i=0;
  while (i<50)
  {
    if (SDCascataSelected[i]==SDfilesCascata[index])
    {
      index=myNex.readNumber("ind.val");
      if (index == 777777)
      {
      index=myNex.readNumber("ind.val");
      }
    memset(SDCascataSelected[i],0,sizeof(SDCascataSelected[i]));
    break;
    }
    else {
      i++;
    }
  }

    myNex.writeStr("t4.txt",SDCascataSelected[0]);
    myNex.writeStr("t5.txt",SDCascataSelected[1]);  
}

I was wondering if maybe someone here can help me with this? Or maybe even point me out a better way to do the whole listing, selecting and managing trough Nextion and arduino SD shield. I am attaching my Nextion HMI file.

I also would like to thank in advance for anyone reading this, and apologize for my bad english.

cascatas_IHM_5pol.zip (112 KB)