Print files

G'day everyone! I was wondering if someone could shed some light for me. I have a Seriall TTL Jpg camera taking a pic everytime a button is pressed, stores the image on SD in chronological order ex: IMAGE00.jpg, IMAGE01.jpg, IMAGE02.jpg and so on. when the image is saved to the SD card I can then call up the sd card to open a file and print file to serial monitor. BAMMM that's all good to go. What I am needing to do is when the file is stored on SD card after button is pressed, cam takes pic, stores in chronological order, i need to print that file (the new Pic everytime) to serial. In my sketch I can only open the file that I name in the code ex: File myfile = SD.open("IMAGE00.txt");......but I need to open the newest pic taken everytime it gets saved on SD card. Cam snaps pic, saves to SD, open that file on serial monitor. Is there someway to save on SD then open the newest file? Here is the part that I have written already, I have tried to search extensively on internet with no success, any help would be appreciated.

#include <Adafruit_VC0706.h>
#include <SD.h>
#include <SPI.h>
#include <Adafruit_NeoPixel.h>

#define SS_SD  4  //SD card CS pin

const int buttonPin = 6;
int buttonState = 0;  // variable for reading the pushbutton status

Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, 7, NEO_GRB + NEO_KHZ400);

// Using hardware serial on DUE: camera TX to RX1 DUE,camera RX to TX1 DUE
Adafruit_VC0706 cam = Adafruit_VC0706(&Serial1);

void setup()
{
  //Serial3.begin(57600);  //Seperate RX and TX
  Serial.begin(57600);  //Monitor
  pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input:

  strip.begin();  //initialize Light
  delay(5);  //Have to put this in or PIXEL stays on
  strip.show();  //set light to off

  Serial.println("TEST");

  // Try to locate the camera
  if (cam.begin())
  {
    Serial.println("Camera Found:");
  }
  else
  {
    Serial.println("No camera found?");
    return;
  }

  // see if the card is present and can be initialized:
  if (!SD.begin(SS_SD))
  {
    Serial.println("SD Card init failed...");
    strip.setPixelColor(0, strip.Color(255, 255, 0)); // bright yellow color.
    strip.show();
    return;
  }

  cam.reset();  //Delay Needed or it won't work
  delay(500);

  //cam.setImageSize(VC0706_640x480);
  //cam.setImageSize(VC0706_320x240);
  cam.setImageSize(VC0706_160x120);
  delay(100);

  uint8_t imgsize = cam.getImageSize();
  Serial.print("Image size: ");
  if (imgsize == VC0706_640x480) Serial.println("640x480");
  if (imgsize == VC0706_320x240) Serial.println("320x240");
  if (imgsize == VC0706_160x120) Serial.println("160x120");

  cam.setCompression(255);  //srts cam comp 0-255 (255 being most)
  delay(500);
}

void loop()
{
  Trigger();
}

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

  if (buttonState == HIGH)
  {
    Serial.println("Snap in 1 sec...");
    delay(1000);

    takePicture();
    //delay (500);
    //takePicture();
    delay(2000);
    sendToMonitor();
  }
}

void takePicture()
{
  if (! cam.takePicture())
  {
    Serial.println("Failed to snap!");
  }
  else
  {
    Serial.println("Picture taken!");
  }

  // Create an image with the name IMAGExx.JPG

  char filename[13];
  strcpy(filename, "IMAGE00.jpg");
  for (int i = 0; i < 100; i++)
  {
    filename[5] = '0' + i / 10;
    filename[6] = '0' + i % 10;

    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename))
    {
      break;
    }
  }

  // Open the file for writing
  File imgFile = SD.open(filename, FILE_WRITE);

  Serial.print("create ");
  Serial.println(filename);

  // Get the size of the image (frame) taken
  uint16_t jpglen = cam.frameLength();
  Serial.print("Read Bytes ");
  Serial.print(jpglen, DEC);
  Serial.print(" byte image.");

  int32_t time = millis();
  pinMode(8, OUTPUT);
  byte wCount = 0; // For counting # of writes
  //cam.getPicture(jpglen);

  uint8_t *buffer;
  while (jpglen != 0)
  {
    uint8_t bytesToRead = min(64, jpglen);
    buffer = cam.readPicture(bytesToRead);
    imgFile.write(buffer, bytesToRead);
    //Serial.print("Read ");  Serial.print(bytesToRead, DEC); Serial.println(" bytes");
    jpglen -= bytesToRead;
  }

  imgFile.close();

  time = millis() - time;
  Serial.println("Done!");
  Serial.print("Took ");
  Serial.print(time);
  Serial.println(" ms");
  Serial.println();

  //Resume Camera
  cam.resumeVideo();
}

