My first time posting, and I just can't resolve an answer. Plz help/advise. Abbreviated file attached. I have no problem reading data from the sd card, adding, deleting files. Naming new files by directly coding them into a sketch is not desirable. I would like to enter new filenames from the serial monitor.
This works:
ln4 String lawn = "fort.txt";
ln 31test = SD.open(lawn, FILE_WRITE);
this does not:
ln27 lawn = Serial.readString();
ln31 test = SD.open(lawn, FILE_WRITE);
to save new file names to a SD card.
It seems rooted in String vs char formatting, but I can't seem to find a solution. Please explain why and how best to resolve it.
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4; // CS pin for SD Card Module
String lawn = "fort.txt"; //used temporarily for SD.open command
File test;
void setup() {
Serial.begin(9600); // temporary pc output for debug
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more
while (1);
}
Serial.println("card initialized.");
delay(2000); //delays serial monitor display status
Serial.println("Enter new filename(8 char maximum) above, press return. EX: lawnjob1.txt.");
while (Serial.available() == 0) {
//Waits for user input
}
//lawn = Serial.readString(); //captures serial monitor input for new file name
Serial.print("New file name is:");
Serial.println(lawn);
delay(500);// added in desperation. Not otherwise needed.
test = SD.open(lawn, FILE_WRITE);
delay(500);//added in desperation. Not otherwise needed.
test.close();
}
void loop() {
}