Delete Files in SD Card

Hi All,

I hope you can help my codes attached below.

I tried to HALT the camera when it took 5 pictures and save to SD card and then I want to delete all the files from SD card and start taking pictures again and keep going; Unfortunately, it does not start deleting files after taking 5 images. If I separate this code into two sketches. It just works fine. I have no ideas how to fix this. I just start to learn Arduino IDE. It will be appreciated if you can help.

#include <SdFat.h>
#include “CameraC329.h”
#define CAMERA_BAUD 14400
SdFatSdio SD;
File photoFile;

int buzzerPin = 2; //set buzzer pin.
const int redLED = 8; //set yellow LED.
const int yellowLED = 7; //set red LED.
int yellowLEDstate = LOW; //save the red LED state.
int redLEDstate = LOW; //save the yellow LED state.
int loopCounter = 0;
int var;
boolean isCreateNewFile = false;
boolean isRemoveFile = false;
int countCreateNewFile = 0; //Test.
int countRemoveFile = 0; //Test.

CameraC329 camera;
uint32_t writtenPictureSize;
void getPicture_callback(uint32_t pictureSize, uint16_t packetSize, uint32_t packetStartPosition, byte* packet)
{
photoFile.write(packet, packetSize);
Serial.write (packet, packetSize);
writtenPictureSize += packetSize;
if (writtenPictureSize >= pictureSize)
{
photoFile.flush();
photoFile.close();
Serial.println(F(“SUCCESS”));
}
}

void setup()
{
Serial.begin(9600);
while (!Serial);

pinMode(redLED, OUTPUT); //Setup yellow LED.
pinMode(yellowLED, OUTPUT); //Setup red LED.
pinMode(buzzerPin, OUTPUT); //Setup buzzer.

Serial.println(F(“"));
Serial.println(F("
”));
Serial.println();
Serial.println(F(“Serial READY”));
}

void loop()
{
loopCounter = loopCounter + 1; //Test.
Serial.println();
Serial.print(F("Loop Counter–> "));
Serial.println(loopCounter);

var = random(1, 5); //Test random CASE option.
var = 1; //Choose specific CASE option to test.

switch (var)
{
case 1:
Serial.println(F(“Create a New File…”));
CreateNewFile(); //Call function.
countCreateNewFile = countCreateNewFile + 1;
delay (1000);
break;

case 2:
Serial.println(F(“Remove File…”));
RemoveFile(); //Call function.
countRemoveFile = countRemoveFile + 1;
break;
}

if ( loopCounter >= 5) {

Serial.println(F("**********************************"));

Serial.print(F(“Total Create a New Files…”));
Serial.println(countCreateNewFile);

Serial.print(F(“Total Remove File…”));
Serial.println(countRemoveFile);

while (true); // test - stop program
}
}
/////////////////////////////////
//CreateNewFile

void CreateNewFile() {
Serial2.begin(CAMERA_BAUD);
while (!Serial2)
Serial.println(F(“Camera connected”));
delay(2000);

if (!SD.begin())
{
Serial.println(F(“SD initialization failed or missing SD card”));
return;
}
else
{
Serial.println(F(“Began SD Card”));

tone(buzzerPin, 440, 10);
delay(100);
noTone(buzzerPin); // turn off tone function.

digitalWrite(yellowLED, LOW); //Initialize the LED to OFF.
digitalWrite(redLED, LOW); //Initialize the LED to OFF.
}
delay(2000);// wait for SD open file

redLEDstate = flashLED(redLED, redLEDstate); //Turn LED on.
isCreateNewFile = true;

char filename = “C329Ph00.jpg”;
for (uint8_t i = 0; i < 5; i++) {
filename[6] = i / 10 + ‘0’;
filename[7] = i % 10 + ‘0’;
if (! SD.exists(filename)) { // only open a new file if it doesn’t exist
photoFile = SD.open(filename, FILE_WRITE);
photoFile.seek(0);
writtenPictureSize = 0;
Serial.println(F(“opened picture file”));
break; // leave the loop!
}
}
if (! photoFile)
{
Serial.println(F(“couldn’t create file”));
return;
}
Serial.println(F(“File created.”));
Serial.print(F("Logging to: "));
Serial.println(filename);
delay (3000);

if (!camera.sync())
{
Serial.println(F(“Camera Sync Failed”));
return;
}
else
{
Serial.println(F(“Camera ON”));
delay(2000);
}

if (!camera.initialize(CameraC329::BAUD14400, CameraC329::CT_JPEG, CameraC329::PR_160x120, CameraC329::JR_320x240))
{
Serial.println(F(“Camera Failed Initialized”));
return;
}
else
{
Serial.println(F(“Camera Initialized”));
}

if (!camera.setQuality(CameraC329::QL_BEST))
{
Serial.println(F(“Fail to Set quality Good”));
}

if (!camera.takeSnapshot(CameraC329::PT_SNAPSHOT))
{
Serial.println(F(“Fail to take snapshot”));
}

if (!camera.getPicture(CameraC329::PT_JPEG_PREVIEW, &getPicture_callback))
{
Serial.println(F("Fail to get Picture "));
}
else {
Serial.println(F(“Pictured Successfully”));
}
if (!camera.reset(CameraC329::RT_STATE))
photoFile.close();
Serial.println(F(“Finished writing data to file”));

delay(3000);
}

//////////////////////////////////
//RemoveFile ******************************************************
void RemoveFile() {
redLEDstate = flashLED(redLED, redLEDstate); //Turn LED on.
isRemoveFile = true; //set delete flag.

Serial.println(F("(1)Remove the file…")); //debug.
Serial.println(“Removing example.txt…”);
char filename[13];
for (uint8_t i = 0; i < 10; i++)
{
sprintf(filename, “C329Ph%02d.jpg”, i);
SD.remove(filename);
if (SD.exists(filename))
{
Serial.print(F(“Could not delete file”));
Serial.println(filename);
}
else
{
Serial.println(F(“All files deleted”));
}
if (!SD.exists(filename)) {
Serial.println(F(“All .jpg extension files gone”));
}
}

isRemoveFile = false; //set delete flag.
redLEDstate = flashLED(redLED, redLEDstate); //Turn LED off.
}

//flashLED **********************************************************
int flashLED(int ledPin, int ledState) {

// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} //end if.

else {
ledState = LOW;
} //end else.

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
return (ledState); //Return the LED state.

} //end flashLED function.

  Serial.println("Removing example.txt...");
  char filename[13];
  for (uint8_t i = 0; i < 10; i++)
  {
    sprintf(filename, "C329Ph%02d.jpg", i);
    SD.remove(filename);
    if (SD.exists(filename))
    {
      Serial.print(F("Could not delete file"));
      Serial.println(filename);
    }
    else
    {
      Serial.println(F("All files deleted"));
    }
    if (!SD.exists(filename)) {
      Serial.println(F("All .jpg extension files gone"));
    }
  }

That Serial.print() statement does NOT describe what the code is doing.

What do your Serial.print() statements tell you is happening? Not that I’d believe any of them, but at least that provide traceability of your code.

Thanks for your comments. The Serial.print() is telling that if the files deleted or still exists.

This code is working just fine if I separate the Void CreateNewFile() and the Void RemoveFile() into two individual sketches; Unfortunately, if I combine them into one sketch. It only runs and stops at the end of Void CreateNewFile(). it won't continue to the void RemoveFile() loop. I am not really sure how to fix it. I'd appreciate if you can help. Thanks