Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * O arquivo teste.txt deve estar no seguinte formato:
- *
- * [alexandre]
- * [joao]
- * [maria]
- * [jose]
- *
- */
- #include <SPI.h>
- #include <SD.h>
- File arquivo;
- void setup()
- {
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- Serial.print("Iniciando SD card...");
- // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
- // Note that even if it's not used as the CS pin, the hardware SS pin
- // (10 on most Arduino boards, 53 on the Mega) must be left as an output
- // or the SD library functions will not work.
- pinMode(10, OUTPUT);
- if (!SD.begin(4)) {
- Serial.println("Falha ao iniciar!");
- return;
- }
- Serial.println("Iniciado com sucesso!");
- lerNomes();
- }
- void loop()
- {
- }
- void lerNomes(){
- // array que vai armazenar os nomes capturados
- String nomes[4];
- char caracter;
- String nomeSalvo;
- int contador = 0;
- arquivo = SD.open("teste.txt");
- if (arquivo) {
- while (arquivo.available()) {
- caracter = arquivo.read();
- while((arquivo.available()) && (caracter != '[')){
- caracter = arquivo.read();
- }
- caracter = arquivo.read();
- while((arquivo.available()) && (caracter != ']')){
- nomeSalvo = nomeSalvo + caracter;
- caracter = arquivo.read();
- }
- if(caracter == ']'){
- /*
- // Para depuraçao na serial monitor
- Serial.print("Nome:");
- Serial.println(nomeSalvo);
- Serial.print("Posicao: ");
- Serial.println(contador);
- */
- // adiciona o nome capturado a uma nova posicao no array
- // comeca por zero 0
- nomes[contador] = nomeSalvo;
- // Limpa a String
- nomeSalvo = "";
- contador++;
- }
- }
- // mostra os nomes em cada posiço do array
- for(int i = 0; i < 4; i++) {
- Serial.print("Nome posicao ");
- Serial.print(i);
- Serial.print(": ");
- Serial.println(nomes[i]);
- }
- // close the file:
- arquivo.close();
- }
- else {
- // if the file didn't open, print an error:
- Serial.println("Erro ao abrir teste.txt");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement