59 lines
1.7 KiB
C++
59 lines
1.7 KiB
C++
/*
|
|
* Append Example
|
|
*
|
|
* This sketch shows how to use open for append and the Arduino Print class
|
|
* with SdFat.
|
|
*/
|
|
#include <SdFat.h>
|
|
#include <SdFatUtil.h> // use functions to print strings from flash memory
|
|
|
|
const uint8_t SD_CHIP_SELECT = SS;
|
|
|
|
SdFat sd;
|
|
|
|
SdFile file;
|
|
|
|
// store error strings in flash to save RAM
|
|
#define error(s) sd.errorHalt_P(PSTR(s))
|
|
//------------------------------------------------------------------------------
|
|
void setup(void) {
|
|
Serial.begin(9600);
|
|
while (!Serial) {} // wait for Leonardo
|
|
PgmPrintln("Type any character to start");
|
|
while (Serial.read() <= 0) {}
|
|
delay(200); // Catch Due reset problem
|
|
|
|
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
|
|
// breadboards. use SPI_FULL_SPEED for better performance.
|
|
if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) sd.initErrorHalt();
|
|
|
|
char name[] = "APPEND.TXT";
|
|
PgmPrint("Appending to: ");
|
|
Serial.println(name);
|
|
|
|
for (uint8_t i = 0; i < 100; i++) {
|
|
// O_CREAT - create the file if it does not exist
|
|
// O_APPEND - seek to the end of the file prior to each write
|
|
// O_WRITE - open for write
|
|
if (!file.open(name, O_CREAT | O_APPEND | O_WRITE)) {
|
|
error("open failed");
|
|
}
|
|
// print 100 lines to file
|
|
for (uint8_t j = 0; j < 100; j++) {
|
|
file.print("line ");
|
|
file.print(j, DEC);
|
|
file.print(" of pass ");
|
|
file.print(i, DEC);
|
|
file.print(" millis = ");
|
|
file.println(millis());
|
|
}
|
|
if (file.writeError) error("write failed");
|
|
if (!file.close()) error("close failed");
|
|
if (i > 0 && i%25 == 0) Serial.println();
|
|
Serial.write('.');
|
|
}
|
|
Serial.println();
|
|
Serial.println("Done");
|
|
}
|
|
void loop(void){}
|