diff --git a/examples/InteractiveDirectory/InteractiveDirectory.ino b/examples/InteractiveDirectory/InteractiveDirectory.ino new file mode 100644 index 0000000..25b2773 --- /dev/null +++ b/examples/InteractiveDirectory/InteractiveDirectory.ino @@ -0,0 +1,121 @@ +/* + SD card basic example + This example shows how to create and delete an SD card directory. + + The circuit: + SD card attached to SPI bus as follows: + ** MOSI - pin 11 + ** MISO - pin 12 + ** CLK - pin 13 + ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN) + created by Abhijeet Kadam + This example code is in the public domain. +*/ +#include +#include + +File myFile, root; +String dir; +int ch; +void setup() { + // Open serial communications and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + Serial.print("Initializing SD card..."); + if (!SD.begin(4)) { + Serial.println("initialization failed!"); + while (1); + } + + root = SD.open("/"); // create root reference. + Serial.println("initialization done."); + + printDirectory(root, 0); // print directory and file list + Serial.println("1.Create Directory"); //ask user to input choice + Serial.println("2.Delete Directory"); + Serial.println("Enter your choice:"); + //wait for input + waitip(); + ch = Serial.parseInt();//get choice input + //for creating directory + if (ch == 1) { + Serial.println("Enter directory name to create:");//ask user for directory name + waitip(); + dir = Serial.readString(); + dir.trim(); + + //if directory already exists + if (SD.exists(dir)) { + Serial.println(dir + " already exists!"); + } else { + //if directory not exists create one + Serial.println(dir + " doesn't exist."); + Serial.println("Creating directory.."); + SD.mkdir(dir); // creating directory + + if (SD.exists(dir)) { + Serial.println(dir + " created!!"); + } else { + Serial.println("Error: " + dir + " not created"); + } + } + } else if (ch == 2) { + //for deleting directory + Serial.println("Enter directory name to delete:");//ask user for directory name + waitip(); + dir = Serial.readString(); + dir.trim(); + if (SD.exists(dir)) { //if directory already exists then delete it. + SD.rmdir(dir); //deleting directory. + + if (SD.exists(dir)) { + Serial.println("Error: " + dir + " not deleted!!"); + } else { + Serial.println(" " + dir + " deleted!!"); + } + } else { + //if directory not exists then show error message. + Serial.println("Error: " + dir + " not exists!!"); + } + } else { + // if user enters wrong choice + Serial.println("Error:wrong choice!!"); + } +} + +void loop() { + // nothing happens after setup finishes. +} + +// wait for user input +void waitip() { + while (Serial.available() == 0) + {} +} + +// prints directory list +void printDirectory(File dir, int numTabs) { + while (true) { + + File entry = dir.openNextFile(); + if (! entry) { + // no more files + break; + } + for (uint8_t i = 0; i < numTabs; i++) { + Serial.print('\t'); + } + Serial.print(entry.name()); + if (entry.isDirectory()) { + Serial.println("/"); + printDirectory(entry, numTabs + 1); + } else { + // files have sizes, directories do not + Serial.print("\t\t"); + Serial.println(entry.size(), DEC); + } + entry.close(); + } +} diff --git a/examples/InteractiveFile/InteractiveFile.ino b/examples/InteractiveFile/InteractiveFile.ino new file mode 100644 index 0000000..9c6f50a --- /dev/null +++ b/examples/InteractiveFile/InteractiveFile.ino @@ -0,0 +1,129 @@ +/* + SD card basic example + This example shows how to create and delete an SD card file. + + The circuit: + SD card attached to SPI bus as follows: + ** MOSI - pin 11 + ** MISO - pin 12 + ** CLK - pin 13 + ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN) + created by Abhijeet Kadam + This example code is in the public domain. + +*/ + +#include +#include + +File myFile, root; +int ch; +String fname; + +void setup() { + // Open serial communications and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + Serial.print("Initializing SD card..."); + + if (!SD.begin(4)) { + Serial.println("initialization failed!"); + while (1); + } + Serial.println("initialization done."); + + // open the file. note that only one file can be open at a time, + // so you have to close this one before opening another. + root = SD.open("/"); // create root reference. + printDirectory(root, 0); // print directory and file list + Serial.println("1.Create File"); //ask user to input choice + Serial.println("2.Delete File"); + Serial.println("Enter your choice:"); + //wait for input + waitip(); + ch = Serial.parseInt();//get choice input + + //for creating file + if (ch == 1) { + Serial.println("Enter file name to create file:"); //ask user for file name eg. test.txt + waitip(); + fname = Serial.readString(); + fname.trim(); + //if file already exists + if (SD.exists(fname)) { + Serial.println(fname + " already exists!"); + + } else { + //if file not exists create one + Serial.println(fname + " doesn't exist."); + Serial.println("Creating file.."); + myFile = SD.open(fname, FILE_WRITE); // creating file + myFile.close(); + if (SD.exists(fname)) { + Serial.println(fname + " created!!"); + } else { + Serial.println("Error: " + fname + " not created"); + } + } + } else if (ch == 2) { + //for deleting file + Serial.println("Enter file name to delete:");//ask user for file name + waitip(); + fname = Serial.readString(); + fname.trim(); + //if file already exists then delete it. + if (SD.exists(fname)) { + SD.remove(fname); // deleting file. + + if (SD.exists(fname)) { + Serial.println("Error: " + fname + " not deleted!!"); + } else { + Serial.println(" " + fname + " deleted!!"); + } + } else { + //if file not exists then show error message. + Serial.println("Error: " + fname + " not exists!!"); + } + } else { + // if user enters wrong choice + Serial.println("Error:wrong choice!!"); + } +} + +void loop() { + // nothing happens after setup +} + +// wait for user input +void waitip() { + while (Serial.available() == 0) + {} +} + +// prints directory list +void printDirectory(File dir, int numTabs) { + while (true) { + + File entry = dir.openNextFile(); + if (! entry) { + // no more files + break; + } + for (uint8_t i = 0; i < numTabs; i++) { + Serial.print('\t'); + } + Serial.print(entry.name()); + if (entry.isDirectory()) { + Serial.println("/"); + printDirectory(entry, numTabs + 1); + } else { + // files have sizes, directories do not + Serial.print("\t\t"); + Serial.println(entry.size(), DEC); + } + entry.close(); + } +} diff --git a/examples/InteractiveReadWrite/InteractiveReadWrite.ino b/examples/InteractiveReadWrite/InteractiveReadWrite.ino new file mode 100644 index 0000000..5b1e5b5 --- /dev/null +++ b/examples/InteractiveReadWrite/InteractiveReadWrite.ino @@ -0,0 +1,137 @@ +/* + SD card read/write + + This example shows how to read and write data to and from an SD card file + The circuit: + SD card attached to SPI bus as follows: + ** MOSI - pin 11 + ** MISO - pin 12 + ** CLK - pin 13 + ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN) + + created by Abhijeet Kadam + + This example code is in the public domain. + +*/ + +#include +#include + +String fname, fileData; +int ch; +File root, myFile; + +void setup() { + // Open serial communications and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + Serial.print("Initializing SD card..."); + + if (!SD.begin(4)) { + Serial.println("initialization failed!"); + while (1); + } + Serial.println("initialization done."); + + root = SD.open("/"); // create root reference. + printDirectory(root, 0); // print directory and file list + + Serial.println("1.Read File"); //ask user to input choice + Serial.println("2.Write File"); + Serial.println("Enter your choice:"); + //wait for input + waitip(); + ch = Serial.parseInt();//get choice input + + //for reading the file + if (ch == 1) { + Serial.println("Enter file name to read file data:");//ask user for file name eg. test.txt + waitip(); + fname = Serial.readString(); + fname.trim(); + + if (SD.exists(fname)) { + //if file already exists + + myFile = SD.open(fname); + if (myFile) { + Serial.println(" " + fname); + // read from the file until there's nothing else in it: + while (myFile.available()) { + Serial.write(myFile.read()); + } + // close the file: + myFile.close(); + } + } else { + //if file not exists show the message + Serial.println(fname + " doesn't exist."); + } + } else if (ch == 2) { + //writing to the file + Serial.println("Enter file name :");//ask user for file name + waitip(); + fname = Serial.readString(); + fname.trim(); + + myFile = SD.open(fname, FILE_WRITE);//open the file if file not exists create one + + Serial.println("Enter data to store inside the file :"); + waitip(); + fileData = Serial.readString(); + fileData.trim(); + // if the file opened okay, write to it: + if (myFile) { + Serial.print("Writing to " + fname + "..."); + myFile.println(fileData); + // close the file: + myFile.close(); + Serial.println("done."); + } else { + // if the file didn't open, print an error: + Serial.println("error opening test.txt"); + } + } else { + // if user enters wrong choice + Serial.println("Error:wrong choice!!"); + } +} + +void loop() { + // nothing happens after setup +} + +// wait for user input +void waitip() { + while (Serial.available() == 0) + {} +} + +// prints directory list +void printDirectory(File dir, int numTabs) { + while (true) { + + File entry = dir.openNextFile(); + if (! entry) { + // no more files + break; + } + for (uint8_t i = 0; i < numTabs; i++) { + Serial.print('\t'); + } + Serial.print(entry.name()); + if (entry.isDirectory()) { + Serial.println("/"); + printDirectory(entry, numTabs + 1); + } else { + // files have sizes, directories do not + Serial.print("\t\t"); + Serial.println(entry.size(), DEC); + } + entry.close(); + } +}