SD.open() anomally; String vs serial monitor input

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

What have you got the Line ending set to in the Serial monitor ?
Is the line ending being included in the String when you read it ?

Hi,
Serial monitor is set to "CR". If I understand 2nd Q correctly, there are no modifiers coded in the sketch, ie Saves serial monitor input to Serial lawn using Serial.readString() and no other modifiers.
I'm "hearing" that I may need to consider looking at serial monitor settings, ie CR vsCR/LF etc.

If serial monitor is used for SD file name inputs as above, setting the serial monitor to "no line ending" worked fine.... however in my case, it compiled ok, otherwise printed to the monitor ok, but refused to update the actual SD. When sketch was reduced in size/re-written it worked ok. thx for pointing me in the right direction. Never expected monitor issue.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.