|
1 | 1 | /*
|
2 |
| -This examples demonstrates the usage of the "Arduino_UnifiedStorage" library, |
3 |
| -which allows the program to easily switch between different storage mediums. |
| 2 | + SimpleStorageWriteRead |
4 | 3 |
|
5 |
| -By uncommenting the appropriate lines, you can choose to use either an SD card, |
6 |
| -a USB storage device, or internal storage as the storage medium. |
| 4 | + Demonstrates basic usage of the "Arduino_UnifiedStorage" library to write and read data to storage. |
| 5 | + Supports SD card, USB storage, and internal storage (default, uncomment to choose). |
7 | 6 |
|
8 |
| -The example code is set up to use an SD card by default. |
| 7 | + n the setup function, the code initializes serial communication, mounts the storage medium, |
| 8 | + creates a root directory with three subdirectories, and writes data to three files in each subdirectory. |
9 | 9 |
|
10 |
| -In the setup function, the code initializes the serial communication and checks if the storage medium is successfully mounted. |
11 |
| -It then creates a root directory and three subdirectories within it. |
12 |
| -After creating the subdirectories, the code creates three files inside each subdirectory and writes data to them. |
| 10 | + Following this, the code showcases reading data from files by using "seek" and "available" methods, |
| 11 | + switching file modes to read, resetting file pointers to the start, |
| 12 | + and printing the read data to the serial monitor using a while loop. |
| 13 | +
|
| 14 | + Created 28th July 2023 |
| 15 | + By Cristian Dragomir |
| 16 | +
|
| 17 | + Modified 24th August 2023 |
| 18 | + By Ali Jahangiri |
| 19 | +
|
| 20 | + https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino |
13 | 21 |
|
14 |
| -Next, the code demonstrates how to read data from the files using the "seek" and "available" methods. |
15 |
| -It changes the mode of the files to read mode, moves the file pointers to the beginning, and reads the data from each file using a while loop. |
16 |
| -The read data is printed to the serial monitor. |
17 | 22 | */
|
18 | 23 |
|
19 | 24 | #include "Arduino_UnifiedStorage.h"
|
20 | 25 |
|
21 |
| -SDStorage unifiedStorage = SDStorage(); // or |
22 |
| -//USBStorage unifiedStorage = USBStorage() // or |
23 |
| -//InternalStorage unifiedStorage = InternalStorage(); |
| 26 | +// Uncomment one of the three lines below to select between SD card, USB or internal storage |
| 27 | +//SDStorage unifiedStorage = SDStorage(); // Create an instance for interacting with SD card storage |
| 28 | +//USBStorage unifiedStorage = USBStorage() // Create an instance for interacting with USB storage |
| 29 | +InternalStorage unifiedStorage = InternalStorage(); // Create an instance for interacting with internal Flash storage (default) |
24 | 30 |
|
25 | 31 | void setup() {
|
26 | 32 | Serial.begin(115200);
|
27 | 33 | while (!Serial);
|
28 | 34 |
|
29 | 35 | if(!unifiedStorage.begin()==0){
|
30 |
| - Serial.println("error mounting SD Card"); |
| 36 | + Serial.println("Error mounting storage device."); |
31 | 37 | }
|
32 |
| - // Create a root directory |
33 | 38 |
|
| 39 | + // Create a root directory in storage device |
34 | 40 | Folder root = unifiedStorage.getRootFolder();
|
35 | 41 |
|
36 | 42 | // Create subdirectories inside the root directory
|
37 | 43 | Folder subdir1 = root.createSubfolder("subdir1");
|
38 | 44 | Folder subdir2 = root.createSubfolder("subdir2");
|
39 | 45 | Folder subdir3 = root.createSubfolder("subdir3");
|
40 | 46 |
|
41 |
| - // Create files inside the subdirectories |
| 47 | + // Create .txt files inside the subdirectories |
42 | 48 | UFile file1 = subdir1.createFile("file1.txt", FileMode::WRITE);
|
43 | 49 | UFile file2 = subdir2.createFile("file2.txt", FileMode::WRITE);
|
44 | 50 | UFile file3 = subdir3.createFile("file3.txt", FileMode::WRITE);
|
|
0 commit comments