sd file count stops when it should continue

Hi, I am new at this and need a point in the right direction. My sketch attached stops creating / writing files to the sd card after the count reaches 509 to 512. I believe it has to do with the bit of code left out. after the FILE WRITE.

Here is my code:

#include <Adafruit_VC0706.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
#include <SD.h>


// comment out this line if using Arduino V23 or earlier
#include <SoftwareSerial.h>

// uncomment this line if using Arduino V23 or earlier
// #include <NewSoftSerial.h>



// SD card chip select line varies among boards/shields:
// Adafruit SD shields and modules: pin 10
// Arduino Ethernet shield: pin 4
// Sparkfun SD shield: pin 8
// Arduino Mega w/hardware SPI: pin 53
// Teensy 2.0: pin 0
// Teensy++ 2.0: pin 20
#define chipSelect 10

#define PIRPin 8 // Motion Detect Pin

File myFile;

char myFileName[50];
int myFileNr = 1;


RTC_DS1307 RTC;  // define the Real Time Clock object
char buffer[50];

// Pins for camera connection are configurable.
// With the Arduino Uno, etc., most pins can be used, except for
// those already in use for the SD card (10 through 13 plus
// chipSelect, if other than pin 10).
// With the Arduino Mega, the choices are a bit more involved:
// 1) You can still use SoftwareSerial and connect the camera to
//    a variety of pins...BUT the selection is limited.  The TX
//    pin from the camera (RX on the Arduino, and the first
//    argument to SoftwareSerial()) MUST be one of: 62, 63, 64,
//    65, 66, 67, 68, or 69.  If MEGA_SOFT_SPI is set (and using
//    a conventional Arduino SD shield), pins 50, 51, 52 and 53
//    are also available.  The RX pin from the camera (TX on
//    Arduino, second argument to SoftwareSerial()) can be any
//    pin, again excepting those used by the SD card.
// 2) You can use any of the additional three hardware UARTs on
//    the Mega board (labeled as RX1/TX1, RX2/TX2, RX3,TX3),
//    but must specifically use the two pins defined by that
//    UART; they are not configurable.  In this case, pass the
//    desired Serial object (rather than a SoftwareSerial
//    object) to the Adafruit_VC0706 constructor.

// Using SoftwareSerial (Arduino 1.0+) or NewSoftSerial (Arduino 0023 & prior):
#if ARDUINO >= 100
// On Uno: camera TX connected to pin 2, camera RX to pin 3:
SoftwareSerial cameraconnection = SoftwareSerial(2, 3);
// On Mega: camera TX connected to pin 69 (A15), camera RX to pin 3:
//SoftwareSerial cameraconnection = SoftwareSerial(69, 3);
#else
NewSoftSerial cameraconnection = NewSoftSerial(2, 3);
#endif
Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);

// Using hardware serial on Mega: camera TX conn. to RX1,
// camera RX to TX1, no SoftwareSerial object is required:
//Adafruit_VC0706 cam = Adafruit_VC0706(&Serial1);
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
  DateTime now = RTC.now();

  // return date using FAT_DATE macro to format fields
  *date = FAT_DATE(now.year(), now.month(), now.day());

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(now.hour(), now.minute(), now.second());
}

void setup() {

  // When using hardware SPI, the SS pin MUST be set to an
  // output (even if not connected or used).  If left as a
  // floating input w/SPI on, this can cause lockuppage.
#if !defined(SOFTWARE_SPI)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  if (chipSelect != 53) pinMode(53, OUTPUT); // SS on Mega
#else
  if (chipSelect != 10) pinMode(10, OUTPUT); // SS on Uno, etc.
#endif
#endif

  Serial.begin(9600);
  Serial.println("VC0706 Camera test");

  pinMode(PIRPin, INPUT);    // initialize the PIR pin as an input

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }

  // Try to locate the camera
  if (cam.begin()) {
    Serial.println("Camera Found:");
  } else {
    Serial.println("No camera found?");
    return;
  }
  // Print out the camera version information (optional)
  char *reply = cam.getVersion();
  if (reply == 0) {
    Serial.print("Failed to get version");
  } else {
    Serial.println("-----------------");
    Serial.print(reply);
    Serial.println("-----------------");
  }

  // Set the picture size - you can choose one of 640x480, 320x240 or 160x120
  // Remember that bigger pictures take longer to transmit!

  //cam.setImageSize(VC0706_640x480);        // biggest
  cam.setImageSize(VC0706_320x240);        // medium
  //cam.setImageSize(VC0706_160x120);          // small

  // You can read the size back from the camera (optional, but maybe useful?)
  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");


  //  Motion detection system can alert you when the camera 'sees' motion!
  //  cam.setMotionDetect(true);           // turn it on
  cam.setMotionDetect(false);        // turn it off   (default)

  // You can also verify whether motion detection is active!
  Serial.print("Motion detection is ");
  if (cam.getMotionDetect())
    Serial.println("ON");
  else
    Serial.println("OFF");


  Wire.begin();
  if (!RTC.begin()) {
    Serial.println("RTC failed");
    while (1);
  };
  // set date time callback function
  SdFile::dateTimeCallback(dateTime);

  // following line sets the RTC to the date & time this sketch was compiled
   RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // RTC.adjust(DateTime(2015, 10, 10, 11, 29, 0));
  RTC.now(); // Get time from RTC
}


void loop() {
  int n = 0;
  while (1) {

    Serial.println("\r\nPIRPin goes high to take a picture");
    while (digitalRead(PIRPin) == LOW);      //wait for buttonPin status to HIGH

    if (digitalRead(PIRPin) == HIGH) {
      delay(20);                               //Debounce
      if (digitalRead(PIRPin) == HIGH)

      {
        Serial.println("\r\nbegin to take picture");
        delay(200);
        if (n == 0)
          Serial.println("Motion!");
        cam.setMotionDetect(false);

        if (! cam.takePicture())
          Serial.println("Failed to snap!");
        else
          Serial.println("Picture taken!");
      }
      Serial.print("\r\nTaking pictures success ,number : ");
      Serial.println(n);
      n++ ;

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

    myFile = SD.open(myFileName, FILE_WRITE);

    uint16_t jpglen = cam.frameLength();
    Serial.print(jpglen, DEC);
    Serial.println(" byte image");

    Serial.print("Writing image to "); Serial.print(myFileName);

    while (jpglen > 0) {
      // read 32 bytes at a time;
      uint8_t *buffer;
      uint8_t bytesToRead = min(64, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
      buffer = cam.readPicture(bytesToRead);
      myFile.write(buffer, bytesToRead);

      //Serial.print("Read ");  Serial.print(bytesToRead, DEC); Serial.println(" bytes");

      jpglen -= bytesToRead;
    }
    myFile.close();
    myFileNr++;
    Serial.println("...Done!");
    cam.resumeVideo();
    cam.setMotionDetect(true);
    }
  }

I presume that you are using a card formatted in FAT16 in which case there is a limit of 512 files in the root directory.

Pete

thank you Pete

You're welcome. Just for future reference, @fat16lib posted the following info about SD cards:

2 GB or smaller cards should be formatted FAT16. FAT16 has a limit of 512 entries in root. FAT32 has a limit of 65,534 entries in root. FAT16 and FAT32 can have 65,534 entries in sub directories.

Pete