Camera to SD Issues - Timing? Processing?

I’m working on a motion activated (PIR) camera arducam that stores to SD card. So far so good. I recently ran across an example where someone used an EyeFi card to transfer files over wifi as they came in. I was able to modify my code to write to a subdirectory that could be read (DCIM/Cameraname/img_XXX.jpg)by the eyefi. It seems to work, but this is where I ran into some trouble. When I run the sketch is captures sometimes 1 image or 2 or even three, but then it snags, stops processing and/or starts to output junk text to the serial monitor.

I’m kind of guessing there’s a processing speed hiccup going on and I’ve tried to insert some delays in various points as a random stab at giving the processor some time to deal with each piece, but no luck. Wondering if anyone is interested in looking at my code and offering some suggestions.

In terms of hardware, I’m using an Arducam mini, an Arduino Uno and an adafruit datalogging shield for sd storage. I’m pasting a portion of the loop code where the camera is activated and captures images.

/*
void loop()
{
  char str[8];
  File outFile;
  byte buf[256];
  static int i = 0;
  static int k = 0;
  static int n = 0;
  uint8_t temp, temp_last;
  uint8_t start_capture = 0;
  int total_time = 0;

  if (digitalRead(pirPin) == HIGH)                                                 //If a motion is detected
  {
    digitalWrite(ledPin, HIGH);                                                    //signal that a motion is detectedstate
    digitalWrite(irPin, HIGH);                                                     //turn on IR LEDs
    if (lockLow)
    {
      lockLow = false;                                                             // We enter in "motion" mode

      Serial.println("---");                                                       //record and display start time of motion detection
      Serial.print("motion detected at ");
      Serial.print(millis() / 1000);
      Serial.println(" sec");
      start_capture = 1;
      delay(100);

      //-------------------------
      //Camera capture general
      //-------------------------

      if (start_capture)
      {

        myCAM.flush_fifo();                                                        //Flush the FIFO
        myCAM.clear_fifo_flag();                                                   //Clear the capture done flag
        myCAM.start_capture();                                                     //Start capture
        Serial.println("Start Capture");
      }
      while (!myCAM.get_bit(ARDUCHIP_TRIG , CAP_DONE_MASK));
      Serial.println("Capture Done!");
      char filename[26];                                                            //Construct a file name
      for (int i = 0; i < 1000; i++) {
        sprintf(filename, "DCIM/CANON999/IMG_%03d.JPG", i);

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

      outFile = SD.open(filename, O_WRITE | O_CREAT | O_TRUNC);                    //Open the new file
      if (! outFile)
      {
        Serial.println("open file failed");
        return;
      }
      total_time = millis();
      i = 0;
      myCAM.CS_LOW();
      myCAM.set_fifo_burst();
      temp = SPI.transfer(0x00);

      while ( (temp != 0xD9) | (temp_last != 0xFF))                                 //Read JPEG data from FIFO
      {
        temp_last = temp;
        temp = SPI.transfer(0x00);

        if (i < 256)                                                                 //Write image data to buffer if not full
          buf[i++] = temp;
        else
        {
          myCAM.CS_HIGH();                                                           //Write 256 bytes image data to file
          outFile.write(buf, 256);
          i = 0;
          buf[i++] = temp;
          myCAM.CS_LOW();
          myCAM.set_fifo_burst();
        }
      }
      if (i > 0)                                                                      //Write the remain bytes in the buffer
      {
        myCAM.CS_HIGH();
        outFile.write(buf, i);
      }

      outFile.close(); //Close the file

      total_time = millis() - total_time;
      Serial.print("Total time used:");
      Serial.print(total_time, DEC);
      Serial.println(" millisecond");                                                  //Clear the capture done flag
      myCAM.clear_fifo_flag();
      start_capture = 0;                                                               //Clear the start capture flag

      //-------------------------

      delay(200);
      lastshot = millis();
      burst = true;                                                                     //Now we go in burst mode,ie picture will be taken every 5S
    }
    if (burst)                                                                          //Once the motion has been detected and a picture taken, we go into this mode to keep taking pictures every 5s until the motion ends
    {
      delay (burstInterval);
      lockLow = false;                                                                  // We enter in "motion" mode
      Serial.println("---");                                                            //record and display start time of motion detection
      Serial.print("motion detected at ");
      Serial.print(millis() / 1000);
      Serial.println(" sec");
      start_capture = 1;
      delay(100);

PIR_Arducam4_burst_IR_eyefi.ino (13.7 KB)