user47558: i work on a group project with an ESP32. We have a lcd display and a sd card connected via the two SPI ports of the ESP32.
The card gets initialized with no problem using this code:    
 bool EKG_recording::initialisation_sd() {

  SPIClass * hspi = new SPIClass(HSPI);

  if (!SD.begin(CS_Pin, *hspi))
  {
    Serial.println("No valid SD card!");
    return 0;
  }

  else if (SD.begin(CS_Pin, *hspi))
  {
    Serial.println("SD card initialized!");
    return 1;
  }
  else return 0;
}

One function i have a problem with is used to count every file on the SD card, simple enough right?
unsigned short EKG_recording::count_files_from_SD(File dir) {
  while (true)
  {
    File entry = dir.openNextFile();
    if (!entry)
    {
      // Serial.println("Anzahl der Daten");
      return counter;
    }
    counter++;
    entry.close();
  }
}

It once worked without a problem, then we integrated the two SPI ports and now i cant figure out why its not working. 
This part:
File entry = dir.openNextFile();
returns false on the first call even though i am 100% sure there are 3 files on the SD card. 
I call the function like this :
File root = SD.open("/");
EKG_recording_1.count_files_from_SD(root);

But why? Any help is very appreciated. 
This is the begin function of the display. Since i didnt write it, i know next to nothing about it.
    void EKG_display_control::begin()
{
  // Backgroundlight off
  pinMode(TFT_LED, OUTPUT);
  disable_display_backlight();

  // Initialise Display
  tft.begin(40000000);
  touch.begin();
  tft.setRotation(ROTATION);

  layout_start_screen();
}




user47558: Ok i found the solution. It was not too complicated. I just assumed the problem was in the code. This works well enough (dont ask me about the counter--)
  unsigned short EKG_recording::count_files_from_SD() {

  root = SD.open("/");
  root.rewindDirectory(); //Sets to beginning of directory


  while (true)
  {
    File entry = root.openNextFile();

    if(!entry){
      Serial.println("Anzahl der Daten");
      counter--;
      return counter;
    }
    counter++;
    entry.close();
  }
}

It was just because i used SD.open() always inside the class-functions, so every file root was bound to the class.

47558: <p>i work on a group project with an ESP32. We have a lcd display and a sd card connected via the two SPI ports of the ESP32.</p>

<p>The card gets initialized with no problem using this code:    </p>

<pre><code> bool EKG_recording::initialisation_sd() {

  SPIClass * hspi = new SPIClass(HSPI);

  if (!SD.begin(CS_Pin, *hspi))
  {
    Serial.println("No valid SD card!");
    return 0;
  }

  else if (SD.begin(CS_Pin, *hspi))
  {
    Serial.println("SD card initialized!");
    return 1;
  }
  else return 0;
}
</code></pre>

<p>One function i have a problem with is used to count every file on the SD card, simple enough right?</p>

<pre><code>unsigned short EKG_recording::count_files_from_SD(File dir) {
  while (true)
  {
    File entry = dir.openNextFile();
    if (!entry)
    {
      // Serial.println("Anzahl der Daten");
      return counter;
    }
    counter++;
    entry.close();
  }
}
</code></pre>

<p>It once worked without a problem, then we integrated the two SPI ports and now i cant figure out why its not working. 
This part:
<code>File entry = dir.openNextFile();</code>
returns false on the first call even though i am 100% sure there are 3 files on the SD card. </p>

<p>I call the function like this :</p>

<p><code>File root = SD.open("/");
EKG_recording_1.count_files_from_SD(root);
</code></p>

<p>But why? Any help is very appreciated. </p>

<p>This is the begin function of the display. Since i didnt write it, i know next to nothing about it.</p>

<pre><code>    void EKG_display_control::begin()
{
  // Backgroundlight off
  pinMode(TFT_LED, OUTPUT);
  disable_display_backlight();

  // Initialise Display
  tft.begin(40000000);
  touch.begin();
  tft.setRotation(ROTATION);

  layout_start_screen();
}
</code></pre>

<p><a href="https://i.stack.imgur.com/wV7S0.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wV7S0.jpg" alt="THE PINOUT:"></a></p>

<p><a href="https://i.stack.imgur.com/urotx.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/urotx.jpg" alt="The MC+SD Adapter"></a></p>
 47558: <p>Ok i found the solution. It was not too complicated. I just assumed the problem was in the code. This works well enough (dont ask me about the counter--)</p>

<pre><code>  unsigned short EKG_recording::count_files_from_SD() {

  root = SD.open("/");
  root.rewindDirectory(); //Sets to beginning of directory


  while (true)
  {
    File entry = root.openNextFile();

    if(!entry){
      Serial.println("Anzahl der Daten");
      counter--;
      return counter;
    }
    counter++;
    entry.close();
  }
}
</code></pre>

<p>It was just because i used SD.open() always inside the class-functions, so every file root was bound to the class.</p>
