ArduCAM / Arduino

This is ArduCAM library for Arduino boards
MIT License
479 stars 351 forks source link

Unable to save images to SD card #535

Open Mizuki645 opened 3 years ago

Mizuki645 commented 3 years ago

Hello, I am working on a project on a college.

I am using OV5642 and would like to save the images I have taken to an SD card.

I ran the code listed on this page, but the camera does not even start. https://github.com/ArduCAM/Arduino/blob/master/ArduCAM/examples/mini/ArduCAM_Mini_5MP_Plus_Multi_Capture2SD/ArduCAM_Mini_5MP_Plus_Multi_Capture2SD.ino

The camera works fine when I run the examples mini code without the SD card shield installed.

How can I help?

Thank you.

Mizuki645 commented 3 years ago

@ArduCAM @ArducamSupport
Hello, I cannot initialize the SD card. What should I do? Also, I would like to know the minimum exposure time for each pixel.

I tried to save the image to the SD card using the following program, but was unable to do so.

` // ArduCAM Mini demo (C)2018 Lee // Web: http://www.ArduCAM.com // This program is a demo of how to use the enhanced functions // This demo was made for ArduCAM_Mini_5MP_Plus. // It can continue shooting and store it into the SD card in JPEG format // The demo sketch will do the following tasks // 1. Set the camera to JPEG output mode. // 2. Capture a JPEG photo and buffer the image to FIFO // 3.Write the picture data to the SD card // 5.close the file //You can change the FRAMES_NUM count to change the number of the picture. //IF the FRAMES_NUM is 0X00, take one photos //IF the FRAMES_NUM is 0X01, take two photos //IF the FRAMES_NUM is 0X02, take three photos //IF the FRAMES_NUM is 0X03, take four photos //IF the FRAMES_NUM is 0X04, take five photos //IF the FRAMES_NUM is 0X05, take six photos //IF the FRAMES_NUM is 0X06, take seven photos //IF the FRAMES_NUM is 0XFF, continue shooting until the FIFO is full //You can see the picture in the SD card. // This program requires the ArduCAM V4.0.0 (or later) library and ArduCAM_Mini_5MP_Plus // and use Arduino IDE 1.6.8 compiler or above

/////////////////////////////////////////////////////////////////////////////// /// INCLUDES ///////////////////////////////////////////////////////////////////////////////

include

include

include

include

include "memorysaver.h"

/////////////////////////////////////////////////////////////////////////////// /// CONSTANTS ///////////////////////////////////////////////////////////////////////////////

define LOGFILE "logfile.txt"

define BUF_SIZE 256

define FRAMES_NUM 0x00

define SD_CS 4

//定義と偽を決定 const int CS = 7; bool is_sd_ready = false; bool is_header = false; int total_time = 0;

//モジュールを定義

if defined (OV5640_MINI_5MP_PLUS)

ArduCAM myCAM(OV5640, CS);

else

ArduCAM myCAM(OV5642, CS);

endif

uint8_t read_fifo_burst(ArduCAM myCAM); //ファイル File logfile; char buffer[BUF_SIZE]; File fileReader; File fileWriter; char logFilename[] = "CAM5SD.LOG"; char imgFilename[] = "IMG001.JPG";

/////////////////////////////////////////////////////////////////////////////// /// SETUP ///////////////////////////////////////////////////////////////////////////////

void setup() { uint8_t vid, pid; uint8_t temp;

Wire.begin();

pinMode(SD_CS, OUTPUT);
Serial.begin(9600); //TODO: Disable the serial thing on final code maybe

// set the CS as an output:
pinMode(CS, OUTPUT);
pinMode(SS, OUTPUT);
digitalWrite(CS, HIGH);

//指定したレジスタアドレスに、指定したデータの値を書き込む
SPI.begin();
myCAM.write_reg(0x07, 0x80);
delay(100);
myCAM.write_reg(0x07, 0x00);
delay(100);

myCAM.write_reg(ARDUCHIP_TEST1, 0x55);
temp = myCAM.read_reg(ARDUCHIP_TEST1);
if (temp != 0x55) {
    log("SPI interface Error!");
    while (1);
} 

else {
    log("SPI interface OK.");
} 

delay(250);

myCAM.wrSensorReg16_8(0xff, 0x01);
myCAM.rdSensorReg16_8(OV5642_CHIPID_HIGH, &vid);
myCAM.rdSensorReg16_8(OV5642_CHIPID_LOW, &pid);

if ((vid != 0x56) || (pid != 0x42)) {
    log("ACK CMD Can't find OV5642 module!");
    while (1);
}

else {
    log("ACK CMD OV5642 detected.");
}

delay(250);

//Change to JPEG capture mode and initialize the OV5640 module
myCAM.set_format(JPEG);
myCAM.InitCAM();
myCAM.clear_fifo_flag();
myCAM.write_reg(ARDUCHIP_FRAMES, FRAMES_NUM);

delay(250);

//SD controller Init
log("Initializing SD card...");
is_sd_ready = SD.begin(SD_CS);
if (is_sd_ready) {
  log("SD card initialized.");
} 

else {
  log("Could'nt initialize SD card..."); 
}

delay(250);

//Log file reset
if (is_sd_ready) {
  log("Resetting log file...");
  SD.remove(LOGFILE);
} 

delay(250);

//Opening test file in WRITE mode
if (is_sd_ready) {
  logfile = SD.open(LOGFILE, O_CREAT | O_WRITE | O_APPEND);

  if (!logfile) {
    log("Couldn't open SD card to write log information");
    while (1);
  }

  log("SD card ready for logging...");
}

log("Setup finished.");

}

/////////////////////////////////////////////////////////////////////////////// /// LOOP ///////////////////////////////////////////////////////////////////////////////

void loop() { myCAM.flush_fifo(); myCAM.clear_fifo_flag(); myCAM.OV5642_set_JPEG_size(OV5642_320x240); //Find if we need to set it always or just onecc in the setup

//Start capture
log("CAM Capture Start...");
myCAM.start_capture();

total_time = millis();
while (!myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK));

//log("CAM Capture Done.");
total_time = millis() - total_time;
log("Capture total_time used (in miliseconds): " + String(total_time)); //FIXME: Log not sent to SD card this time

total_time = millis();

if (is_sd_ready && read_fifo_burst(myCAM)) {
  total_time = millis() - total_time;
  log("Save capture total_time used (in miliseconds): " + String(total_time));
}

//Clear the capture done flag
myCAM.clear_fifo_flag();

log("Waiting...");
delay(100000);

}

/////////////////////////////////////////////////////////////////////////////// /// FUNCTIONS ///////////////////////////////////////////////////////////////////////////////

void log(String content) { memset(buffer, 0, BUF_SIZE); strcat(buffer, content.c_str()); if (Serial) { Serial.println(buffer); } if (logfile) { strcat(buffer, "\n"); write_sd_file(); } }

uint8_t read_fifo_burst(ArduCAM myCAM) { uint8_t temp = 0, temp_last = 0; uint32_t length = myCAM.read_fifo_length();

if (Serial) {
  Serial.print(F("The fifo length is: "));
  Serial.println(length, DEC); 
}

if (length >= MAX_FIFO_SIZE) //8M
{
    log("Over size.");
    return false;
}

if (length == 0) //0 kb
{
    log("Size is 0.");
    return false;
}

static int i = 0;
static int k = 0;
char str[16];
File outFile;
byte buf[256];
myCAM.CS_LOW();
myCAM.set_fifo_burst();//Set fifo burst mode
//temp = SPI.transfer(0x00); //This first fix mentionned in faq doesn't change anything
i = 0;

while (length--) {
    temp_last = temp;
    temp = SPI.transfer(0x00);
    //Read JPEG data from FIFO
    if ((temp == 0xD9) && (temp_last == 0xFF)) //If find the end ,break while,
    {
        buf[i++] = temp;  //save the last  0XD9
        //Write the remain bytes in the buffer
        myCAM.CS_HIGH();
        if(!outFile) {
          log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        }
        outFile.write(buf, i);
        //Close the file
        outFile.close(); //FIXME: No data pushed to the file on the SD card
        log("Image write OK");
        is_header = false;
        myCAM.CS_LOW();
        myCAM.set_fifo_burst();
        i = 0;
    }
    if (is_header == true) {
        //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);
            outFile.flush();
            i = 0;
            buf[i++] = temp;
            myCAM.CS_LOW();
            myCAM.set_fifo_burst();
        }
    } else if ((temp == 0xD8) & (temp_last == 0xFF)) {
        is_header = true;
        myCAM.CS_HIGH();
        //Create a avi file
        k = k + 1;
        itoa(k, str, 10);
        strcat(str, ".jpg");

        //Open the new file
        log("Opening following file for write : " + String(imgFilename));
        outFile = SD.open(imgFilename, FILE_WRITE); //Use initial functions again
        if (!outFile) {
            log("File open failed. Stopping...");
            while (1);
        }
        myCAM.CS_LOW();
        myCAM.set_fifo_burst();
        buf[i++] = temp_last;
        buf[i++] = temp;
    }
}
myCAM.CS_HIGH();
log("Writing success.");

return true;

}

void write_sd_file() { //Writing a new line of text int bytesWritten = logfile.write((uint8_t *) buffer, strlen(buffer)); logfile.flush (); if (Serial) { Serial.println(String(bytesWritten) + " bytes have been written to the SD card!"); } } `