I used and sd card I want to open the file write 1 value on it then clear it at next write and write new value again and so on...
I searched on clear file but no clear function? can you help me!
so I go with sd.remove(filename)... but also on the test code (Files) removing part give that the file is removed successfully >>> but at the test code (listfiles) it give all files removed or not>> so that mean that nothing removed actually? any help how to clear a file or how to delete it?
File 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 Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
}
else {
Serial.println("example.txt doesn't exist.");
}
// open a new file and immediately close it:
Serial.println("Creating example.txt...");
myFile = SD.open("example.txt", FILE_WRITE);
myFile.close();
// Check to see if the file exists:
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
}
else {
Serial.println("example.txt doesn't exist.");
}
// delete the file:
Serial.println("Removing example.txt...");
SD.remove("example.txt");
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
}
else {
Serial.println("example.txt doesn't exist.");
}
}
My sketch also is having trouble removing a file. In the line in the void setup(), I am trying to remove file ul.txt, but I keep getting that message in my code that says "ultra not working". Ill post my entire code:
#include <NewPing.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <SPI.h>
SoftwareSerial mySerial(3,2);
Adafruit_GPS GPS(&mySerial);
char c;
String NMEA1;
String NMEA2;
#define TRIGGER_PIN 6
#define ECHO_PIN 7
#define MAX_DISTANCE 200
#define pingSpeed 1000
#define chipSelect 4
unsigned long pingTimer1, pingTimer2, pingTimer3;
NewPing sonar(6,7,65);
File myFile;
void setup() {
Serial.begin(115200);
pingTimer1 = millis() + pingSpeed;
pingTimer2 = pingTimer1 + (pingSpeed/2);
pingTimer3= pingTimer2 + pingSpeed;
GPS.begin(9600);
GPS.sendCommand("$PGCMD,33,0*6D");
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
if(!SD.begin(4)) {
Serial.println(F("not beginning"));
return;
}
Serial.println(F("its working"));
pinMode(10,OUTPUT);
SD.remove("ultrad.txt");
if(! SD.remove("ultrad.txt")){
Serial.println(F("ultra not working"));
return;
}
SD.remove("gps.txt");
if(!SD.remove("gps.txt")){
Serial.println(F("gps not working"));
return;
}
}
void loop() {
if(millis()>= pingTimer1) {
pingTimer1 += pingSpeed;
Ping();
}
if (millis() >= pingTimer2) {
pingTimer2 = pingTimer1 + (pingSpeed/2);
GPSdata();
}
if (millis() >= pingTimer3) {
pingTimer3 = pingTimer2 + pingSpeed;
SDCARD();
}
}
void Ping(){
delay(50);
int uS=sonar.ping();
// Serial.print(F("Ping: "));
// Serial.print(uS/US_ROUNDTRIP_CM);
// Serial.println(F("cm"));
}
void GPSdata() {
clearGPS();
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
NMEA1=GPS.lastNMEA();
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
NMEA2=GPS.lastNMEA();
// Serial.print(GPS.latitude);
// Serial.print(F(","));
// Serial.println(GPS.longitude);
// Serial.print(GPS.hour, DEC);Serial.print(':');
// Serial.println(GPS.minute, DEC);
}
void clearGPS() {
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
}
void SDCARD(){
int uS=sonar.ping();
myFile = SD.open("ultrad.txt", FILE_WRITE);
myFile.print(uS/US_ROUNDTRIP_CM);
myFile.print(",");
myFile.print("cm");
myFile.print(",");
myFile.print(GPS.hour, DEC);myFile.print(':');
myFile.println(GPS.minute, DEC);
myFile.close();
myFile = SD.open("gps.txt", FILE_WRITE);
myFile.print(GPS.latitude);
myFile.print(",");
myFile.print(GPS.longitude);
myFile.print(",");
myFile.print(GPS.hour, DEC);myFile.print(':');
myFile.println(GPS.minute, DEC);
myFile.close();
}
I thought since I had the "(! ......)" before the SD.remove, it wouldn't execute that remove command again, rather, just see if it had not removed and if it did not work from the previous remove command, it would display the not working message.
So what you're saying is that even in that second statement with the "!" before the SD. remove, it is still removing it even though I have the ! command?
execute SD.remove("ultrad.txt")
get the return value from that call
take the opposite logic value by using the NOT operator
and as this is in the if, check that logic value and if true then do something.
because you had already removed the file without checking for the error previously then the second time you try you are 100% sure it will fail to remove the file and thus print the error
so just get rid of the first statement before the if and you are fine; (well you'll print "ultra not working" if SD.remove returned false.)