Reading from an SD card to a string one line a time. Not cycling/looping

Im certain this is in the reference section of the Arduino website, and probably in the forum too, but for the life of me i cant find it! maybe my search box skills are not up to it. 

I want to read an SD card text file one line at a time. I am using the arduino to process a Gcode file (saved as a .TXT rather than .NGC)

I think the best approach is to be buffer one line at a time, using /n. (using a char string perhaps?) I can then discard the lines that dont start with G"ii" and make an action based on the ones that do.

I figure i would then go one line at a time in the loop, but would make sure it would progress to the next time each time the loop cycles, rather than repeating line one.....

would someone for who this is easy possibly outlay what i would need? Read from SD card then buffer to a string??

C/C++ has a command called readline()

I did a search for arduino readline. The link below is the first hit. One of the posters includes code.

http://forum.arduino.cc/index.php?topic=100429.0

From the code posted at that link, you should be able to get going on your own program.

Thanks Birdmun."readline()"

Thanks Birdmun.

“readline()” is new to me, ill have a poke at this.

the more you know!

 

Nope. Still lost :(Such a

Nope. Still lost :frowning:

Such a simple task i think i must be misunderstanding on a really basic level

Am i right in thinking that i need to take the Data, as a stream of charachters, and save it in a predefined string. I can then access that string to access the data on the line. Using the SD card as ROM, and the string as RAM…?

Thanks Oddbot, I will have a

Thanks Oddbot, I will have a look.

 

Ive got here so far:

 

void loop() {

 

  while (myFile.available()) {

 

    char inputChar = myFile.read(); // Gets one byte from serial buffer

 

    if (inputChar == ‘/n’)

 

    {

      Serial.println(inputString);

      Serial.println(“test”);

      delay (1000);

    }

 

    else

 

    {

      inputString[index] = inputChar; // Store it

      index++; // Increment where to write next

    }

  }

}

 

It does nothing, im sure there is something stupid in there.

One of the other links I hit on my “arduino readline” search was

http://linux-utils.blogspot.com/2010/10/arduino-serial-read-line.html

The above code is pretty much the same as the code in the first link. The above code is just more generic.

That helped birdmun and

That helped birdmun and Oddbot, thanks.

So now i have it pulling out the first line (I have had to use the letterG to terminate, no idea why but i will sort that once this first issue is sorted. The program doesnt cycle though. it only does one iteration and then stops.

can you see why? Do i need to re open the file each time?

(Updated to include ALL the code)

 

//LIBRARIES
#include <SD.h>
#include <AFMotor.h>

//CONNECTIONS
File myFile; // instance of a file
const int chipSelect = 15; // adafruit SD breakout, wired 15 - 18. must use modified SD library to allow for reassignment of pins.
AF_Stepper motorL(200, 1); // Left Motor, M1 & M2
AF_Stepper motorR(200, 2); // Right Motor, M3 & M4 // Forward is Up on both motors.
const int button = 13; //button holds the sketch in setup, until pressed. This stops the motors from moving under USB power while uploading.
const int led = 14;
const int relay = 2;

// WORKING VALUES
char inputString [100];
char inputChar;
int stringIndex = 0; // String stringIndexing int;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // setup
  pinMode (led, OUTPUT);
  pinMode (button, INPUT);
  pinMode (relay, OUTPUT);
  motorL.setSpeed(20); // 10 rpm
  motorR.setSpeed(20); // 10 rpm

  Serial.print(“Motors ready, Initializing SD card…”);
  // make sure that the default chip select pin is set to
  // output, even if you don’t use it:
  pinMode(SS, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(15,16,17,18)) {
    Serial.println(“Card failed, or not present”);
    // don’t do anything more:
    while (1) ;
  }
  Serial.println(“card initialized.”);

  // Open up the file we’re going to log to!
  myFile = SD.open(“HELLO.txt”);
  if (! myFile) {
    Serial.println(“error opening datalog.txt”);
    // Wait forever since we cant write data
    while (1) ;
  }
  digitalWrite (led, HIGH);
  Serial.println(“Waiting…”);
  //hold
  while (digitalRead (button) == HIGH){
    // stops script. Its waiting for a button press (LOW on “button”)
  }
  Serial.println("…Running");
}
void loop()
{
  inputChar = myFile.read(); // Gets one byte from serial buffer
  if (inputChar != ‘G’) // define breaking char here (\n isnt working for some reason, i will follow this up later)
  {
    inputString[stringIndex] = inputChar; // Store it
    stringIndex++; // Increment where to write next
  }
  else
  {
    Serial.print("test: "); // shows that the program is cycling, for debugging only
    Serial.println(inputString);
    delay (1000);
    stringIndex = 0; // clear the value for the next cycle
  }
}

The only thing I can think to suggest would be

after

void loop() {
  inputChar = myFile.read();

add
  while(inputChar != EOF) {
    inputChar = myFile.read();
    … rest of code
  }
     

It wasnt working because the

It wasnt working because the string wasnt long enough Ive upped it to 500 and it works ok. It is now cycling the first line only, but iits progress! I shall return if i get stuck again. Thanks guys.

Sorted. Birdmun, i used "10"

Sorted. Birdmun, i used “10” (DEC for ascii Line feed) but your suggestion of EOF put me on the right track. once again, thanks guys! :slight_smile: