error while opening file on sd card

Hi everyone,

this is the example code that works

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
	// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

and this is my code, based on the example

myFile = SD.open("calibration.txt", FILE_WRITE);
  if (myFile) {
    for (i = 0; i <= 7; i++) {
      myFile.print(analogRead(lineSensor[i]));
      myFile.println("/");
    }
    myFile.close();
    Serial.println("Data written");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening calibration.txt");
  }

I always get "error opening calibration.txt", and I got no error messages while compiling.

Can someone help me please?
Thanks in advance

I always get "error opening calibration.txt"

I believe that the filename is limited to 8.3 format Try a shorter filename

thanks for your answer and sorry for the late response.

I tried a shorter filename and it worked. But now I have another problem. This is on the sd card:

121/
66/
107/
218/
298/
296/
293/
485/

and is read by the Arduino as this:

4950494713105454471310494855471310504956471310505756471310505754471310505751471310525653471310

Here is the code:

if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  myFile = SD.open("data/cal.txt", FILE_WRITE);
  if (myFile) {
    for (i = 0; i <= 7; i++) {
      myFile.print(analogRead(lineSensor[i]));
      myFile.println("/");
    }
    myFile.close();
    Serial.println("Data written");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening cal.txt");
  }
  myFile = SD.open("data/cal.txt");
  if (myFile) {
    while (myFile.available()) {
      Serial.print(myFile.read());
    }
    myFile.close();
  }

What am I doing wrong?

495049471310 (49 50 49 47 13 10) is 1 2 1 / Carriage Return Linefeed in ASCII

You are reading and printing the characters that represent the digits

So if I understand, it’s the same and I need to convert them? How can I do this?

How can I do this?

For the digits subtract 48 from what you get

ie, you get 49 so subtract 48 and you have converted it to a 1

However, I suspect that there may be a deeper problem. You saved 121 and you get back 1 then 2 then 1 using the "subtract 48" method but I suspect that you really want 121 in a variable. Is that right ?

Yes, I want them in an array.

h_anton:
Yes, I want them in an array.

Do you want the individual digits in an array or the integers in an array ?

I.e. I want 121 in array[0]; 66 in array[1]; ...

h_anton:
I.e. I want 121 in array[0]; 66 in array[1]; ...

OK

Set an array index variable to zero
Set a target int variable to zero
Read a character.
If it is between '0' and '9' subtract 48 (or '0') to get the actual digit
Multiply the target variable by 10 and add the new digit to the result
Keep going until you get a '/' and the number will be in the target variable
Put it in the array and increment the array index variable
Keep going to the end of the file

Try this with pencil and paper to see how and why it works

Note that the target array must have been previously declared with enough elements to hold all of the numbers.