Go Down

Topic: SD Card open function changes previous variable (Read 388 times) previous topic - next topic

nathan_ngu

Nov 25, 2018, 05:02 am Last Edit: Nov 25, 2018, 09:43 pm by nathan_ngu
Whenever I open a file to edit using my SD card writer, it changes the answer of a previous variable.

Code: [Select]

String str = "";
  char c;
  boolean contd = true;
  String fileStringMaster;
  while (contd == true) {
    Serial.println("gonna go again");
    fileStringMaster = getMasterData();
    if(fileStringMaster != "again") {
      break;
    }
  }
  Serial.print("the master string is "); Serial.println(fileStringMaster);
  Serial.println();

  for (int i = 0; i < fileStringMaster.length(); ++i) {
      c = fileStringMaster[i];
      if (c == ',') {
        Serial.println(str);
        str = "";
      } else {
        str += c;
      }
    }


This code gets the answers
Code: [Select]

the master string is 238.00methane,29.00alcohol,25.94temp,48.33humid,417.00CO2 enviro,2.00TVOC,100779.25Pressure,47.09Altitude

238.00methane
29.00alcohol
25.94temp
48.33humid
417.00CO2 enviro
2.00TVOC
100779.25Pressure
47.09Altitude


Code: [Select]

String str = "";
  char c;
  boolean contd = true;
  String fileStringMaster;
  while (contd == true) {
    Serial.println("gonna go again");
    fileStringMaster = getMasterData();
    if(fileStringMaster != "again") {
      break;
    }
  }
  Serial.print("the master is "); Serial.println(fileStringMaster);
 
  myFile = SD.open(file_name, FILE_WRITE);
  if (myFile) {
    Serial.println("start second print");
    for (int i = 0; i < fileStringMaster.length(); ++i) {
      c = fileStringMaster[i];
      if (c == ',') {
        Serial.println(str);
        myFile.println(str);
        str = "";
      } else {
        str += c;
      }
    }
    myFile.close();
    Serial.println("finish second print");
  } else {
    Serial.println("Did NOT print 2nd time");
  }


But, when all I change is that I open a file, I get the answer

the master is

(my function returns nothing. also, the file does not open even though it's formatted to FAT, the file name is less than 8 characters long, and then pins are hooked up correctly)

The function getMasterData() does not change at all between the two code examples.

Does opening a file affect strings in a way that I'm not aware of?
Hopefully some coding wizard out there could explain.

Thanks,
Nathan

el_supremo

OK, so it's solved. Why not tell us how you solved it?

Pete
Don't send me technical questions via Private Message.

nathan_ngu

For a minute, I thought I solved the issue, even though I had just initialized the card correctly.

My bad

Nathan

Go Up