Advertisement
Guest User

Dados SD em Variaveis

a guest
Aug 12th, 2015
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. /**
  2. *
  3. * O arquivo teste.txt deve estar no seguinte formato:
  4. *
  5. * [alexandre]
  6. * [joao]
  7. * [maria]
  8. * [jose]
  9. *
  10. */
  11.  
  12. #include <SPI.h>
  13. #include <SD.h>
  14.  
  15. File arquivo;
  16.  
  17. void setup()
  18. {
  19.   // Open serial communications and wait for port to open:
  20.   Serial.begin(9600);
  21.   while (!Serial) {
  22.     ; // wait for serial port to connect. Needed for Leonardo only
  23.   }
  24.   Serial.print("Iniciando SD card...");
  25.   // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  26.   // Note that even if it's not used as the CS pin, the hardware SS pin
  27.   // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  28.   // or the SD library functions will not work.
  29.   pinMode(10, OUTPUT);
  30.   if (!SD.begin(4)) {
  31.     Serial.println("Falha ao iniciar!");
  32.     return;
  33.   }
  34.   Serial.println("Iniciado com sucesso!");
  35.  
  36.   lerNomes();
  37.  
  38. }
  39.  
  40. void loop()
  41. {
  42. }
  43.  
  44. void lerNomes(){
  45.  
  46.   // array que vai armazenar os nomes capturados
  47.   String nomes[4];
  48.   char caracter;
  49.   String nomeSalvo;
  50.   int contador = 0;
  51.  
  52.   arquivo = SD.open("teste.txt");
  53.   if (arquivo) {
  54.     while (arquivo.available()) {
  55.       caracter = arquivo.read();
  56.       while((arquivo.available()) && (caracter != '[')){
  57.         caracter = arquivo.read();
  58.       }
  59.  
  60.       caracter = arquivo.read();
  61.       while((arquivo.available()) && (caracter != ']')){
  62.         nomeSalvo = nomeSalvo + caracter;
  63.         caracter = arquivo.read();
  64.       }
  65.       if(caracter == ']'){
  66.         /*
  67.          // Para depuraçao na serial monitor
  68.          Serial.print("Nome:");
  69.          Serial.println(nomeSalvo);
  70.          Serial.print("Posicao: ");
  71.          Serial.println(contador);
  72.          */
  73.          
  74.          // adiciona o nome capturado a uma nova posicao no array
  75.          // comeca por zero 0
  76.          nomes[contador] = nomeSalvo;
  77.  
  78.         // Limpa a String
  79.         nomeSalvo = "";
  80.        
  81.         contador++;
  82.       }
  83.     }
  84.  
  85.     // mostra os nomes em cada posiço do array
  86.     for(int i = 0; i < 4; i++) {
  87.       Serial.print("Nome posicao ");
  88.       Serial.print(i);
  89.       Serial.print(": ");
  90.       Serial.println(nomes[i]);
  91.     }
  92.  
  93.     // close the file:
  94.     arquivo.close();
  95.   }
  96.   else {
  97.     // if the file didn't open, print an error:
  98.     Serial.println("Erro ao abrir teste.txt");
  99.   }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement