08.11.2017 Views

arduino_básico_Michael_McRoberts

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Capítulo 15 ■ Leitura e escrita de dados em um cartão SD<br />

347<br />

// Escreve uma string no arquivo<br />

void writeString(SdFile& f, char *str) {<br />

uint8_t n;<br />

for (n = 0; str[n]; n++);<br />

f.write((uint8_t *)str, n);<br />

}<br />

void setup() {<br />

Serial.begin(9600);<br />

Serial.println();<br />

Serial.println("Type any character to start");<br />

while (!Serial.available());<br />

// inicializa o cartão SD em SPI_HALF_SPEED, para evitar erros de barramento com as protoboards.<br />

// Utilize SPI_FULL_SPEED para um melhor desempenho, caso seu cartão suporte essa opção.<br />

if (!card.init(SPI_HALF_SPEED)) error("card.init failed");<br />

// inicializa um volume FAT<br />

if (!volume.init(&card)) error("volume.init failed");<br />

// abre o diretório raiz<br />

if (!root.openRoot(&volume)) error("openRoot failed");<br />

// cria um novo arquivo<br />

char name[] = "TESTFILE.TXT";<br />

file.open(&root, name, O_CREAT | O_EXCL | O_WRITE);<br />

// Coloca a data e a hora atuais<br />

file.timestamp(2, 2010, 12, 25, 12, 34, 56);<br />

// escreve 10 linhas no arquivo<br />

for (uint8_t i = 0; i < 10; i++) {<br />

writeString(file, "Line: ");<br />

writeNumber(file, i);<br />

writeString(file, " Write test.");<br />

writeCRLF(file);<br />

}<br />

// fecha o arquivo e força uma operação de escrita de todos os dados para o cartão SD<br />

file.close();<br />

Serial.println("File Created");<br />

// abre um arquivo<br />

if (file.open(&root, name, O_READ)) {<br />

Serial.println(name);<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!