How to make the file name being dynamic for my project?

hi,somebody can help me solve this problem about sd card coding.tq so much

problem: 1st =when i save 1st picture into memory card is complete.
2nd=when i save 2nd photo into memory card ,the 1st picture will automatic deleted.
3rd=how can make my sd card will follow step 1,2,3,4,5… until memory full ? somebody can help me edit the coding.tqtq

THIS IS MY ORIGINAL LIBRARY

#include <NewSoftSerial.h>

#include <byteordering.h>
#include <fat.h>
#include <FAT16.h>
#include <fat_config.h>
#include <partition.h>
#include <partition_config.h>
#include <sd-reader_config.h>
#include <sd_raw.h>
#include <sd_raw_config.h>

const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
byte incomingbyte;
NewSoftSerial mySerial(4,5); // Set Arduino pin 4 and 5 as softserial
long int a=0x0000,j=0,k=0,count=0,i=0;
uint8_t MH,ML;
boolean EndFlag=0;
FAT TestFile;

void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{
Serial.begin(38400);
mySerial.begin(38400);
pinMode(3,OUTPUT); //pin 2 is connected to LED to indicate the status of SD card initialization

if(!sd_raw_init())
{
digitalWrite(2,HIGH);
while(1);
}
TestFile.initialize();
TestFile.create_file(“1.jpg”); // create a new jpeg file

}

void loop()
{
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {

byte a[32];

SendResetCmd();
delay(4000); //Wait 2-3 second to send take picture command

SendTakePhotoCmd();

while(mySerial.available()>0)
{
incomingbyte=mySerial.read();
}

TestFile.open();
while(!EndFlag)
{
j=0;
k=0;
count=0;
SendReadDataCmd();
delay(20);
while(mySerial.available()>0)
{
incomingbyte=mySerial.read();
k++;
if((k>5)&&(j<32)&&(!EndFlag))
{
a[j]=incomingbyte;
if((a[j-1]==0xFF)&&(a[j]==0xD9)) //tell if the picture is finished
EndFlag=1;
j++;
count++;
}
}

for(j=0;j<count;j++)
{
if(a[j]<0x10)
Serial.print(“0”);
Serial.print(a[j],HEX); // observe the image through serial port
Serial.print(" ");
}

TestFile.write((char*)a);
Serial.println();

i++;

}
TestFile.close();
Serial.print(“Finished writing data to file”);

while(1);
}
}

//Send Reset command
void SendResetCmd()
{
mySerial.print(0x56, BYTE);
mySerial.print(0x00, BYTE);
mySerial.print(0x26, BYTE);
mySerial.print(0x00, BYTE);
}

//Send take picture command
void SendTakePhotoCmd()
{
mySerial.print(0x56, BYTE);
mySerial.print(0x00, BYTE);
mySerial.print(0x36, BYTE);
mySerial.print(0x01, BYTE);
mySerial.print(0x00, BYTE);
}

//Read data
void SendReadDataCmd()
{
MH=a/0x100;
ML=a%0x100;
mySerial.print(0x56, BYTE);
mySerial.print(0x00, BYTE);
mySerial.print(0x32, BYTE);
mySerial.print(0x0c, BYTE);
mySerial.print(0x00, BYTE);
mySerial.print(0x0a, BYTE);
mySerial.print(0x00, BYTE);
mySerial.print(0x00, BYTE);
mySerial.print(MH, BYTE);
mySerial.print(ML, BYTE);
mySerial.print(0x00, BYTE);
mySerial.print(0x00, BYTE);
mySerial.print(0x00, BYTE);
mySerial.print(0x20, BYTE);
mySerial.print(0x00, BYTE);
mySerial.print(0x0a, BYTE);
a+=0x20; //address increases 32£¬set according to buffer size
}

void StopTakePhotoCmd()
{
mySerial.print(0x56, BYTE);
mySerial.print(0x00, BYTE);
mySerial.print(0x36, BYTE);
mySerial.print(0x01, BYTE);
mySerial.print(0x03, BYTE);
}

You do not change the filename “1.jpg” so it get overwritten each time you .open it in loop.

First: do not SHOUT at us! Second: read the sticky post at the top of this forum! Third: use code tags for posting code, otherwise your code may be manipulated by the forum system.

After you wrote your picture, you're freezing your Arduino:

 while(1);

You write your array without limiting the size:

      TestFile.write((char*)a);

change that to:

      TestFile.write((char*)a, count);

You have an end check which doesn't work in all conditions. Think of a[31] holding 0xFF. You then start a new 32 byte block. If the first byte read (a[0]) is 0xD9, you won't recognize it as the end marker.

long int a=0x0000,j=0,k=0,count=0,i=0;

Why do these variables have to be long integers (32bit)? They never go outside the 8bit range and can be defined of type byte, making your code faster and less memory consuming. Why is "a" defined twice (once as a long int and then as a byte array)?

actually i not shout :cold_sweat: ~ just wanna ask anyone can help meT.T~i so sorry :disappointed_relieved: actually i not really understand all code inside,i search in internet and try do edit it.have u any solution to help me to improve the system. or u can help me/teach me create snap photos and all save in memory card? pls~ :)

choong87:
actually i not shout :cold_sweat:

All caps title means shouting.

~ just wanna ask anyone can help meT.T~i so sorry :disappointed_relieved:

Start by reading the thread you’ve been asked to read.