|
| 1 | +/* |
| 2 | + Non-blocking Write |
| 3 | +
|
| 4 | + This example demonstrates how to perform non-blocking writes |
| 5 | + to a file on a SD card. The file will contain the current millis() |
| 6 | + value every 10ms. If the SD card is busy, the data will be buffered |
| 7 | + in order to not block the sketch. |
| 8 | +
|
| 9 | + NOTE: myFile.availableForWrite() will automatically sync the |
| 10 | + file contents as needed. You may lose some unsynced data |
| 11 | + still if myFile.sync() or myFile.close() is not called. |
| 12 | +
|
| 13 | + The circuit: |
| 14 | + - Arduino MKR Zero board |
| 15 | + - micro SD card attached |
| 16 | +
|
| 17 | + This example code is in the public domain. |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <SD.h> |
| 21 | + |
| 22 | +// file name to use for writing |
| 23 | +const char filename[] = "demo.txt"; |
| 24 | + |
| 25 | +// File object to represent file |
| 26 | +File txtFile; |
| 27 | + |
| 28 | +// string to buffer output |
| 29 | +String buffer; |
| 30 | + |
| 31 | +unsigned long lastMillis = 0; |
| 32 | + |
| 33 | +void setup() { |
| 34 | + Serial.begin(9600); |
| 35 | + while (!Serial); |
| 36 | + |
| 37 | + // reserve 1kB for String used as a buffer |
| 38 | + buffer.reserve(1024); |
| 39 | + |
| 40 | + // set LED pin to output, used to blink when writing |
| 41 | + pinMode(LED_BUILTIN, OUTPUT); |
| 42 | + |
| 43 | + // init the SD card |
| 44 | + if (!SD.begin()) { |
| 45 | + Serial.println("Card failed, or not present"); |
| 46 | + // don't do anything more: |
| 47 | + while (1); |
| 48 | + } |
| 49 | + |
| 50 | + // If you want to start from an empty file, |
| 51 | + // uncomment the next line: |
| 52 | + // SD.remove(filename); |
| 53 | + |
| 54 | + // try to open the file for writing |
| 55 | + txtFile = SD.open(filename, FILE_WRITE); |
| 56 | + if (!txtFile) { |
| 57 | + Serial.print("error opening "); |
| 58 | + Serial.println(filename); |
| 59 | + while (1); |
| 60 | + } |
| 61 | + |
| 62 | + // add some new lines to start |
| 63 | + txtFile.println(); |
| 64 | + txtFile.println("Hello World!"); |
| 65 | +} |
| 66 | + |
| 67 | +void loop() { |
| 68 | + // check if it's been over 10 ms since the last line added |
| 69 | + unsigned long now = millis(); |
| 70 | + if ((now - lastMillis) >= 10) { |
| 71 | + // add a new line to the buffer |
| 72 | + buffer += "Hello "; |
| 73 | + buffer += now; |
| 74 | + buffer += "\r\n"; |
| 75 | + |
| 76 | + lastMillis = now; |
| 77 | + } |
| 78 | + |
| 79 | + // check if the SD card is available to write data without blocking |
| 80 | + // and if the buffered data is enough for the full chunk size |
| 81 | + unsigned int chunkSize = txtFile.availableForWrite(); |
| 82 | + if (chunkSize && buffer.length() >= chunkSize) { |
| 83 | + // write to file and blink LED |
| 84 | + digitalWrite(LED_BUILTIN, HIGH); |
| 85 | + txtFile.write(buffer.c_str(), chunkSize); |
| 86 | + digitalWrite(LED_BUILTIN, LOW); |
| 87 | + |
| 88 | + // remove written data from buffer |
| 89 | + buffer.remove(0, chunkSize); |
| 90 | + } |
| 91 | +} |
0 commit comments