thomazrb icon

Grava e Ler variáveis no SD

thomazrb | PRO | 08/09/19 02:52:59 PM UTC | 0 ⭐ | 522 👁️ | Never ⏰ | []
text |

2.01 KB

|

None

|

0 👍

/

0 👎

#include <SPI.h>
#include <SD.h>
 File myFile;
String caracteres = "";
String variavel;
 void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
    Serial.print("Initializing SD card...");
   if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
   // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  SD.remove("thomaz.txt");
  myFile = SD.open("thomaz.txt", FILE_WRITE);
   // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println(100);
    myFile.println(200);  
    myFile.println(-5000);
    myFile.println(1.32);  
    myFile.println(17);
    myFile.println(-50.2);  
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
   // re-open the file for reading:
  myFile = SD.open("thomaz.txt");
  if (myFile) {
    // read from the file until there's nothing else in it:
    while (myFile.available()) { // le letra por letra do arquivo
      char x = myFile.read(); // guarda a letra
      // quando encontrar uma quebra de linha LN ( caracteres 13 e 10) ele n salva na variavel
      if ((x != 13) && (x !=10))
          caracteres = caracteres + x;
      if (x == 10) // no vinal da quebra de linha ele armazena 
      {
         variavel = caracteres;
         Serial.print("Peguei uma variavel : ");
         Serial.println(variavel);
         caracteres = ""; //limpa os caracteres
      }
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}
 void loop() {
  // nothing happens after setup
}

Comments