Files
SyncHome/trunk/Arduino/libraries/SD/examples/NonBlockingWrite/NonBlockingWrite.ino

119 lines
3.7 KiB
Arduino
Raw Normal View History

2023-03-17 11:59:21 +00:00
/*
Non-blocking Write
This example demonstrates how to perform non-blocking writes
to a file on a SD card. The file will contain the current millis()
2026-03-20 17:18:15 +00:00
value every 10ms. If the SD card is busy, the data will be dataBuffered
2023-03-17 11:59:21 +00:00
in order to not block the sketch.
2026-03-20 17:18:15 +00:00
If data is successfully written, the built in LED will flash. After a few
seconds, check the card for a file called datalog.txt
2023-03-17 11:59:21 +00:00
NOTE: myFile.availableForWrite() will automatically sync the
file contents as needed. You may lose some unsynced data
still if myFile.sync() or myFile.close() is not called.
2026-03-20 17:18:15 +00:00
Pin numbers reflect the default SPI pins for Uno and Nano models
Updated for clarity and uniformity with other examples
2023-03-17 11:59:21 +00:00
The circuit:
2026-03-20 17:18:15 +00:00
analog sensors on analog ins 0, 1, and 2
SD card attached to SPI bus as follows:
** SDO - pin 11
** SDI - pin 12
** CLK - pin 13
** CS - depends on your SD card shield or module.
Pin 10 used here for consistency with other Arduino examples
(for MKR Zero SD: SDCARD_SS_PIN)
modified 24 July 2020
by Tom Igoe
2023-03-17 11:59:21 +00:00
This example code is in the public domain.
*/
#include <SD.h>
2026-03-20 17:18:15 +00:00
const int chipSelect = 10;
2023-03-17 11:59:21 +00:00
// file name to use for writing
2026-03-20 17:18:15 +00:00
const char filename[] = "datalog.txt";
2023-03-17 11:59:21 +00:00
// File object to represent file
2026-03-20 17:18:15 +00:00
File myFile;
2023-03-17 11:59:21 +00:00
// string to buffer output
2026-03-20 17:18:15 +00:00
String dataBuffer;
// last time data was written to card:
2023-03-17 11:59:21 +00:00
unsigned long lastMillis = 0;
void setup() {
2026-03-20 17:18:15 +00:00
// Open serial communications and wait for port to open:
2023-03-17 11:59:21 +00:00
Serial.begin(9600);
2026-03-20 17:18:15 +00:00
// reserve 1 kB for String used as a dataBuffer
dataBuffer.reserve(1024);
2023-03-17 11:59:21 +00:00
// set LED pin to output, used to blink when writing
pinMode(LED_BUILTIN, OUTPUT);
2026-03-20 17:18:15 +00:00
// wait for Serial Monitor to connect. Needed for native USB port boards only:
while (!Serial);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("1. is a card inserted?");
Serial.println("2. is your wiring correct?");
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
while (true);
2023-03-17 11:59:21 +00:00
}
2026-03-20 17:18:15 +00:00
Serial.println("initialization done.");
2023-03-17 11:59:21 +00:00
// If you want to start from an empty file,
// uncomment the next line:
2026-03-20 17:18:15 +00:00
// SD.remove(filename);
2023-03-17 11:59:21 +00:00
// try to open the file for writing
2026-03-20 17:18:15 +00:00
myFile = SD.open(filename, FILE_WRITE);
if (!myFile) {
2023-03-17 11:59:21 +00:00
Serial.print("error opening ");
Serial.println(filename);
2026-03-20 17:18:15 +00:00
while (true);
2023-03-17 11:59:21 +00:00
}
// add some new lines to start
2026-03-20 17:18:15 +00:00
myFile.println();
myFile.println("Hello World!");
Serial.println("Starting to write to file...");
2023-03-17 11:59:21 +00:00
}
void loop() {
// check if it's been over 10 ms since the last line added
unsigned long now = millis();
if ((now - lastMillis) >= 10) {
2026-03-20 17:18:15 +00:00
// add a new line to the dataBuffer
dataBuffer += "Hello ";
dataBuffer += now;
dataBuffer += "\r\n";
// print the buffer length. This will change depending on when
// data is actually written to the SD card file:
Serial.print("Unsaved data buffer length (in bytes): ");
Serial.println(dataBuffer.length());
// note the time that the last line was added to the string
2023-03-17 11:59:21 +00:00
lastMillis = now;
}
// check if the SD card is available to write data without blocking
2026-03-20 17:18:15 +00:00
// and if the dataBuffered data is enough for the full chunk size
unsigned int chunkSize = myFile.availableForWrite();
if (chunkSize && dataBuffer.length() >= chunkSize) {
2023-03-17 11:59:21 +00:00
// write to file and blink LED
digitalWrite(LED_BUILTIN, HIGH);
2026-03-20 17:18:15 +00:00
myFile.write(dataBuffer.c_str(), chunkSize);
2023-03-17 11:59:21 +00:00
digitalWrite(LED_BUILTIN, LOW);
2026-03-20 17:18:15 +00:00
// remove written data from dataBuffer
dataBuffer.remove(0, chunkSize);
2023-03-17 11:59:21 +00:00
}
}