Radio Shack Camera Shield camsd.pde sketch - writing jpeg files to a SD

Does anyone know how to modify this sketch so the camera auto increments the file name by 1 instead of replacing the "temp.jpg" with the same name? (The sketch below is only a portion of the total sketch. It's the part that writes the file).

I would like the camera to capture: "temp1.jpg, temp2.jpg, temp3.jpg..." and so on. The camera script detects motion, and then snaps a new shot - which is great. However, I would like to see all of the shots in progression versus the way it works now -- by replacing the last photo with the most recent photo under the same file name.

#ifdef sdCamera

/*******************************************************************************

  • Function Name : capture_photo
  • Description : capture a photo and store the file named temp.jpg into SD
  • Input : None
  • Output : None
  • Return : None
    *******************************************************************************/
    void capture_photo(){

// Check to see if the file exists:
// if exists,delete the file:
if(sd.exists("temp.jpg")) sd.remove("temp.jpg");

// open a new empty file for write at end like the Native SD library
if (!myFile.open("temp.jpg", O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening temp.jpg for write failed");
}

// close the file:
myFile.close();

VC0706_compression_ratio(63);
delay(100);

VC0706_frame_control(3);
delay(10);

VC0706_frame_control(0);
delay(10);
rx_ready=false;
rx_counter=0;

Serial.end(); // clear all rx buffer
delay(5);

Serial.begin(115200);

//get frame buffer length
VC0706_get_framebuffer_length(0);
delay(10);
buffer_read();

//while(1){};

// store frame buffer length for coming reading
frame_length=(VC0706_rx_buffer[5]<<8)+VC0706_rx_buffer[6];
frame_length=frame_length<<16;
frame_length=frame_length+(0x0ff00&(VC0706_rx_buffer[7]<<8))+VC0706_rx_buffer[8];

vc_frame_address =READ_DATA_BLOCK_NO;

myFile.open("temp.jpg", O_RDWR);
while(vc_frame_address<frame_length){
VC0706_read_frame_buffer(vc_frame_address-READ_DATA_BLOCK_NO, READ_DATA_BLOCK_NO);
delay(9);

//get the data with length=READ_DATA_BLOCK_NObytes
rx_ready=false;
rx_counter=0;
buffer_read();

// write data to temp.jpg
myFile.write(VC0706_rx_buffer+5,READ_DATA_BLOCK_NO);

//read next READ_DATA_BLOCK_NO bytes from frame buffer
vc_frame_address=vc_frame_address+READ_DATA_BLOCK_NO;

}

// get the last data
vc_frame_address=vc_frame_address-READ_DATA_BLOCK_NO;

last_data_length=frame_length-vc_frame_address;

VC0706_read_frame_buffer(vc_frame_address,last_data_length);
delay(9);
//get the data
rx_ready=false;
rx_counter=0;
buffer_read();

myFile.write(VC0706_rx_buffer+5,last_data_length);

myFile.close();

}

#endif

Does anyone know how to modify this sketch so the camera auto increments the file name by 1 instead of replacing the "temp.jpg" with the same name? (The sketch below is only a portion of the total sketch. It's the part that writes the file).

Sure. Replace this:

   // Check to see if the file exists:
   // if exists,delete the file:
   if(sd.exists("temp.jpg")) sd.remove("temp.jpg");
      
   // open a new empty file for write at end like the Native SD library
    if (!myFile.open("temp.jpg", O_RDWR | O_CREAT | O_AT_END)) {

With code that does what you want.

sprintf() might prove useful. Some global variable added to that snippet might be useful.

Thanks. I figured thats the part of the code to fix. Just don't know how. There's not a lot of good info out there on auto-incrementing file names for Arduino. Most of the examples I've seen are part of a larger, much more complex application.

If you would like my sketch for RS_Camera, joeharper007@aol.com

  • After much frustration and persistence, the Radio Shack components finally
  • produced a functioning camera and with some improvements, saving multiple JPG files.
  • An Arduino Uno board, a Radio Shack camera shield, an SD disk, a
  • four wire connecting cable, and a USB printer cable are all that is needed.
  • For convenience in testing, a TV with a composite input and a 9 volt battery
  • with cable to power the Uno board are necessary.
  • To connect everything:
  • The white wire of the 4 wire cable goes from TXD on the camera board to Digital pin 0 (RX) on the Uno board.
  • The brown wire of the 4 wire cable goes from RXD on the camera board to Digital pin 1 (TX) on the Uno board.
  • The red wire goes to +5 volts, and the black wire to ground.
  • The major problem was getting the SD chip select set properly.
  • Later in this sketch, you are given a choice to set the SD chip select.
  • In the line: const uint8_t SdChipSelect = SS;
  • When using the camera shield, it should be changed to
  • const uint8_t SdChipSelect = 4;
  • Even though using the camera shield, the selection for the Ethernet board works fine.
  • BEFORE UPLOADING, DISCONNECT THE TXD AND THE RXD WIRES!!!
  • AFTER UPLOADING, DISCONNECT THE USB CABLE AND RECONNECT THE TXD AND RXD WIRES.
  • RECONNECT THE USB CABLE, THEN PRESS THE RESET BUTTON ON THE UNO BOARD.
  • THE BUILT-IN MOTION FEATURE OF THE CAMERA WILL NOW TAKE PICS AND SAVE THEM FOR YOU!!!

Also,
// Ignore theses two .h files, they don't present a problem as I first thought.
// Arduino seems to find them somewhere after the Arduino software installation.

// THIS IS IMPORTANT!!!
// THE FOLLOWING FILES WHEN DOWNLOADED AND UNZIPPED SHULD BE EXTRACTED TO THE FOLLOWING DIRECTORY!!!
// C:\Program Files (x86)\Arduino\libraries
// OTHERWISE, WHEN COMPING THIS SKETCH, ARDUINO WON'T KNOW WHERE TO FIND THE SUPPORT FILES.

#include <Sd2Card.h> // ***** SD card driver from SD Card Shield | Seeed Studio Wiki or SD card shield supplier *****
#include <SdFat.h> // ***** FAT file system from SD Card Shield | Seeed Studio Wiki or SD card shield supplier *****

Here is the modified code:
void capture_photo(){
char myFileName[16];
int myFileNr=1;

// create a new file each and every time
while (myFileNr != 0) {
sprintf(myFileName, "CCC%03d.jpg", myFileNr);
if (sd.exists(myFileName) == false) break;
myFileNr++;
}

// open a new empty file for write at end like the Native SD library
if (!myFile.open(myFileName, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening ???.jpg for write failed");
}

If you would like my sketch for RS_Camera, joeharper007@aol.com

  • After much frustration and persistence, the Radio Shack components finally
  • produced a functioning camera and with some improvements, saving multiple JPG files.
  • An Arduino Uno board, a Radio Shack camera shield, an SD disk, a
  • four wire connecting cable, and a USB printer cable are all that is needed.
  • For convenience in testing, a TV with a composite input and a 9 volt battery
  • with cable to power the Uno board are necessary.
  • To connect everything:
  • The white wire of the 4 wire cable goes from TXD on the camera board to Digital pin 0 (RX) on the Uno board.
  • The brown wire of the 4 wire cable goes from RXD on the camera board to Digital pin 1 (TX) on the Uno board.
  • The red wire goes to +5 volts, and the black wire to ground.
  • The major problem was getting the SD chip select set properly.
  • Later in this sketch, you are given a choice to set the SD chip select.
  • In the line: const uint8_t SdChipSelect = SS;
  • When using the camera shield, it should be changed to
  • const uint8_t SdChipSelect = 4;
  • Even though using the camera shield, the selection for the Ethernet board works fine.
  • BEFORE UPLOADING, DISCONNECT THE TXD AND THE RXD WIRES!!!
  • AFTER UPLOADING, DISCONNECT THE USB CABLE AND RECONNECT THE TXD AND RXD WIRES.
  • RECONNECT THE USB CABLE, THEN PRESS THE RESET BUTTON ON THE UNO BOARD.
  • THE BUILT-IN MOTION FEATURE OF THE CAMERA WILL NOW TAKE PICS AND SAVE THEM FOR YOU!!!

Also,
// Ignore theses two .h files, they don't present a problem as I first thought.
// Arduino seems to find them somewhere after the Arduino software installation.

// THIS IS IMPORTANT!!!
// THE FOLLOWING FILES WHEN DOWNLOADED AND UNZIPPED SHULD BE EXTRACTED TO THE FOLLOWING DIRECTORY!!!
// C:\Program Files (x86)\Arduino\libraries
// OTHERWISE, WHEN COMPING THIS SKETCH, ARDUINO WON'T KNOW WHERE TO FIND THE SUPPORT FILES.

#include <Sd2Card.h> // ***** SD card driver from SD Card Shield | Seeed Studio Wiki or SD card shield supplier *****
#include <SdFat.h> // ***** FAT file system from SD Card Shield | Seeed Studio Wiki or SD card shield supplier *****

Here is the modified code:
void capture_photo(){
char myFileName[16];
int myFileNr=1;

// create a new file each and every time
while (myFileNr != 0) {
sprintf(myFileName, "CCC%03d.jpg", myFileNr);
if (sd.exists(myFileName) == false) break;
myFileNr++;
}

// open a new empty file for write at end like the Native SD library
if (!myFile.open(myFileName, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening ???.jpg for write failed");
}