Advertisement
thomazrb

Grava e Ler variáveis no SD

Aug 9th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <SD.h>
  3.  
  4. File myFile;
  5. String caracteres = "";
  6. String variavel;
  7.  
  8. void setup() {
  9. // Open serial communications and wait for port to open:
  10. Serial.begin(9600);
  11. while (!Serial) {
  12. ; // wait for serial port to connect. Needed for native USB port only
  13. }
  14.  
  15.  
  16. Serial.print("Initializing SD card...");
  17.  
  18. if (!SD.begin(4)) {
  19. Serial.println("initialization failed!");
  20. while (1);
  21. }
  22. Serial.println("initialization done.");
  23.  
  24. // open the file. note that only one file can be open at a time,
  25. // so you have to close this one before opening another.
  26. SD.remove("thomaz.txt");
  27. myFile = SD.open("thomaz.txt", FILE_WRITE);
  28.  
  29. // if the file opened okay, write to it:
  30. if (myFile) {
  31. Serial.print("Writing to test.txt...");
  32. myFile.println(100);
  33. myFile.println(200);
  34. myFile.println(-5000);
  35. myFile.println(1.32);
  36. myFile.println(17);
  37. myFile.println(-50.2);
  38. // close the file:
  39. myFile.close();
  40. Serial.println("done.");
  41. } else {
  42. // if the file didn't open, print an error:
  43. Serial.println("error opening test.txt");
  44. }
  45.  
  46. // re-open the file for reading:
  47. myFile = SD.open("thomaz.txt");
  48. if (myFile) {
  49. // read from the file until there's nothing else in it:
  50. while (myFile.available()) { // le letra por letra do arquivo
  51. char x = myFile.read(); // guarda a letra
  52. // quando encontrar uma quebra de linha LN ( caracteres 13 e 10) ele n salva na variavel
  53. if ((x != 13) && (x !=10))
  54. caracteres = caracteres + x;
  55. if (x == 10) // no vinal da quebra de linha ele armazena
  56. {
  57. variavel = caracteres;
  58. Serial.print("Peguei uma variavel : ");
  59. Serial.println(variavel);
  60. caracteres = ""; //limpa os caracteres
  61. }
  62. }
  63. // close the file:
  64. myFile.close();
  65. } else {
  66. // if the file didn't open, print an error:
  67. Serial.println("error opening test.txt");
  68. }
  69. }
  70.  
  71. void loop() {
  72. // nothing happens after setup
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement