#include <SD.h>
#define pin_cs 4
File TugasArief,root;
void setup() {
Serial.begin(9600);
if (!SD.begin(pin_cs))
{
Serial.println("Mungkin ada masalah dengan memory card ");
while(1);
}
buat_File();
cekIsicard();
// put your setup code here, to run once:
}
void loop() {
int adc = analogRead(A0);
isiFile(adc);
delay(1000);
bacaFile();
delay(1000);
// put your main code here, to run repeatedly:
}
void isiFile(int nilaiadc)
{
TugasArief = SD.open("test.txt",FILE_WRITE);
if(TugasArief)
{
TugasArief.println(nilaiadc);
TugasArief.close();
}
else
{
Serial.println("Gagal mengisi file");
while(1);
}
}
void bacaFile()
{
TugasArief = SD.open("test.txt");
if(TugasArief)
{
while(TugasArief.available())
{
Serial.write(TugasArief.read());
}
TugasArief.close();
}
else
{
Serial.println("gagal membaca file");
while(1);
}
}
void cekIsicard()
{
root = SD.open("/");
while(true){
File cekfile = root.openNextFile();
if(!cekfile)
{
break;
}
Serial.print(cekfile.name());
Serial.print(" ");
Serial.println(cekfile.size());
cekfile.close();
}
}
void buat_File()
{
TugasArief = SD.open("test.txt",FILE_WRITE);
if(TugasArief)
{
TugasArief.println("ini adalah isi file tugas mikrokontroller sensor dan transduser ARIEF SURYA ABADI");
TugasArief.close();
}
else
{
Serial.println("Gagal membuat file");
while(1);
}
}