Missing out something on Chip Select

Hi, I would like to know if where am I missing something because I have this code, and after the first loop the program wont Proceed. I suspect that it is on the disabling of the CS/SS of the shield(maybe not or another probable cause). So here's the code

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include "RTClib.h"
#include <SD.h>
#include <Wire.h>
#include <Thread.h>
#include <ArduCAM.h>
#include "memorysaver.h"

#define ADAFRUIT_CC3000_IRQ   3 
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIVIDER);

#define WLAN_SSID       ""          
#define WLAN_PASS       ""
#define WLAN_SECURITY   WLAN_SEC_WPA2

#define IDLE_TIMEOUT_MS  3000
#define WEBSITE      "192.168.10.100"
#define WEBPAGE      "/DesignProject/pages/add.php"


#define SD_CS 4
#define SPI_CS 53
const int pirPin = 48;
uint32_t ip = 3232238180;

ArduCAM myCAM(OV2640,SPI_CS);
RTC_DS1307 rtc;

void setup() {
  uint8_t temp;
  uint8_t vid,pid;
  #if defined (__AVR__)
    Wire.begin(); 
  #endif
  #if defined(__arm__)
    Wire1.begin(); 
  #endif
  Serial.begin(115200);
  Serial.println("Program Start!\n");
  
  /*
  Serial.println(F("Initialization done!\n"));
  //Initializing RTC
  Serial.println(F("Initializating RTC!"));
  if (!rtc.begin()){
    Serial.println(F("Failed to Initialize RTC!\n"));
  }
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  Serial.println(F("Initialization of RTC sucessful!\n"));
  */
  
  SPI.begin();
  //ARDUCAM
  Serial.println("ArduCAM Start!");
  // set the SPI_CS as an output:

  pinMode(SPI_CS, OUTPUT);
  pinMode(ADAFRUIT_CC3000_CS, OUTPUT);
  
  //Check if the ArduCAM SPI bus is OK
  myCAM.write_reg(ARDUCHIP_TEST1, 0x55);
  temp = myCAM.read_reg(ARDUCHIP_TEST1);
  if(temp != 0x55)
  {
    Serial.println("SPI interface Error!\n");
    while(1);
  }
  
  myCAM.write_reg(ARDUCHIP_MODE, 0x00);

  //Check if the camera module type is OV2640
  myCAM.rdSensorReg8_8(OV2640_CHIPID_HIGH, &vid);
  myCAM.rdSensorReg8_8(OV2640_CHIPID_LOW, &pid);
  if((vid != 0x26) || (pid != 0x42))
    Serial.println("Can't find OV2640 module!\n");
  else
    Serial.println("OV2640 detected\n");

  //Change to BMP capture mode and initialize the OV2640 module  
  myCAM.set_format(JPEG);
  myCAM.InitCAM();
  myCAM.OV2640_set_JPEG_size(OV2640_1600x1200);
  //Initialization of SD Card
  Serial.println(F("Initializing SD Card!"));
  if (!SD.begin(SD_CS)) {
    Serial.println(F("Initialization failed!\n"));
    while(1);
  }
  
}

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

  if(digitalRead(pirPin)==HIGH)
  {
    digitalWrite(SD_CS, HIGH);
    //Flush the FIFO
    myCAM.flush_fifo();
    //Clear the capture done flag
    myCAM.clear_fifo_flag();
    //Start capture
    myCAM.start_capture();
    Serial.println("Start Capture");

    while(!(myCAM.read_reg(ARDUCHIP_TRIG) & CAP_DONE_MASK));

    Serial.println("Capture Done!");
    //myCAM.CS_HIGH();
    //Construct a file name
    k = k + 1;
    itoa(k, str, 10); 
    strcat(str,".jpg");        //Generate file name
    //myCAM.write_reg(ARDUCHIP_MODE, 0x00);      //Switch to MCU, freeze the screen 
    //GrabImage(str);

    //Open the new file
    outFile = SD.open(str, O_WRITE | O_CREAT | O_TRUNC);
    if (! outFile)
    {
      Serial.println("Open file failed");
      return;
    } else {
      Serial.println("Open file Successful");
    }
    total_time = millis();
    i = 0;
    myCAM.CS_LOW();
    myCAM.set_fifo_burst();
    temp = SPI.transfer(0x00);
    //
    //Read JPEG data from FIFO
    while ( (temp != 0xD9) | (temp_last != 0xFF))
    {
      temp_last = temp;
      temp = SPI.transfer(0x00);
  
      //Write image data to buffer if not full
      if (i < 256)
        buf[i++] = temp;
      else
      {
        //Write 256 bytes image data to file
        myCAM.CS_HIGH();
        outFile.write(buf, 256);
        i = 0;
        buf[i++] = temp;
        myCAM.CS_LOW();
        myCAM.set_fifo_burst();
      }
    }
    //Write the remain bytes in the buffer
    if (i > 0)
    {
      myCAM.CS_HIGH();
      outFile.write(buf, i);
    }
    //Close the file
    outFile.close();
    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();
    //Initializing CC3000(Wifi Shield)
    Serial.println(F("Initializating Wifi Shield!"));
    if (!cc3000.begin())
    {
      Serial.println(F("Couldn't begin()! Check your wiring?"));
      while(1);
    }
    
    //connect to SSID; if err, display failed
    Serial.println(F("Connecting..."));
    if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
      Serial.println(F("Failed to Connect!"));
      while(1);
    }
    //else display connected
    Serial.println(F("Connected!"));
    
    //request for DHCP to find IP address
    //will be deleted after configuring router
    //Don't leave until IP address, DNS, and gateway is assigned
    Serial.println(F("Request DHCP"));
    while (!cc3000.checkDHCP())
    {
      delay(100);
    }
    while (! displayConnectionDetails()) {
      delay(1000);
    }
    myCAM.CS_LOW();
    digitalWrite(ADAFRUIT_CC3000_CS, HIGH);
    }
}
bool displayConnectionDetails(void)
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
  
  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }
}

I have an Arduino Mega2560
CC3000 Wifi Shield (CC3000 WIFI Hardware Manual.pdf - Google Drive)
ArduCam mini OV2640

Arducam Connections
CS 53(Mega)
SCL SCL(Mega)
SDA SDA(Mega)
5v 5v(Wifi Shield)
GND GND(Wifi Shield)
MOSI, MISO, SCK (Connected to external ICSP Headers above the Wifi Shield as shown on the link above for the Wifi Shield)

This is the output on the Serial Monitor Below and stops at the connection details

Program Start!

ArduCAM Start!
OV2640 detected

Initializing SD Card!
Start Capture
Capture Done!
Open file Successful
Total time used:4088 millisecond
Initializating Wifi Shield!
Connecting...
Connected!
Request DHCP

IP Addr: 192.168.10.101
Netmask: 255.255.255.0
Gateway: 192.168.10.254
DHCPsrv: 0.0.0.0
DNSserv: 192.168.10.198

I have no special idea, except adding further debug output to the statements following the last output, and on enter/exit loop().

The names CS_HIGH and CS_LOW are meaningless, should better read CS_ON and CS_OFF, but tell that the library maintainers :frowning:

I could imagine few reasons when loop() really doesn't execute any more: memory corruption (buffer overrun...), or stack overflow, or resource (timer?) conflicts. Again topics more for the library maintainers than about your code. But I'm not an expert, perhaps somebody else already used the modules and libraries and can help?

you have no "digitalWrite(SD_CS,LOW)" (should there be one?)