Pages: [1]   Go Down
Author Topic: Read int values from the SD card  (Read 3055 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear all,
I have a problem with a program that I am developing I am using and Arduino Ethernet Shield with the  incorporated SD.
In the program, I storage in the SD card some values of the an accelerometer, the number of these values are aleatory depending of the test duration. The values are properly storage in a file.
After of this storage, I need verify the values stored for do it I need open the file and read these values.

The opened file has the next values:
192.txt
705;546;437
426;402;385
307;291;303
257;245;254
232;225;230
221;218;221
216;214;216
215;214;415
221;222;223
224;225;226
228;229;229

I would like, storage in a first array ax the first colum, in the ay the second and in the az the last column of values. I need do it some matematics calculus with these arrays.

By the moment, I has the next code opening the file but I have really problems in storage the values as I comment previously.
Code:
File myFile = SD.open(nomfichero);
      if (myFile) {
        Serial.println(nomfichero);
       while (myFile.available()) {
          Serial.write(myFile.read());
         }// close the file:
          myFile.close();
          } else {
  // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
      }

I read in some foros that exist the function strocke and atof but I don't know how this functions operate.

I am very thankfully some ideas about my problems.

Thanks and regards.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This might get you closer:
Code:
void setup()
{
File myFile = SD.open("192.txt");
      if (myFile) {
        Serial.println("192.txt");
       while (myFile.available()) {
          int x = myFile.parseInt();
          int y = myFile.parseInt();
          int z = myFile.parseInt();
          Serial.print(x);
          Serial.print(";");
           Serial.print(y);
          Serial.print(";");
           Serial.print(z);
          Serial.println(";");
         }// close the file:
          myFile.close();
          } else {
  // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
      }
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, Johnwasser I didn't know the function parsein(), really interesting!.
I think that I complete the code:

In the setup ...
Code:
int i=0;
int ax[]={};
int ay[]={};
int az[]={};

and in the loop ....
Code:
  File myFile = SD.open(nomfichero);
      if (myFile) {
        Serial.println(nomfichero);
        while (myFile.available()) {
          //Serial.write(myFile.read());
          int x = myFile.parseInt();
          int y = myFile.parseInt();
          int z = myFile.parseInt();
          ax[i] = x;
          ay[i] = y;
          az[i] = z;
          i=i+1;
    }// close the file:
          myFile.close();
          i=0;
          for (i=0;i<10;i++){
          int value = az[i];
            Serial.println(value);
          }
      } else {
  // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
      }
     }

Why I obtain the same values in the ax, ay and az dates?
How I can see in the Serial the values of the all the arrays for evaluate if the arrays is generated OK?

Thank you very much another time.
Regards.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int ax[]={};
int ay[]={};
int az[]={};
The size if the arrays will then be defined by the number of initializers you provide. That number is pretty small, resulting in arrays that can't hold much. Just so you know, so you don't write out of bounds later.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem with the initialize the arrays values is that I don't know the size of these vectors, because depending of the test measurements you can have differents size vectors.
Is possible that I can do it a very great initialitation as:
Code:
for (i=0;1<100000;i++){
ax[i]=0;
ay[i]=0;
az[i]=0;
}
Is it possible or it's a crazy solution?
Thanks and regards.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is possible that I can do it a very great initialitation as:
Code:
for (i=0;1<100000;i++){
ax[i]=0; ay[i]=0; az[i]=0;}

Only if you declare the arrays before you initialize them:
Code:
int ax[100000];
int ay[100000];
int az[100000];
Of course to do that you need a processor with over 600,000 bytes of SRAM.  The 386P has 2,000 and the Mega (2560) has 8,000.  You can add 512K of extra SRAM to the Mega but that would still not be enough.

Are you sure you have to have every value in memory before you an do your calculations?

If so you may need to process your data on a more capable machine.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you  johnwasser, for your reply and help. About your comment...
Quote
Are you sure you have to have every value in memory before you an do your calculations?
Really is not needed, I would explain you better the application and may be you can help me in found a solution.
The values stored in the file, "1.txt" (for example) are the values of the acceleration is x, y and z coordenates, once I storage this values (that don't have a fixed size) I need open this file and compound the resultant acceleration as ar=(ax^2+ay^2+az^2)^1/2; and calculate the integral of these points as v=time/2 (ar[i-1]+ar) newton rapson methode, and evaluate the maximun of this value V. For me is interesting evaluate storage all the values v for with a PC and Excel obtain the graphics.
I am thinking, and I hope that you can evaluate this possibility, use another file 1_v.txt when i storage all values of v and with this don't initializate any array, is possible this?
I am thinking in a code as this (in this I writte some doubts that I have):
Code:
File myFile = SD.open(nomfichero);
      if (myFile) {
        Serial.println(nomfichero);
        while (myFile.available()) {
          //Serial.write(myFile.read());
          int x = myFile.parseInt();
          int y = myFile.parseInt();
          int z = myFile.parseInt();
          File myfile2 = SD.open(nomfichero2,FILE_WRITE); //I DON'T KNOW IF I CAN OPEN AT THE SAME TIME TWO DIFFERENTS FILES.
          ar = sqrt((sq((x*0.007-3.6)) + sq(0.007*y-3.6) + sq(0.007*z-3.6)));// MY FUNCTION TRANSFER SENSOR
         myfile2.println(ar);                                        // I DON'T KNOW IF IS POSSIBLE PRINT A FLOAT VALUE IN THE SD.     
         myfile2.close();
         
    }// close the file:
          myFile.close();
         if (myFile2) {
              // CALCULATE THE INTEGRAL ECUATION AND FOUND THE MAX VALUE.
          }
      } else {
  // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
      }
     }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you really storing acceleration data as ints?
Logged

Offline Offline
Sr. Member
****
Karma: 2
Posts: 272
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This might get you closer:
Code:
void setup()
{
File myFile = SD.open("192.txt");
      if (myFile) {
        Serial.println("192.txt");
       while (myFile.available()) {
          int x = myFile.parseInt();
          int y = myFile.parseInt();
          int z = myFile.parseInt();
          Serial.print(x);
          Serial.print(";");
           Serial.print(y);
          Serial.print(";");
           Serial.print(z);
          Serial.println(";");
         }// close the file:
          myFile.close();
          } else {
  // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
      }
}
This is a very good solution.
I have got a question, my problem is, that I not only store 3 Parameter in a line for example: 2;3;2, but only two parameters like this: 2;3
So in my SD-card the value are stored in this way:
2;3;2
2;3

But when I read this apout I have a problem becuase in teh second line i get a 0 for the third varialbe, and so I can't recognize wheteher I store a = or a empty parameter?
How I can avoid this?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/StreamParseInt

I think that after a parseInt() you will be able to read the next character to see if it is a ';'.

Code:
  while (myFile.available()) {
          int x,y,z;
          bool y_present, z_present;
          x = myFile.parseInt();
          y_present = false;
          z_present = false;
          if (myFile.read() == ';') {
               y_present = true;
               y = myFile.parseInt();
               if (myFile.read() == ';') {
                   z_present = true;
                   z = myFile.parseInt();
               }
           }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Sr. Member
****
Karma: 2
Posts: 272
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks a lot:D
Logged

Pages: [1]   Go Up
Jump to: