-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathInteractiveFile.ino
More file actions
129 lines (114 loc) · 3.22 KB
/
InteractiveFile.ino
File metadata and controls
129 lines (114 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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 <SPI.h>
#include <SD.h>
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();
}
}