Pages: [1]   Go Down
Author Topic: SD card not saving complete data  (Read 943 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Using the Arduino Uno and DFRobot SD module and LinkSprite JPEG TTL camera, I am taking a picture and saving it to the SD card. The data is shown in HEX in the serial monitor. Everything that is printed to the serial monitor is not saving to the SD card. Is there a problem with the code, the SD card, or the capabilities of the Uno?

SD: 2GB PNY MicroSD

snippet of code where the problem lies:
Code:
void setup()
{
  Serial.begin(9600);
  mySerial.begin(38400);
  pinMode(13,OUTPUT);       //pin 2 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("image11.jpg");     // create a new jpeg file
}
void loop()
{     
  byte a[32];
     SendResetCmd();
     delay(3000);                            //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"); 
 
     while(1);
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8699
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I expect that this function:

Code:
TestFile.write((char*)a);

expects a character string and so only writes until it reaches a null character.  It seems likely that a JPEG image is likely to contain some null characters.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

This piece of code will flush the serial buffer and copy the last received byte in incomingByte.... so your missing bytes .....

Logged

Rob Tillaart

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  pinMode(13,OUTPUT);       //pin 2 is connected to LED to indicate the status of SD card initialization
If pin 2 is connected to the LED, why are you setting the mode for pin 13?

Code:
     delay(3000);                            //Wait 1 second to send take picture command
Is your watch slow?

If you can't make the obvious comments correct, do they serve any useful purpose? If not, why are they there?

Logged

Pages: [1]   Go Up
Jump to: