Need help making a vertically scrolling menu to read items from an SD card

Hey there, I'm wondering if anybody can help me with my 'Arduino Mega' + '20x4 LCD screen'+ 'SD Datalogging Shield' project.

What I'd like to do, is this:

  • Be able to read and display .txt files from my SD card onto my LCD screen (DONE).
  • With a cursor of some sort, scroll vertically through the (potentially many) files until I find the one I'm looking for (I'm stuck here)
  • When i click on whichever file (with a pushbutton), it will set that file name to a 'String', and begin running my main loop (I think this would be the best way - open to suggestions).

The main loop (which I already have completely written), will open a .txt file (the name will be set by the previous 'String'). It reads two numbers (ex: 128, 44) from the text file, and then runs a DC motor back and forth to the parameters set by that open .txt file

I will use the LCDMenuLibrary to close that file and exit out of the function, then go back to the list of files on the SD Card and load another file with different settings.

As mentioned, I've been able to display the files from my SD card onto my LCD screen, but now I'm unsure of how to scroll through the files so that i can actually see/load any of them.

Is it possible to create a vertically scrolling cursor to do this(without using the LCDMenuLibrary, which I'm already using in another part of the main code)?

Here's the current rendition of the 'SD Interface' code:

/*
   LCD_Show_SD_Card.ino
   Written 5/10/19 by Zach Rockafellow
   A 'work-in-progress' towards creating an Arduino-based SD interface
*/

#include <SD.h>
#include <Adafruit_LiquidCrystal.h>

Adafruit_LiquidCrystal lcd (23, 25, 22, 5, 4, 3, 2);

File root;
unsigned int z = 0; // used to increase the current row on the LCD screen
unsigned int button = A5; // pushbutton, connected to Rotary Knob

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  lcd.begin(20, 4);
  pinMode(53, OUTPUT);
  pinMode(button, INPUT_PULLUP);

  Serial.print("Initializing SD card...");
  lcd.setCursor(0, 0);
  lcd.print("Card Initializing...");

  if (!SD.begin(53))
  {
    Serial.println("initialization failed!");
    lcd.setCursor(0, 1);
    lcd.print("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  lcd.setCursor(0, 1);
  lcd.print("initialization done.");
  delay(2000);
  Serial.println("hit any key then enter to run the list");
  lcd.clear();
}

void loop() {

  lcd.setCursor(0, 0);
  lcd.print("Press 'Pushbutton'");
  lcd.setCursor(0, 1);
  lcd.print("to run the list");
  if (digitalRead(button) == 0) {
    lcd.clear();
    readSDCard();
  }
}
void readSDCard()
{
  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");
  lcd.setCursor(14, 3);
  lcd.print("done!");
  delay(5000);
  lcd.clear();

}

void printDirectory(File dir, int numTabs)
{
  while (true)
  {

    File entry =  dir.openNextFile();
    if (! entry)
    {
      // no more files
      //Serial.println("**nomorefiles**");
      break;
    }

    for (uint8_t i = 0; i < numTabs; i++)
    {
      Serial.print('\t');
    }
    Serial.print(entry.name());

    lcd.setCursor(0, z++);

    lcd.print(entry.name());
    delay(250);

    // Take this out to make the files list one after another
    if (z > 3) {
      //lcd.clear(); // added to clear screen every 4 files
      z = 0;
    }

    // this is all for the Serial Monitor ONLY (not LCD Screen - yet)
    if (entry.isDirectory())
    {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    }
    else
    {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
  }
}

Thanks so much for any help! Very much appreciated

Still working on this.

I've found a useful code that I'm going to hopefully alter a bit to work. Still hoping someone can provide some suggestions on creating a vertically scrolling cursor that will select/open a file on my SD card.

Here's the link: