I need some help with a couple issues (which are related I think)
I'm very very new here, so let me know if I have breaches in etiquette please.
Part 1)
I have a data file I want to turn into variables to use.
The data file looks like
2
3
44
120
etc.
I use
oneLine = myFile.readStringUntil("\n");
and
nailNum = oneLine.toInt();
but when I dump that to see, I get just the first variable, and no others.
Can anyone tell me what I'm doing wrong. I've been reading like crazy to understand but, well, fail.
Part 2.
Related to that variable input technique, is that I am using strings instead of arrays.
I remember reading in a forum discussion that for the life of me I cannot find again, that using arrays is a better idea, and using atoi() instead of toInt().
I think the reasoning being it used a lot less memory, which is now an issue for me.
But I am struggling with that also.
Can someone elaborate on array virtues as well as how to use them to import an integer from a list in a text file on the SD card?
The posting would be handy as it also had the support code which I need, such as how to read an entire line from some flavor of file related read.
Thanks so much.
#include <SPI.h>
#include <SD.h>
File myFile;
String oneLine = String();
//int lineNum = 1;
int nailNum = 0; //Valid 0 - 240, larger or smaller causes while(1)
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
}
Serial.println("Initializing SD card");
if (!SD.begin(10)) {
Serial.println("initialization FAIL. Self destruct sequence in 10...9..");
while (1)
;
}
Serial.println("initialization WORKED. Amazing.");
// open the file for reading:
myFile = SD.open("test1.txt", FILE_READ); //so here is myFile, which is test1.txt
}
/* while(dataFile.available())
{
arrayOfChars[i] = dataFile.readStringUntil('\n'); // Read the charcters until you get a new string
i++;
}
*/
void loop() { // get each line, turn into a variable, print variable
if (myFile) { //make sure the file is there and can be opened
Serial.println("Now reading from file. Yay. ");
} else { //otherwise print fail and infinite loop
Serial.println("Can't seem to find file.....");
while (1)
;
}
myFile = SD.open("test1.txt", FILE_READ);
while (myFile.available()) { //read from myFile until the end
oneLine = myFile.readStringUntil("\n");
Serial.println("string found. Digesting line number ");
//Serial.println(lineNum); // just so I have some idea how it's progressing
//lineNum += 1;
nailNum = oneLine.toInt();
if ((nailNum >= 240) || (nailNum < 0)) { //nailNum has be be valid,
Serial.println("Number found out of range or invalid, or end of file");
while (1)
;
}
Serial.println(nailNum);
}
}
