Go Down

Topic: Writing Images to a SD Card help (Read 4826 times) previous topic - next topic

dpy2011

I am using an Arduino Uno to capture images from a LinkSprite Jpeg camera and write them to an SD card. I am using an 2GB SD Card formatted in FAT16. The system works great for about a half of an hour, and then it stops being able to write images. It is not filling up the disk space. (Only 5.31 MB ar being used. When connected via USB, the serial port acts like it is still writing, but after 170 images no new images appear on the card.
Here is the code I am using. I am also implementing a motion sensor and a real time clock for a time stamp.


Code: [Select]

#include <NewSoftSerial.h>
#include <Wire.h>
#include <RTClib.h>
#include <byteordering.h>
#include <fat.h>
#include <FAT16.h>
#include <fat_config.h>
#include <partition.h>
#include <partition_config.h>
#include <sd-reader_config.h>
#include <sd_raw.h>
#include <sd_raw_config.h>
#include <stdio.h>
byte incomingbyte;
NewSoftSerial mySerial(4,5);          // Set Arduino pin 4 and 5 as softserial
long int a=0x0000,j=0,k=0,count=0,i=0,x=0,y=0;                           
uint8_t MH,ML;
boolean EndFlag=0;
FAT TestFile;     
RTC_DS1307 RTC;                             
void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{
  Serial.begin(38400);
  mySerial.begin(38400);
  Wire.begin();
  RTC.begin();
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(9,OUTPUT);//pin 2 is connected to LED to indicate the status of SD card initialization
  //SetImageSizeCmd();
  digitalWrite(2,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(3,LOW);
  digitalWrite(6,LOW);
 
  if(!sd_raw_init())
{
    digitalWrite(2,HIGH);
    while(1);     
}   


}

void loop()

int motion = digitalRead(8);
if (motion == 1)
{
a=0x0000;
j=0;
k=0;
count=0;
i=0;
EndFlag=0;
DateTime now = RTC.now();
char buffer [50];

sprintf(buffer, "%d %d %d    %d %d %d.jpeg", now.month(), now.day(), now.year(), now.hour(), now.minute(), now.second());

// + '/' + now.day() + '/' + now.year() + ' ' + now.hour() + ':' + now.minute() +':' + now.second();

TestFile.initialize();
TestFile.create_file(buffer);
x++;
if(x==10){y++;x=0;}
byte a[32];

     SendResetCmd();
     delay(1000);                            //Wait 2-3 second to send take picture command
     
      SendTakePhotoCmd();

     while(mySerial.available()>0)
      {
        incomingbyte=mySerial.read();
      }   

     TestFile.open();
      while(!EndFlag)
      { 
         j=0;
         k=0;
         count=0;
         SendReadDataCmd();
         delay(20);
          while(mySerial.available()>0)
          {
               incomingbyte=mySerial.read();
               k++;
               if((k>5)&&(j<32)&&(!EndFlag))
               {
               a[j]=incomingbyte;
               if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
               EndFlag=1;                           
               j++;
       count++;
               }
          }
         
        for(j=0;j<count;j++)
        {   if(a[j]<0x10)
         Serial.print("0");
        Serial.print(a[j],HEX);           // observe the image through serial port
         Serial.print(" ");
       }
       
          TestFile.write((char*)a);   
          Serial.println();
     
         i++;
     
      }
    TestFile.close();   
   Serial.print("Finished writing data to file"); 
}
else
{
  delay(100);
 
   Serial.print("no motion");
}
     while(0);
}


void SendResetCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x26, BYTE);
      mySerial.print(0x00, BYTE);
}

void SetImageSizeCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x31, BYTE);
      mySerial.print(0x05, BYTE);
      mySerial.print(0x04, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x19, BYTE);
      mySerial.print(0x22, BYTE);
}

void SetBaudRateCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x24, BYTE);
      mySerial.print(0x03, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0xAE, BYTE);
      mySerial.print(0xC8, BYTE);

}



void SendTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE); 
}


void SendReadDataCmd()
{
      MH=a/0x100;
      ML=a%0x100;
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x32, BYTE);
      mySerial.print(0x0c, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x0a, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(MH, BYTE);
      mySerial.print(ML, BYTE);   
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x20, BYTE);
      mySerial.print(0x00, BYTE); 
      mySerial.print(0x0a, BYTE);
      a+=0x20;     
}

void StopTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE)w
      mySerial.print(0x03, BYTE);       
}


Does anyone know why this code is not able to srite more than 170 images to the card?

robtillaart

Very interesting project, could you tell more about this e.g. a playground article?

Don't know if it is the cause but in the old days a root directory could hold only a fixed number of entries.
If so, two possible solutions:
- Try with a fat32 if the same number of entries are made
- make a subdirectory for every 100 pictures or so.

---

wrt the code, looks good however you could compact all the separate commands with a number of arrays, and one generic function

Code: [Select]

// first byte = length , rest are cmd bytes
byte reset[] = { 0x04, 0x56, 0x00, 0x26, 0x00 };   
byte setImageSize[] = { 0x09, 0x56, 0x00, 0x31,0x05, 0x04, 0x01, 0x00, 0x19, 0x22 };
byte takePhoto[] = {0x05, 0x56, 0x00, 0x36, 0x01, 0x00 }; 

void sendCmd(byte *cmd)
{
  for(int i=1; i<= cmd[0]; i++) mySerial.print(cmd[i], BYTE);
}


Hopes this helpes

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

dpy2011

I cleaned up the code a bit and commented in more to help define what is going on. Most of this code is source code from the LinkSprite camera. I am really new at arduino, so I am having a difficult time telling what it is doing at times.
The system looks like this:
I have a Camera, an SD card breakout board, a Real Time Clock (RTC), and a PIR motion sensor connected to the Arduino Uno. The goal of the system is to take pictures whenever there is motion detected. When motion is detected, then a jpeg file is created with a time stamp from the RTC and the image is saved to the SD card under that file name. It takes the arduino about 7-10 seconds to load the image. (Resolution is set at 120x160). Idealy I am trying to make this system be able to stay somewhere and have surveillance images saved to the card for a long period of time. But as I said the problem comes after picture #170.
Your suggestion to make a subdirrectory sounds interesting, how would i go about doing that? Does that description help describe better what is happening? hopefully this revision of the code is a little clearer.
Code: [Select]

#include <NewSoftSerial.h>
#include <Wire.h>
#include <RTClib.h>
#include <byteordering.h>
#include <fat.h>
#include <FAT16.h>
#include <fat_config.h>
#include <partition.h>
#include <partition_config.h>
#include <sd-reader_config.h>
#include <sd_raw.h>
#include <sd_raw_config.h>
#include <stdio.h>


byte incomingbyte;
NewSoftSerial mySerial(4,5);          // Set Arduino pin 4 and 5 as softserial
long int a=0x0000,j=0,k=0,count=0,i=0,x=0,y=0;                           
uint8_t MH,ML;
boolean EndFlag=0;
FAT TestFile;     
RTC_DS1307 RTC;                             
void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{
  Serial.begin(38400);
  mySerial.begin(38400);
  Wire.begin();
  RTC.begin();
  // all these values are used as VCC or GND for components
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(9,OUTPUT);
  digitalWrite(2,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(3,LOW);
  digitalWrite(6,LOW);
 
  if(!sd_raw_init())
{
    digitalWrite(2,HIGH); //outputs high on pin 2 when card is initialized.
    while(1);     
}   


}

void loop()

int motion = digitalRead(8); //If motion is sensed set "motion" HIGH

if (motion == 1)
  {
  //reset all variables
  a=0x0000;
  j=0;
  k=0;
  count=0;
  i=0;
  EndFlag=0;
  DateTime now = RTC.now(); // Sets "now" with current Date and Time status
 
  //Creates a jpeg file named "mm dd yyyy    hh mm ss.jpeg"
  char buffer [32];
  sprintf(buffer, "%d %d %d  %d %d %d.jpeg", now.month(), now.day(), now.year(), now.hour(), now.minute(), now.second());
 
   TestFile.initialize();
   TestFile.create_file(buffer);
   
  byte a[32];
 
       SendResetCmd();
       delay(1000);   //Wait 1 second to send take picture command
        SendTakePhotoCmd();
       while(mySerial.available()>0)
        {
          incomingbyte=mySerial.read();
        }   
 
       TestFile.open();
        while(!EndFlag)
        { 
           j=0;
           k=0;
           count=0;
           SendReadDataCmd();
           delay(20);
            while(mySerial.available()>0)
            {
                 incomingbyte=mySerial.read();
                 k++;
                 if((k>5)&&(j<32)&&(!EndFlag))
                 {
                 a[j]=incomingbyte;
                 if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
                 EndFlag=1;                           
                 j++;
         count++;
                 }
            }
           
          for(j=0;j<count;j++)
          {   if(a[j]<0x10)
           Serial.print("0");
           Serial.print(a[j],HEX);           // observe the image through serial port
           Serial.print(" ");
         }
         
            TestFile.write((char*)a);   
            Serial.println();
       
           i++;
       
        }
      TestFile.close();   
     Serial.print("Finished writing data to file"); 
  }
else
  {
    // if there is no motion, delay 100 ms before checking again.
    delay(100); 
    Serial.print("no motion");
  }
     while(0);
}


void SendResetCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x26, BYTE);
      mySerial.print(0x00, BYTE);
}

void SetImageSizeCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x31, BYTE);
      mySerial.print(0x05, BYTE);
      mySerial.print(0x04, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x19, BYTE);
      mySerial.print(0x22, BYTE);
}

void SetBaudRateCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x24, BYTE);
      mySerial.print(0x03, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0xAE, BYTE);
      mySerial.print(0xC8, BYTE);

}



void SendTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE); 
}


void SendReadDataCmd()
{
      MH=a/0x100;
      ML=a%0x100;
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x32, BYTE);
      mySerial.print(0x0c, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x0a, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(MH, BYTE);
      mySerial.print(ML, BYTE);   
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x20, BYTE);
      mySerial.print(0x00, BYTE); 
      mySerial.print(0x0a, BYTE);
      a+=0x20;     
}

void StopTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x03, BYTE);       
}

robtillaart

Quote
Your suggestion to make a subdirrectory sounds interesting, how would i go about doing that? Does that description help describe better what is happening? hopefully this revision of the code is a little clearer.


You have to take a look at your SD/fat library for examples. My SDFat library has a makeDir function

As you are new to Arduino I strongly advice to spend some times in the tutorial an reference section and try the examples shown there. That gives you a basic understanding of how the Arduino programming works.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

CAtiger

I am trying to do the same thing except I am using the PING))) ultrasonic sensor. I have 2 problems:
1) the program will not run continuously for every time the sensor is within 12 inches of an object.
2) I cannot open the files written on the SD card.

I have tried all loops from for, to while, to do...while, and even added loop again. Still will not repeat the picture taking process.

Any suggestions?

Code: [Select]
#include <NewSoftSerial.h>
#include <byteordering.h>
#include <fat.h>
#include <FAT16.h>
#include <fat_config.h>
#include <partition.h>
#include <partition_config.h>
#include <sd-reader_config.h>
#include <sd_raw.h>
#include <sd_raw_config.h>

const int pingPin = 8;          //ping pin
byte incomingbyte;
NewSoftSerial mySerial(2,3);          // Set Arduino pin 2 and 3 as softserial
long int a=0x0000,j=0,k=0,count=0,i=0,x=0,y=0;                           
uint8_t MH,ML;
boolean EndFlag=0;
FAT TestFile;     
                             

void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SetCompressionRatioCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{
  Serial.begin(9600);
  mySerial.begin(38400);
  pinMode(13,OUTPUT);       //pin 13 is connected to LED to indicate the status of SD card initialization
  if(!sd_raw_init())
     {
      digitalWrite(13,HIGH);
      while(1);     
     }
   
    TestFile.initialize();
    //Serial.println("initializing........");
    TestFile.create_file("img03.jpg");     // create a new jpeg file   
}

void loop()
{
  long duration, inches, cm;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);


  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
 
   if(inches <= 12)
    {
      Serial.print("MOTION DETECTED: ");
      Serial.print(inches);
      Serial.print("in ");
      Serial.println();
   
     byte a[32];
     SendResetCmd();
     delay(1000);                            //Wait 1 second to send take picture command
     
     SendTakePhotoCmd();
     while(mySerial.available()>0)
      {
        incomingbyte=mySerial.read();
      }   
     TestFile.open();
      while(!EndFlag)
      { 
         j=0;
         k=0;
         count=0;
         SendReadDataCmd();
         delay(20);
          while(mySerial.available()>0)
          {
               incomingbyte=mySerial.read();
               k++;
               if((k>5)&&(j<32)&&(!EndFlag))
               {
               a[j]=incomingbyte;
               if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
               EndFlag=1;                           
               j++;
        count++;
               }
          }
         
        for(j=0; j<count; j++)
        {   if(a[j]<0x10)
         Serial.print("0");
        Serial.print(a[j],HEX);           // observe the image through serial port
         Serial.print(" ");
       }
       
          TestFile.write((char*)a);   
          Serial.println();
     
         i++;
     
      }
      TestFile.close();   
      Serial.println("Finished writing data to file"); 

      while(1);
    }
   
    else
    {
      delay(1000); //wait one second before loop
      loop();
    }
 
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

void SendResetCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x26, BYTE);
      mySerial.print(0x00, BYTE);
}
void SetImageSizeCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x31, BYTE);
      mySerial.print(0x05, BYTE);
      mySerial.print(0x04, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x19, BYTE);
      mySerial.print(0x22, BYTE);
}
void SetBaudRateCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x24, BYTE);
      mySerial.print(0x03, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0xAE, BYTE);
      mySerial.print(0xC8, BYTE);
}
void SetCompressionRatioCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x31, BYTE);
      mySerial.print(0x05, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x12, BYTE);
      mySerial.print(0x04, BYTE);
      mySerial.print(0x36, BYTE);
}
void SendTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE);
}

void SendReadDataCmd()
{
      MH=a/0x100;
      ML=a%0x100;
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x32, BYTE);
      mySerial.print(0x0c, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x0a, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(MH, BYTE);
      mySerial.print(ML, BYTE);   
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x20, BYTE);
      mySerial.print(0x00, BYTE); 
      mySerial.print(0x0a, BYTE);
      a+=0x20;     
}
void StopTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x03, BYTE);   
}

scott-42

You can't use Pin 13 for the LED when using the SDCard as it is also using pin 13 for data communications. So take out the pin 13 stuff in your setup() and your SD code should start working.

CAtiger

I changed the led pin for the SD card, but my program will not loop. Even before adding the PING, the camera would not take pictures continuously. Is there any way to modify the code to make this loop repeat?

scott-42


... my program will not loop ... Is there any way to modify the code to make this loop repeat?

If your post is the latest version of your code, then I think your problem is with the
Code: [Select]
while(1); line in loop(). Also you don't ever need to call loop() within loop() at the very end after you have that delay.

CAtiger

This is the output of running the file :
Code: [Select]
Finished writing data to file
MOTION DETECTED: 7in
Finished writing data to file
MOTION DETECTED: 7in
Finished writing data to file
MOTION DETECTED: 7in
Finished writing data to file


I believe the code will not repeat because of the initialization of the SD card. I am new to Arduino and I am reading tons and tons of information on the different libraries.

Code: [Select]
TestFile.initialize();
    //Serial.println("initializing........");
    TestFile.create_file("img002.txt");     // create a new jpeg file save as .txt then change to .jpg 

Will this snippet of code cause a problem in writing many files since it is initializing this TestFile or because the file has the same name and it may not be able to adapt the name, i.e "img002[1], img002[2]??


Code: [Select]
#include <NewSoftSerial.h>
#include <byteordering.h>
#include <fat.h>
#include <FAT16.h>
#include <fat_config.h>
#include <partition.h>
#include <partition_config.h>
#include <sd-reader_config.h>
#include <sd_raw.h>
#include <sd_raw_config.h>

const int pingPin = 8;          //ping pin
byte incomingbyte;
NewSoftSerial mySerial(2,3);          // Set Arduino pin 2 and 3 as softserial
long int a=0x0000,j=0,k=0,count=0,i=0,x=0,y=0;                           
uint8_t MH,ML;
boolean EndFlag=0;
FAT TestFile;     
                             

void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SetCompressionRatioCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{
  Serial.begin(9600);
  mySerial.begin(38400);
  pinMode(9,OUTPUT);       //pin 9 is connected to LED to indicate the status of SD card initialization
  if(!sd_raw_init())
     {
      digitalWrite(9,HIGH);
      while(1);     
     }
   
    TestFile.initialize();
    //Serial.println("initializing........");
    TestFile.create_file("img002.txt");     // create a new jpeg file save as .txt then change to .jpg 
}

void loop()
{
  long duration, inches, cm;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);


  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
 
   if(inches <= 12)
    {
      Serial.print("MOTION DETECTED: ");
      Serial.print(inches);
      Serial.print("in ");
      Serial.println();
   
     byte a[32];
     SendResetCmd();
     delay(1000);                            //Wait 1 second to send take picture command
     
     SendTakePhotoCmd();
     while(mySerial.available()>0)
      {
        incomingbyte=mySerial.read();
      }   
     TestFile.open();
      while(!EndFlag)
      { 
         j=0;
         k=0;
         count=0;
         SendReadDataCmd();
         delay(20);
          while(mySerial.available()>0)
          {
               incomingbyte=mySerial.read();
               k++;
               if((k>5)&&(j<32)&&(!EndFlag))
               {
               a[j]=incomingbyte;
               if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
               EndFlag=1;                           
               j++;
        count++;
               }
          }
         
        for(j=0; j<count; j++)
        {   if(a[j]<0x10)
         Serial.print("0");
        Serial.print(a[j],HEX);           // observe the image through serial port
         Serial.print(" ");
       }
       
          TestFile.write((char*)a);   
          Serial.println();
     
         i++;
     
      }
      TestFile.close();   
      Serial.println("Finished writing data to file"); 

      while(0);
    }
   
    else
    {
      delay(1000); //wait one second before loop
    }
 
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

void SendResetCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x26, BYTE);
      mySerial.print(0x00, BYTE);
}
void SetImageSizeCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x31, BYTE);
      mySerial.print(0x05, BYTE);
      mySerial.print(0x04, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x19, BYTE);
      mySerial.print(0x22, BYTE);
}
void SetBaudRateCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x24, BYTE);
      mySerial.print(0x03, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0xAE, BYTE);
      mySerial.print(0xC8, BYTE);
}
void SetCompressionRatioCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x31, BYTE);
      mySerial.print(0x05, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x12, BYTE);
      mySerial.print(0x04, BYTE);
      mySerial.print(0x36, BYTE);
}
void SendTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE);
}

void SendReadDataCmd()
{
      MH=a/0x100;
      ML=a%0x100;
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x32, BYTE);
      mySerial.print(0x0c, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x0a, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(MH, BYTE);
      mySerial.print(ML, BYTE);   
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x20, BYTE);
      mySerial.print(0x00, BYTE); 
      mySerial.print(0x0a, BYTE);
      a+=0x20;     
}
void StopTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x03, BYTE);   
}

scott-42


This is the output of running the file :
Code: [Select]
Finished writing data to file
MOTION DETECTED: 7in
Finished writing data to file
MOTION DETECTED: 7in
Finished writing data to file
MOTION DETECTED: 7in
Finished writing data to file


I believe the code will not repeat because of the initialization of the SD card. I am new to Arduino and I am reading tons and tons of information on the different libraries.

Code: [Select]
TestFile.initialize();
    //Serial.println("initializing........");
    TestFile.create_file("img002.txt");     // create a new jpeg file save as .txt then change to .jpg 

Will this snippet of code cause a problem in writing many files since it is initializing this TestFile or because the file has the same name and it may not be able to adapt the name, i.e "img002[1], img002[2]??


Your code is repeating but you are correct in assuming that you need to fix your code to create a new file each time it needs to write out a new image. I don't think the SD library handles name collisions (I haven't looked though) so you should take care of constructing your image names appropriately and checking for name collisions yourself.

Go Up