Arduino + TTL Camera +SD

I'm sorry that i didn't post some needed information
i'm using the dfrobot sd module (http://www.diybin.com/products/DFRobot-SD-Module.html),with pin 3 as my SS pin
and using this ls-y201 as my ttl camera (德源科技),with TX1,RX1 on arduino mega 2560 board
any idea will be appreciated,thanks a lot

byte incomingbyte;
NewSoftSerial mySerial(4,5);                     //Configure pin 4 and 5 as soft serial port
int a=0x0000,j=0,k=0,count=0;                    //Read Starting address       
uint8_t MH,ML;
boolean EndFlag=0;
                               
void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{ 
  Serial.begin(19200);
  mySerial.begin(38400);
}

void loop() 
{
     SendResetCmd();
     delay(4000);                               //After reset, wait 2-3 second to send take picture command
      
      SendTakePhotoCmd();

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

      }   
      byte a[32];
      
      while(!EndFlag)
      {  
         j=0;
         k=0;
         count=0;
         SendReadDataCmd();

         delay(25);
          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))      //Check if the picture is over
               EndFlag=1;                           
               j++;
	       count++;
               }
          }
         
          for(j=0;j<count;j++)
          {   if(a[j]<0x10)
              Serial.print("0");
              Serial.print(a[j],HEX);
              Serial.print(" ");
          }                                       //Send jpeg picture over the serial port
          Serial.println();
      }      
     while(1);
}

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

//Send take picture command
void SendTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE);  
}

//Read data
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;                            //address increases 32£¬set according to buffer size
}

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

In your code and in this there is difference in delays value why? And Follow the rule "Divide and conquer" 8)
First step get picture and show the value of "a[j]" on serial port. Copy those value and save them manually on file.

If file output not broken Include Sd Card library and use it at proper place

else Use this library http://www.sparkfun.com/datasheets/Sensors/Imaging/JPEG%20Camera%20Libraries.zip :wink: .

One very important point left by viewing your picture "to avoid problem with 640X480", change the definition of starting address variable "a" from int to long int.

cheers

Cybernetician
thx for your advice and i'll try it
the reason why i have the different delay time because I try to figure out the reason of this problem
I try to change the baudrate, the delay time in different part!

Cybernetician
thx for your advice and i'll try it
the reason why i have the different delay time because I try to figure out the reason of this problem
I try to change the baudrate, the delay time in different part!

Most probably your Problem: "int a" 8)

Solution:change the definition of starting address variable "a" from int to long int.

cheers

If you want to take one picture, and capture the output, why is the code in loop()? Put the code in setup() and leave loop() empty. Get rid of that while(1) loop.

There should be NO delay()s. You know how much data you need to read. Keep looping until you have read the required amount of data. Do not diddle around with delay(). The delay()s are causing the serial buffer to overflow before you get around to reading all the data.

As you can see from the results, you can keep up for a while, but, eventually you fall behind and start loosing data.

pauls,thanks for your advice and i''ll pay more attention on this,i should think more clearly and carefully about that,but not just try to use some weird ways to find out the solution,thanks a lot!!
Cybernetician,you are right and i get the right picture that i need,thank you very much: )

It's my pleasure 8)

Enjoy "Art of Logic"

I am not able to generate the image :0
Below is my code.. Now since the manual says the image is JPEG format.. what other processing is required.

#include <NewSoftSerial.h>

byte incomingbyte;
NewSoftSerial mySerial(2,3);                     //Configure pin 4 and 5 as soft serial port
long int a=0x0000,j=0,k=0,count=0;                    //Read Starting address       
uint8_t MH,ML;
boolean EndFlag=0;
                               
void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{ 
  Serial.begin(38400);
  mySerial.begin(38400);
}

void loop() 
{
     SendResetCmd();
     delay(3000);                               //After reset, wait 2-3 second to send take picture command
      
      SendTakePhotoCmd();

     while(mySerial.available()>0)  //I DONT UNDERTAND !!!!!! wont this return the last byte in incomingbyte ??
      {
        incomingbyte=mySerial.read();

      }   
      byte a[32];
      
      while(!EndFlag)
      {  
         j=0;
         k=0;
         count=0;
         SendReadDataCmd();

         delay(25);
          while(mySerial.available()>0)
          {
               incomingbyte=mySerial.read();
               k++;
               if((k>5)&&(j<32)&&(!EndFlag)) //why is it k<5 ??
               {
               a[j]=incomingbyte;
               if((a[j-1]==0xFF)&&(a[j]==0xD9))      //Check if the picture is over
               EndFlag=1;                           
               j++;
	       count++;
               }
          }
         
          for(j=0;j<count;j++)
          {   if(a[j]<0x10)         //why 0x10 ? why do we check for this (>16) condition
              Serial.print("0");    //why print this 0
              Serial.print(a[j],HEX);
              Serial.print(" ");           //why print this " space "
          }                                       //Send jpeg picture over the serial port
          Serial.println();
      }      
     while(1);   // ??? is that even necessary its already in void loop()
}

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

//Send take picture command
void SendTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE);  
}

//Read data
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;                            //address increases 32£¬set according to buffer size
}

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

The output in the serial monitor is displayed in HEX which is presumably the JPEG format.
So what do I do next ??

What do i do with the HEX data obtained now (See the attached file for the serial output). Once i am able to construct an JPEG image out of this I want to transmit it on wireless radio (however that is in the later stage)

Does anyone have a routine/code written that can change these HEX values to required JPEG values and all we would have to do is to change the extension and the JPEG should appear.

TEST.txt (9.99 KB)

     while(mySerial.available()>0)  //I DONT UNDERTAND !!!!!! wont this return the last byte in incomingbyte ??

No, the available() method only returns the number of bytes to be read.

        incomingbyte=mySerial.read();

This gets one of them.

      }

This ends to the loop that has thrown away random amounts of data. The part that isn't clear is why you have this loop here at all. Apparently is it just to ignore any response from the command to take a photo. Like, maybe, the camera reports "Done" or "Not tonight, I have a headache".

Seems to me that it might be an idea to actually print the response.

     while(1);   // ??? is that even necessary its already in void loop()

So loop() only iterates once, and you only take one picture.

shaikhmuneer23:
I am not able to generate the image :0
Below is my code.. Now since the manual says the image is JPEG format.. what other processing is required.

#include <NewSoftSerial.h>

byte incomingbyte;
NewSoftSerial mySerial(2,3);                     //Configure pin 4 and 5 as soft serial port
long int a=0x0000,j=0,k=0,count=0;                    //Read Starting address      
uint8_t MH,ML;
boolean EndFlag=0;
                             
void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{
 Serial.begin(38400);
 mySerial.begin(38400);
}

void loop()
{
    SendResetCmd();
    delay(3000);                               //After reset, wait 2-3 second to send take picture command
     
     SendTakePhotoCmd();

while(mySerial.available()>0)  //I DONT UNDERTAND !!!!!! wont this return the last byte in incomingbyte ??
     {
       incomingbyte=mySerial.read();

}  
     byte a[32];
     
     while(!EndFlag)
     {  
        j=0;
        k=0;
        count=0;
        SendReadDataCmd();

delay(25);
         while(mySerial.available()>0)
         {
              incomingbyte=mySerial.read();
              k++;
              if((k>5)&&(j<32)&&(!EndFlag)) //why is it k<5 ??
              {
              a[j]=incomingbyte;
              if((a[j-1]==0xFF)&&(a[j]==0xD9))      //Check if the picture is over
              EndFlag=1;                          
              j++;
      count++;
              }
         }
       
         for(j=0;j<count;j++)
         {   if(a[j]<0x10)         //why 0x10 ? why do we check for this (>16) condition
             Serial.print("0");    //why print this 0
             Serial.print(a[j],HEX);
             Serial.print(" ");           //why print this " space "
         }                                       //Send jpeg picture over the serial port
         Serial.println();
     }      
    while(1);   // ??? is that even necessary its already in void loop()
}

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

//Send take picture command
void SendTakePhotoCmd()
{
     mySerial.print(0x56, BYTE);
     mySerial.print(0x00, BYTE);
     mySerial.print(0x36, BYTE);
     mySerial.print(0x01, BYTE);
     mySerial.print(0x00, BYTE);  
}

//Read data
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;                            //address increases 32£¬set according to buffer size
}

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




The output in the serial monitor is displayed in HEX which is presumably the JPEG format.
So what do I do next ??

What do i do with the HEX data obtained now (See the attached file for the serial output). Once i am able to construct an JPEG image out of this I want to transmit it on wireless radio (however that is in the later stage)

Does anyone have a routine/code written that can change these HEX values to required JPEG values and all we would have to do is to change the extension and the JPEG should appear.

"Hello Paul :slight_smile:

Just found that you were online so dropping a line.
I posted a question in the JPEG camera post, if you find time please take a look and help me out.

Thanks again.
Muneer"

I am not Paul. But i know he can solve your question. 8)

Sorry Cybernetician
I just signed up to this forum and I am not really a forum type of guy. I was referring to you itself.
Ignore my newbie bahaviour.

Thank you Paul for the response. However you have missed out the HEX to JPEG part.
Please find my my attached in the last post to see the HEX file in the serial.

Thank you.

shaikhmuneer23:
What do i do with the HEX data obtained now (See the attached file for the serial output).

Instead of printing the bytes out using an ascii hex representation, just write the sequence of raw bytes to a file. Up to you to get them from the Arduino to a file. The file would contain the raw data received from the camera. It sounds as if you expect that data to constitute a valid JPEG image, and if you're right then you would be able to open the saved file in any application that can deal with that image format.

Does anyone have a routine/code written that can change these HEX values to required JPEG values and all we would have to do is to change the extension and the JPEG should appear.

The data appears consistent with JPEG data. Just store the bytes you are printing in a file with the .jpeg extension, and you should be able to see a picture when you open the file.

Thank you PeterH and Paul.

I was expecting to copy the HEX values to notepad and then changing the extension to .jpg.
But of course that did not work :cold_sweat:

So what if I write these HEX bytes to .txt using a SD library in Arduino 0.22>
How would this data written to the SD card be any different from what I am pasting directly from the monitor to notepad. While I write data to SD is it processing it in some form so that when i just change the extension from .txt to jpg the image should be there ??

Please bear with me...my knowledge is limited in this area.

Thanks

I was expecting to copy the HEX values to notepad and then changing the extension to .jpg.
But of course that did not work

Of course not. The string representation of a HEX value and the hex value itself are two completely different things. The file you attached has the string representation of some hex values in it, not some hex values.

So what if I write these HEX bytes to .txt using a SD library in Arduino 0.22

That is what you need to do.

How would this data written to the SD card be any different from what I am pasting directly from the monitor to notepad.

See above.

While I write data to SD is it processing it in some form so that when i just change the extension from .txt to jpg the image should be there ??

The SD library can open a file with any 3 letter extension, so just make it jpg when you create the file. Then there is no need to change it.

I was expecting to copy the HEX values to notepad and then changing the extension to .jpg.
But of course that did not work

PaulS : Of course not. The string representation of a HEX value and the hex value itself are two completely different things. The file you attached has the string representation of some hex values in it, not some hex values.

By hex value itself do you mean the HEX representation with a prefix of 0x
e.g. 3E --> 0x3E. Is this what you mean.

Because before I save it to the SD i want to assure what i need to write to the SD and in which format so I will imitate the same using notepad and see for myself if the trick works.

By hex value itself do you mean the HEX representation with a prefix of 0x
e.g. 3E --> 0x3E. Is this what you mean.

No I do not. You read a byte from the camera:

               incomingbyte=mySerial.read();

Write that byte, without converting it to a string, with or without the leading 0x, to the file.

Hint: use the write() method, not the print() method.

* Linksprite */

#include <NewSoftSerial.h>

byte incomingbyte;
NewSoftSerial mySerial(2,3);                     //Configure pin 4 and 5 as soft serial port
int long a=0x0000,j=0,k=0,count=0;                    //Read Starting address       
uint8_t MH,ML;
boolean EndFlag=0;
                               
void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{ 
  Serial.begin(9600);
  mySerial.begin(38400);
}

void loop() 
{    
     SendResetCmd();
     delay(3000); //ChangeBaudRate();
     
     //SetImageSizeCmd();  
     //SendResetCmd();
     //delay(3000);                               //After reset, wait 2-3 second to send take picture command
      
      SendTakePhotoCmd();

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

      }   
      byte a[32];
      
      while(!EndFlag)
      {  
         j=0;
         k=0;
         count=0;
         SendReadDataCmd();

         delay(25);
          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))      //Check if the picture is over
               EndFlag=1;                           
               j++;
	       count++;
               }
          }
         
          for(j=0;j<count;j++)
          {   if(a[j]<0x10)
              Serial.print("0");
              Serial.print(a[j],HEX);
              Serial.print(" ");
          }                                       //Send jpeg picture over the serial port
          //Serial.println();
      }      
     while(1);
}

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

//Send take picture command
void SendTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x00, BYTE);  
}

//Read data
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;                            //address increases 32£¬set according to buffer size
}

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(0x11, BYTE);
}

void StopTakePhotoCmd()
{
      mySerial.print(0x56, BYTE);
      mySerial.print(0x00, BYTE);
      mySerial.print(0x36, BYTE);
      mySerial.print(0x01, BYTE);
      mySerial.print(0x03, BYTE);        
}
void ChangeBaudRate()
{
      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);             
}

This code is producing the JPEG HEX values and I managed to use a HEXEditor to construct a .JPG from it.
Find the attached .JPG file, I simply copied the HEX values from the Serial Monitor and pasted it into the HexEditor and it generated the string necessary and saved the file as .jpg and there was the IMAGE !!!

However the image has errors and this is not all.
My objective is to save this image from HEX to JPG on an SD.
I purchased a EthernetShield that has the microSD card on board and I want to save the file as JPG on SD. Please help me with this :roll_eyes:
The CS is pin 4 and pin 53 is to be kept as an output, I have taken all the measures for the SD card to function properly and also am able to write string to a txt file and saved on SD "Hello World" :stuck_out_tongue:

But I want to write the image hex values as string to the card with .jpg extension. Please advice how I should write to SD that will guarantee that the image will be opened without the same old "Corrupted or loo large image" error that I have been facing since 2 weeks now. How can I get rid of the errors in the pic, I tried playing around with the baud rate because I assumed that may have caused a problem but that did not solve.

oo.jpg

This code is producing the JPEG HEX values and I managed to use a HEXEditor to construct a .JPG from it.
Find the attached .JPG file, I simply copied the HEX values from the Serial Monitor and pasted it into the HexEditor and it generated the string necessary and saved the file as .jpg and there was the IMAGE !!!

Way more work than necessary.

My objective is to save this image from HEX to JPG on an SD.

Piece of cake. Open the file, as picFile. Then change this crap:

          for(j=0;j<count;j++)
          {   if(a[j]<0x10)
              Serial.print("0");
              Serial.print(a[j],HEX);
              Serial.print(" ");
          }

to:

              picFile.write(a,count);