Help. Have no idea of what problem is.

Hi,

Yesterday i just wrote a simple code based on ReadWrite example on SD library and everything was working fine. i run the program more than 50 times and it worked

Today when i try to run the program again this simple doesnt work and i just get trash on serial monitor.

I already formated the SD Card and wrote the same .txt files than i need, i already check all the connections and everything is fine.

the best thing is that i run the ReadWrite example and i get it correctly. im really mind blowing with this. help pls.

in attachments a print of serie monitor.

Im using the standard SD library. The example is working and i can’t find the problem.

Here is my code:

#include <SPI.h>
#include <SD.h>

File myFile;
File myFile2;

char settings[2][10];

int y = 0;
char val[20];
int i = 0;

int player_id = 1;
int n_game = 3;
int n_button = 1;
int n_plays = 10;
unsigned long ltime = 100;

void setup(){
  
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }
  Serial.print("Initializing SD card...");                                                         

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // re-open the file for reading:
  myFile = SD.open("settings.txt", FILE_READ);
  if (myFile) {
    Serial.println("settings.txt:");

    for(y = 0; y < 2; y++){
      i=0;
      while(val[i-1]!='\r'){
      val[i]=myFile.read();
      i++;
      }
      strcpy(settings[y], val);
    }
    
    myFile.close();
  } else {
    Serial.println("error opening settings.txt");
  }

  for(y = 0; y < 4; y++){
    Serial.print(settings[y]);  
    delay(500);
    }

  myFile2 = SD.open("history.txt", FILE_WRITE);
  if (myFile2) {
    for(y = 0; y < n_plays; y++)
    {
      if(y == 0){
        myFile2.print(player_id);
        myFile2.print("-");
        myFile2.print(n_game);
        myFile2.print(" ");
        Serial.print(player_id);
        Serial.print("-");
        Serial.print(n_game);
        Serial.print(" ");
      }
      else if((y > 0) && (y < (n_plays-1))){
        myFile2.print(n_button);
        myFile2.print(".");
        myFile2.print(ltime);
        myFile2.print(" ");
        Serial.print(n_button);
        Serial.print(".");
        Serial.print(ltime);
        Serial.print(" ");
      }
      else {
        myFile2.print(n_button);
        myFile2.print(".");
        myFile2.println(ltime);
        Serial.print(n_button);
        Serial.print(".");
        Serial.println(ltime);
      }

      ltime = ltime*2;
    }

    Serial.println("History Saved!");
    
    myFile2.close();
  } else {
    Serial.println("error opening history.txt");
  }
}


void loop()
{
  // nothing happens after setup
}

Instead of open the settings.txt file i open the test.txt created on the example and it works.

why can’t i open the settings.txt file that simply contains “5 10”?

gezuz:
Hi,

Yesterday i just wrote a simple code based on ReadWrite example on SD library and everything was working fine. i run the program more than 50 times and it worked

Today when i try to run the program again this simple doesnt work and i just get trash on serial monitor.

I already formated the SD Card and wrote the same .txt files than i need, i already check all the connections and everything is fine.

the best thing is that i run the ReadWrite example and i get it correctly. im really mind blowing with this. help pls.

in attachments a print of serie monitor.

Im using the standard SD library. The example is working and i can’t find the problem.

Here is my code:

#include <SPI.h>

#include <SD.h>

File myFile;
File myFile2;

char settings[2][10];

int y = 0;
char val[20];
int i = 0;

int player_id = 1;
int n_game = 3;
int n_button = 1;
int n_plays = 10;
unsigned long ltime = 100;

void setup(){
 
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  Serial.print(“Initializing SD card…”);

if (!SD.begin(4)) {
    Serial.println(“initialization failed!”);
    return;
  }
  Serial.println(“initialization done.”);

// re-open the file for reading:
  myFile = SD.open(“settings.txt”, FILE_READ);
  if (myFile) {
    Serial.println(“settings.txt:”);

for(y = 0; y < 2; y++){
      i=0;
      while(val[i-1]!=’\r’){
      val[i]=myFile.read();
      i++;
      }
      strcpy(settings[y], val);
    }
   
    myFile.close();
  } else {
    Serial.println(“error opening settings.txt”);
  }

for(y = 0; y < 4; y++){
    Serial.print(settings[y]); 
    delay(500);
    }

myFile2 = SD.open(“history.txt”, FILE_WRITE);
  if (myFile2) {
    for(y = 0; y < n_plays; y++)
    {
      if(y == 0){
        myFile2.print(player_id);
        myFile2.print("-");
        myFile2.print(n_game);
        myFile2.print(" “);
        Serial.print(player_id);
        Serial.print(”-");
        Serial.print(n_game);
        Serial.print(" “);
      }
      else if((y > 0) && (y < (n_plays-1))){
        myFile2.print(n_button);
        myFile2.print(”.");
        myFile2.print(ltime);
        myFile2.print(" “);
        Serial.print(n_button);
        Serial.print(”.");
        Serial.print(ltime);
        Serial.print(" “);
      }
      else {
        myFile2.print(n_button);
        myFile2.print(”.");
        myFile2.println(ltime);
        Serial.print(n_button);
        Serial.print(".");
        Serial.println(ltime);
      }

ltime = ltime*2;
    }

Serial.println(“History Saved!”);
   
    myFile2.close();
  } else {
    Serial.println(“error opening history.txt”);
  }
}

void loop()
{
  // nothing happens after setup
}

well, i just left a empty line at the end of the .txt and its working...

    for(y = 0; y < 2; y++){
      i=0;
      while(val[i-1]!='\r'){
      val[i]=myFile.read();
      i++;
      }

You compare the value in val[i - 1] before you write to val[ i ] and increment i. What IS in val[i - 1] at that point?

After reading a record, you never re-initialize val.