0

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();
}

THE PINOUT:

The MC+SD Adapter

  • i use a sd adapter, connected via wires to the pins of the ESP32, also added the pinout – Xenoshell Jan 11 at 17:33
  • Its just a SD adapter you can also plug into a PC or Laptop. I soldered some pins to it and connected those with the pins of the ESP32 – Xenoshell Jan 11 at 17:40
  • I also edited the begin function of the display into the main post – Xenoshell Jan 11 at 17:45
  • ok. how is SPI for the display initialized? I guess it uses the default SPI object. so everything looks ok. – Juraj Jan 11 at 17:47
  • I asked my team member and she said the display uses the default SPI – Xenoshell Jan 11 at 18:18
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.