// PROGRAM SD CARD //
// PERANGKAT KONTROL AKSES //
// MALIKA AYUNASARI || 2019101624//
// =======================TUGAS I========================= //
#include <SD.h>
#include <SPI.h>
#define CS_PIN 10
File root;
File textFile;
File pikFile;
void setup() {
Serial.begin(115200);
Serial.print("Initializing SD card... ");
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}
Serial.println("Initialization done.");
// MENAMPILKAN KAPASITAS MEMORI-I
Serial.println("Files in the card:");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("");
// MEMBACA FILE YUHU.txt
textFile = SD.open("YUHU.txt");
if (textFile) {
Serial.println("YUHU.txt: ");
while (textFile.available()) {
String line = textFile.readStringUntil('\n');
Serial.println(line);
}
textFile.close();
} else {
Serial.println("error opening YUHU.txt!");
}
// MEMBUAT FILE COBA.txt
File meFile = SD.open("COBA.txt", FILE_WRITE);
if (meFile) {
Serial.println(" ");
Serial.println("Writing to COBA.txt...");
meFile.println("HALLO SIBERMAN");
meFile.println("HALLO SANDIMAN");
meFile.close();
Serial.println("done making new file!");
} else {
Serial.println("Error opening COBA.txt!");
}
// MEMBACA FILE COBA.txt
meFile = SD.open("COBA.txt");
if (meFile) {
Serial.println("COBA.txt: ");
// Read from the file until there's nothing else in it:
while (meFile.available()) {
Serial.write(meFile.read());
}
// Close the file:
meFile.close();
} else {
// If the file didn't open, print an error:
Serial.println("Error opening COBA.txt");
}
// MENAMPILKAN KAPASITAS MEMORI-I (after add COBA.txt)
Serial.println("Files in the card:");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("");
// MEMBUAT FILE TEST.txt
File myFile = SD.open("TEST.txt", FILE_WRITE);
if (myFile) {
Serial.println("Writing to TEST.txt...");
myFile.print("Politeknik Siber dan Sandi Negara");
myFile.close();
Serial.println("done making new file!");
} else {
Serial.println("Error opening TEST.txt!");
}
// MEMBACA FILE TEST.txt
myFile = SD.open("TEST.txt");
if (myFile) {
Serial.println("TEST.txt: ");
// 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 the file didn't open, print an error:
Serial.println("Error opening TEST.txt");
}
// MENAMPILKAN KAPASITAS MEMORI-II (after add TEST.txt)
Serial.println("Files in the card:");
myFile = SD.open("/");
printDirectory(myFile, 0);
Serial.println("");
// UPDATE FILE TEST.txt
myFile = SD.open("TEST.txt", FILE_WRITE);
if (myFile) {
myFile.println(" dibawah Badan Siber dan Sandi Negara.");
myFile.close();
Serial.println("Updating file TEST.txt!");
} else {
Serial.print("Error opening test.txt!");
}
// MEMBACA FILE TEST.txt YANG SUDAH DI UPDATE
myFile = SD.open("TEST.txt");
if (myFile) {
Serial.print("");
Serial.println("TEST.txt: ");
// 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 the file didn't open, print an error:
Serial.println("Error opening test.txt");
}
// MENAMPILKAN KAPASITAS MEMORI-III (after TEST.txt updated)
Serial.print("Files in the card:");
myFile = SD.open("/");
printDirectory(myFile, 0);
Serial.println("");
// MEMBACA FILE YUHU.txt PERBARIS
textFile = SD.open("YUHU.txt");
if (textFile) {
Serial.println("YUHU.txt: ");
int lineCount = 1; // Nomor baris
while (textFile.available()) {
String line = textFile.readStringUntil('\n');
Serial.print("Baris ke-");
Serial.print(lineCount);
Serial.print(": ");
Serial.println(line);
lineCount++;
}
textFile.close();
} else {
Serial.println("Error opening YUHU.txt!");
}
}
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();
}
}
void loop(){
}