Good morning,
I am quite new to arduino with a little bit background with 8051.
After some testing and playing around with some sensors I wanted to store and read files from an SD card.
writing works and reading also but I want after reading put it into an array.
shis sketch only reads some strings (i.e. schubiduba, oleoloe, etc...) from SD card and puts them together lookong for a CR or LF.
Then i want to write these into an string array and this does not work - the setup function loops.
Strange behavior for me:
when activating the line for array input ( myarray[linecount]=String(text); //writes the string into the array )
the setup() function loops ?!?!
I tried to hardcode the array for test purposes with
myarray[0]=String(text); //writes the string into the array
but this does not work - the 1st value isnot being displayed.
I also supposed memory Problems, seems not to be a mem problem, I have always about 850..900 left (think it will be bytes...)
I changed the arduino - same behavior.
I am afrid I made a silly mistake in my code ![]()
code:
// include the library code:
#include <LiquidCrystal.h>
#include <MemoryFree.h>
#include <SD.h>
int val=1;
String text = "";
int einzeichen;
int zeichencount=0;
int linecount =0;
String myarray[5];
// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);
File myFile;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("init SD card...");
delay(1000);
lcd.clear();
pinMode(10, OUTPUT); //Must be set due its an ethernet shield
if (!SD.begin(4)) {
lcd.setCursor(0, 0);
lcd.print("SD init failed!");
delay(1000);
lcd.clear();
return;
}
lcd.setCursor(0, 0);
lcd.print("SD init done.");
delay(1000);
lcd.clear();
// open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
lcd.setCursor(0, 0);
lcd.print("SD reading file ");
lcd.setCursor(0, 1);
lcd.print("test.txt");
delay(500);
lcd.clear();
linecount =0; //this variable counts the enter - or complete words
// read from the file until there's nothing else in it:
while (myFile.available()) {
einzeichen = myFile.read();
lcd.setCursor(0, 0);
lcd.print(einzeichen); // wirtes ASCIII Value to LCS
lcd.setCursor(0, 1);
lcd.print(char(einzeichen)); // writes char Value to LCS
delay(300);
lcd.print(" ");
lcd.setCursor(10, 0);
lcd.print(freeMemory()); // prints free Mem to upper right on LCD
if ((einzeichen == 10) || (einzeichen == 13)) { // if CR or LF
myarray[linecount]=String(text); //writes the string into the array
linecount++;
lcd.setCursor(0, 1);
lcd.print(text); // writes string to LCD
delay(1000);
text = "";
lcd.clear();
}
else {
text = text + char(einzeichen); // adds the new chat to the string
}
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
lcd.setCursor(0, 0);
lcd.print("error opening test.txt");
delay(1000);
lcd.clear();
}
}
Any suggestons/ Tips are welcome!
thx a lot, Wolfgang