When using an array setup() loops

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 :frowning:

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

If setup appears to be looping, then you're probably resetting for some reason.
Please also note that "string" is not the same a "String" - we don't like the latter.
Please sort out the indentation of your code.

        myarray[0]=String(text); //writes the string into the array

myarray[0] is just a single char it can't take a whole string.

Mark

myarray[0] is just a single char it can't take a whole string

String myarray[5];

?

Thanks a lot for your answers. I tested a little further and now I am a little bit confused....

If my array canonly contain only 1 character, why does this simple exapmle work?

With this I write a String + convertted Number to the Array and the main loop does Display:

Oleole1
Oleole2
.....
.....

seems that I am a Character/String magician :wink:

thx a lot, Wolfgang

// include the library code:
#include <LiquidCrystal.h>
#include <MemoryFree.h>
#include <SD.h>

int einzeichen;
int linecount =0;
String myarray[5];
String text;

// 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);


void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);

  lcd.setCursor(0,0);
  lcd.print("init...");
  delay(1000);


  for (linecount=0; linecount<5; linecount++){
    text = String(linecount);
    text = "Oleole" + text;
    myarray[linecount] = text;

  }
}

void loop()
{
  for (linecount=0; linecount<5; linecount++){
    lcd.setCursor(0,0);
    lcd.print(myarray[linecount]);
    delay(500);
    lcd.clear();

  }

}

If my array canonly contain only 1 character, why does this simple exapmle work?

We don't know why holmes4 thinks that.
We await enlightenment.

[...]
String text = "";
[...]
String myarray[5];

[...]
    while (myFile.available()) {
         
        einzeichen = myFile.read();

        [...]		
        myarray[linecount]=String(text); //writes the string into the array
        linecount++;
        [...]
    }

[...]

So what happens after the 5th line of text is read from the file? I don't know if that's your problem or not in this case but nothing good ever comes from putting more data someplace you don't have enough space for.

Hope this helps,

Brad
KF7FER

I did not see that the op had used an array of strings.

Mark

GOT IT !!

Thanks Brad, that was the Problem.
The main problem :slight_smile:

When processing the folow of characters i checked for CR OR LF - when one of both appeared a new array entry was made, so it made String - LF - String - LF - String.........

The reset was made because I defined to few Array index values..

I reviewed the code, made some changes and corrected indetation :slight_smile:

thx a lot guys

PS: to who it could be for interest, heres the working code:

/*
This Array- Test Sketch reads a file with strings in the Setup() Section and 
 writes them to an array.
 In the Main Section the Content of the array is being displayed on the LCD.
 */

#include <LiquidCrystal.h>
#include <MemoryFree.h> //this can be commented out - just for freeMemory display- purposes
//when commented out please also comment out all lcd.print(freeMemory()); entries!
#include <SD.h>

int val=0;
String text = "";
int einzeichen;
int zeichencount=0;
int linecount =0;
int maxrows = 5;                  // must be bigger or equal than the max readble rows on SD text file
String myarray[5];                // same as above - maxrows and size of array must be equal!

// initialize the library with the numbers of the interface pins
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(100);
      //      lcd.print("                ");

      lcd.setCursor(0, 0);
      lcd.print("write ARR");
      lcd.setCursor(12, 0);
      lcd.print(freeMemory()); // prints free Mem to upper right on LCD

      if (einzeichen == 13) { // if einzeichen is CR, string is complete => Array

        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 {

        if (einzeichen == 10) {
          // do nothing, einzeichen is the LF
        }

        else{
          text = text + char(einzeichen); // adds the new char (read from SD) 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();
  }

}

void loop()
{
  lcd.clear();

  for (val=0; val<maxrows; val++) {
    lcd.setCursor(0, 0);
    lcd.print("MAIN");
    lcd.setCursor(12, 0);
    lcd.print(freeMemory());
    lcd.setCursor(0, 1);
    lcd.print(myarray[val]);
    delay(1000);
    lcd.clear();
  }

}