From a597ccd2274e1255eddab087377d4330e3d46755 Mon Sep 17 00:00:00 2001 From: KAbhijeet2105 Date: Fri, 27 Mar 2020 02:32:41 +0530 Subject: [PATCH 1/2] new examples added --- .../InteractiveDirectory.ino | 129 ++++++++++++++++ examples/InteractiveFile/InteractiveFile.ino | 137 +++++++++++++++++ .../InteractiveReadWrite.ino | 142 ++++++++++++++++++ 3 files changed, 408 insertions(+) create mode 100644 examples/InteractiveDirectory/InteractiveDirectory.ino create mode 100644 examples/InteractiveFile/InteractiveFile.ino create mode 100644 examples/InteractiveReadWrite/InteractiveReadWrite.ino diff --git a/examples/InteractiveDirectory/InteractiveDirectory.ino b/examples/InteractiveDirectory/InteractiveDirectory.ino new file mode 100644 index 0000000..15463c8 --- /dev/null +++ b/examples/InteractiveDirectory/InteractiveDirectory.ino @@ -0,0 +1,129 @@ +/* + 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 + + if (ch == 1) //for creating directory + { + Serial.println("Enter directory name to create:");//ask user for directory name + waitip(); + dir = Serial.readString(); + dir.trim(); + + if (SD.exists(dir)) { //if directory already exists + 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. +} + +void waitip()// wait for user input +{ + while (Serial.available() == 0) + {} +} +void printDirectory(File dir, int numTabs) // prints directory list +{ + 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..0273209 --- /dev/null +++ b/examples/InteractiveFile/InteractiveFile.ino @@ -0,0 +1,137 @@ +/* + 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 + + + if (ch == 1) //for creating file + { + Serial.println("Enter file name to create file:");//ask user for file name eg. test.txt + waitip(); + fname = Serial.readString(); + fname.trim(); + + if (SD.exists(fname)) { //if file already exists + 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 (SD.exists(fname)) { //if file already exists then delete it. + + 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 +} + +void waitip()// wait for user input +{ + while (Serial.available() == 0) + {} +} + +void printDirectory(File dir, int numTabs) // prints directory list +{ + 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..05971f0 --- /dev/null +++ b/examples/InteractiveReadWrite/InteractiveReadWrite.ino @@ -0,0 +1,142 @@ +/* + 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 + + if (ch == 1) //for reading the file + { + 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 +} + + +void waitip()// wait for user input +{ + while (Serial.available() == 0) + {} +} + +void printDirectory(File dir, int numTabs) // prints directory list +{ + 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(); + } +} From 100795e421711a656acadd14b20bc8812b265af5 Mon Sep 17 00:00:00 2001 From: KAbhijeet2105 Date: Fri, 27 Mar 2020 09:44:56 +0530 Subject: [PATCH 2/2] bug fix --- .../InteractiveDirectory.ino | 54 ++++++++----------- examples/InteractiveFile/InteractiveFile.ino | 54 ++++++++----------- .../InteractiveReadWrite.ino | 33 +++++------- 3 files changed, 60 insertions(+), 81 deletions(-) diff --git a/examples/InteractiveDirectory/InteractiveDirectory.ino b/examples/InteractiveDirectory/InteractiveDirectory.ino index 15463c8..25b2773 100644 --- a/examples/InteractiveDirectory/InteractiveDirectory.ino +++ b/examples/InteractiveDirectory/InteractiveDirectory.ino @@ -24,8 +24,7 @@ void setup() { ; // wait for serial port to connect. Needed for native USB port only } Serial.print("Initializing SD card..."); - if (!SD.begin(4)) - { + if (!SD.begin(4)) { Serial.println("initialization failed!"); while (1); } @@ -40,33 +39,30 @@ void setup() { //wait for input waitip(); ch = Serial.parseInt();//get choice input - - if (ch == 1) //for creating directory - { + //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 (SD.exists(dir)) { //if directory already exists + //if directory already exists + if (SD.exists(dir)) { Serial.println(dir + " already exists!"); - - } else { //if directory not exists create one + } 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 - { + if (SD.exists(dir)) { + Serial.println(dir + " created!!"); + } else { Serial.println("Error: " + dir + " not created"); } } - } - else if (ch == 2) { //for deleting directory + } else if (ch == 2) { + //for deleting directory Serial.println("Enter directory name to delete:");//ask user for directory name waitip(); dir = Serial.readString(); @@ -74,22 +70,17 @@ void setup() { if (SD.exists(dir)) { //if directory already exists then delete it. SD.rmdir(dir); //deleting directory. - if (SD.exists(dir)) - { + if (SD.exists(dir)) { Serial.println("Error: " + dir + " not deleted!!"); - } - else { + } else { Serial.println(" " + dir + " deleted!!"); - } - } - else //if directory not exists then show error message. - { + } else { + //if directory not exists then show error message. Serial.println("Error: " + dir + " not exists!!"); } - } - else // if user enters wrong choice - { + } else { + // if user enters wrong choice Serial.println("Error:wrong choice!!"); } } @@ -98,13 +89,14 @@ void loop() { // nothing happens after setup finishes. } -void waitip()// wait for user input -{ +// wait for user input +void waitip() { while (Serial.available() == 0) {} } -void printDirectory(File dir, int numTabs) // prints directory list -{ + +// prints directory list +void printDirectory(File dir, int numTabs) { while (true) { File entry = dir.openNextFile(); diff --git a/examples/InteractiveFile/InteractiveFile.ino b/examples/InteractiveFile/InteractiveFile.ino index 0273209..9c6f50a 100644 --- a/examples/InteractiveFile/InteractiveFile.ino +++ b/examples/InteractiveFile/InteractiveFile.ino @@ -46,57 +46,49 @@ void setup() { waitip(); ch = Serial.parseInt();//get choice input - - if (ch == 1) //for creating file - { - Serial.println("Enter file name to create file:");//ask user for file name eg. test.txt + //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 (SD.exists(fname)) { //if file already exists + //if file already exists + if (SD.exists(fname)) { Serial.println(fname + " already exists!"); - } else { //if file not exists create one + } 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)) - { + if (SD.exists(fname)) { Serial.println(fname + " created!!"); - - } else - { + } else { Serial.println("Error: " + fname + " not created"); } } - } - else if (ch == 2) { //for deleting file + } 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 (SD.exists(fname)) { //if file already exists then delete it. - + //if file already exists then delete it. + if (SD.exists(fname)) { SD.remove(fname); // deleting file. - if (SD.exists(fname)) - { + if (SD.exists(fname)) { Serial.println("Error: " + fname + " not deleted!!"); - } - else { + } else { Serial.println(" " + fname + " deleted!!"); - } - } - else //if file not exists then show error message. - { + } else { + //if file not exists then show error message. Serial.println("Error: " + fname + " not exists!!"); } - } - else // if user enters wrong choice - { + } else { + // if user enters wrong choice Serial.println("Error:wrong choice!!"); } } @@ -105,14 +97,14 @@ void loop() { // nothing happens after setup } -void waitip()// wait for user input -{ +// wait for user input +void waitip() { while (Serial.available() == 0) {} } -void printDirectory(File dir, int numTabs) // prints directory list -{ +// prints directory list +void printDirectory(File dir, int numTabs) { while (true) { File entry = dir.openNextFile(); diff --git a/examples/InteractiveReadWrite/InteractiveReadWrite.ino b/examples/InteractiveReadWrite/InteractiveReadWrite.ino index 05971f0..5b1e5b5 100644 --- a/examples/InteractiveReadWrite/InteractiveReadWrite.ino +++ b/examples/InteractiveReadWrite/InteractiveReadWrite.ino @@ -47,19 +47,19 @@ void setup() { waitip(); ch = Serial.parseInt();//get choice input - if (ch == 1) //for reading the file - { + //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 + 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()); @@ -67,13 +67,12 @@ void setup() { // close the file: myFile.close(); } - } - else { //if file not exists show the message + } else { + //if file not exists show the message Serial.println(fname + " doesn't exist."); } - } - - else if (ch == 2) { //writing to the file + } else if (ch == 2) { + //writing to the file Serial.println("Enter file name :");//ask user for file name waitip(); fname = Serial.readString(); @@ -92,15 +91,12 @@ void setup() { // 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 - { + } else { + // if user enters wrong choice Serial.println("Error:wrong choice!!"); } } @@ -109,15 +105,14 @@ void loop() { // nothing happens after setup } - -void waitip()// wait for user input -{ +// wait for user input +void waitip() { while (Serial.available() == 0) {} } -void printDirectory(File dir, int numTabs) // prints directory list -{ +// prints directory list +void printDirectory(File dir, int numTabs) { while (true) { File entry = dir.openNextFile();