void sendToMonitor ()
{
  File myfile = SD.open("IMAGE00.jpg");  //need to change this so I can open newest image

  if (myfile)
  {

    Serial.println("Start");

    while (myfile.available())
    {
      Serial.write(myfile.read());
    }
    Serial.println("Finished");
    myfile.close();

    Serial.println();
    Serial.println("FILE SENT TO MONITOR");

    strip.setPixelColor(0, strip.Color(0, 150, 0)); // bright green color.
    strip.show();
    delay(4000);

    // turn LED off:
    strip.setPixelColor(0, strip.Color(0, 0, 0));
    strip.show();
  }
  else
  {
    Serial.println("error opening the file");
    strip.setPixelColor(0, strip.Color(255, 0, 0)); // Red color.
    strip.show();
  }
}

You need to dynamically setup the filename string.

Something alike the following should work (not tested):

char filename[13] = {"IMAGExx.JPG"};
if(counter < 10) {
  filename[5] = '0';
  filename[6] = '0' + counter;
} else {
  uint8_t tmp = counter / 10;
  filename[5] = '0' + tmp;
  filename[6] = '0' + counter - tmp * 10;
}

Note: There of are smarter ways doing this.

derVernichter!!!!!Wow a great Reply and quick!! I will give it a test and see if this helps! Again thank you so much for the reply!!!!

Sorry derVernichter, because I am still learning as I go, would be kind enough to show me where to place this snippet of code you wrote? It's still a bit confusing to me because I still have to tell arduino to open sd, then look for most recent file, and send that to serial monitor.....the most recent being in chronological order, 01, 02, 03 etc....what if I have already sent 00, 01 and I now need to send 02 cause it's the latest....Sorry but a bit confusing while I am learning and my hands are going up in the air! Thanks again

Oh taking another look into your source, you already have functionality for getting the next free filename in the takePicture method (I must have overlooked this, sorry). You may use the exact same for getting the most recent file.

char filename[] = {"IMAGExx.JPG\0\0\0"}; //you in fact want to have 14 bytes as the filenames may be 13 characters long + additional terminating \0

void getFileName(char* filename, uint8_t buflen, boolean getFree) {
  char lastFilename[buflen];
 
  for (int i = 0; i < 100; i++)
  {
    memcpy(lastFilename, filename, buflen);
    filename[5] = '0' + i / 10;
    filename[6] = '0' + i % 10;

    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename))
    {
      break;
    }
  }

  if(!getFree)
    memcpy(filename, lastFilename, buflen); //copy back the previous used filename in case you do not want a free name
  return filename;
}

Thank you again! Where would I place this code?, because I see in your code you do not tell arduino to "open Sd Card" to read any files, this is where it becomes confusing for me. I am snapping a picture....gets stored on SD, I tell Arduino to open SD and find the last file the cam just snapped, if I were to snap 2 pictures 500ms apart would arduino only send the last file created, or would I put your code in somewhere that after the pic was taken it immediately send to serial, then another pic is taken and that one gets sent and so on. I very much apologize for my curiosity in completing this, as this is the last step in my code and it'd driving me mad for 2 weeks.....

So I have ran this over and over, and tried all combinations...but I am not swift enough to know how or where to put this code inside my sketch....If someone could be of assistance that would be wonderful...

but I am not swift enough to know how or where to put this code inside my sketch....If someone could be of assistance that would be wonderful...

Someplace in your code, you open a file with a hardcoded name. Replace that code with the code that was provided. SHOW WHAT YOU DID and explain what happens when you compile/link/upload/run the code that is not what you expect/want/desire to have happen, or what does not happen that you do want to have happen.

Ok Paul thank you