#include <SD.h>
#define chipSelect 53
File myFile;
void setup()
{
Serial.begin(9600);
pinMode(chipSelect,OUTPUT);
if (!SD.begin(chipSelect)) Serial.println("initialization failed!"); //Initializes the SD library and card.
else Serial.println("initialization done.");
}
void loop()
{
int data;
Serial.print("1:Check file exist ");
Serial.print("2:Create file ");
Serial.print("3:Remove file ");
Serial.print("4:Write file ");
Serial.println("5:Read file");
while(!Serial.available()) continue;
data=Serial.read();
if (data=='1')
{
if (SD.exists("example.txt")) Serial.println("example.txt exists."); //Tests whether a file or directory exists on the SD card.
else Serial.println("example.txt doesn't exist.");
}
if (data=='2')
{
Serial.println("creating example.txt");
myFile = SD.open("example.txt",FILE_WRITE); //Opens a file on the SD card.
myFile.close(); //Close the file
}
if (data=='3')
{
Serial.println("removing example.txt");
SD.remove("example.txt"); // delete file:
}
if (data=='4')
{
myFile = SD.open("example.txt",FILE_WRITE); //Opens a file on the SD card.
if (myFile)
{
Serial.println("writing to example.txt");
myFile.println("shahrulnizam.com");
myFile.close(); //Close the file
}
else Serial.println("error opening example.txt");
}
if (data=='5')
{
myFile = SD.open("example.txt"); //Opens a file on the SD card.
if (myFile)
{
Serial.println("reading from example.txt:");
while (myFile.available()) Serial.write(myFile.read());
myFile.close(); //Close the file
}
else Serial.println("error opening example.txt");
}
Serial.println();
}
0 comentarios:
Publicar un comentario