Pages: [1] 2   Go Down
Author Topic: Arduino + TTL Camera +SD  (Read 2165 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I already check most of the topic about ttl camera with arduino in this forum..
but I still can not find out how to deal with this big trouble,I use the board MEGA2560,amd my code is as follow:

Code:
#include <SdFat.h>

const uint8_t chipSelect = SS;
SdFat sd;
SdFile myFile;

byte incomingbyte;
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 changebaudrate();

ArduinoOutStream cout(Serial);
#define error(s) sd.errorHalt_P(PSTR(s))
void setup()
{

  Serial.begin(19200);
  Serial1.begin(38400);
  pinMode(3, OUTPUT);
  
  
  if (!sd.begin(3, SPI_HALF_SPEED)) sd.initErrorHalt();
  if (!myFile.open("test16.jpg", O_WRITE | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }  

}


void loop()
{
  
    SendResetCmd();
    delay(3000);
//    changebaudrate();
//    delay(200);
//    Serial1.end();
//    delay(200);
//    Serial1.begin(9600);
//    delay(200);
 
    byte a[32];
      
       SendTakePhotoCmd();
        delay(30);
    
     while(Serial1.available()>0)
      {
        incomingbyte=Serial1.read();
      }  
    //   delay(30);
    
      while(!EndFlag)
      {  
        delay(15);
        j=0;
         k=0;
         count=0;
         SendReadDataCmd();

         delay(30);
          while(Serial1.available()>0)
          {
              
               incomingbyte=Serial1.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);          
              myFile.write(a[j]);
              Serial.print(" ");
          }                                       //Send jpeg picture over the serial port
          Serial.println();
      }      

     myFile.close();
     while(1);
}

//Send Reset command
void SendResetCmd()
{
      Serial1.write(0x56);
      Serial1.write(byte(0x00));
      Serial1.write(0x26);
      Serial1.write(byte(0x00));
}

//Send take picture command
void SendTakePhotoCmd()
{
      Serial1.write(0x56);
      Serial1.write(byte(0x00));
      Serial1.write(0x36);
      Serial1.write(0x01);
      Serial1.write(byte(0x00));  
}

//Read data
void SendReadDataCmd()
{
      MH=a/0x100;
      ML=a%0x100;
      Serial1.write(0x56);
      Serial1.write(byte(0x00));
      Serial1.write(0x32);
      Serial1.write(0x0c);
      Serial1.write(byte(0x00));
      Serial1.write(0x0a);
      Serial1.write(byte(0x00));
      Serial1.write(byte(0x00));
      Serial1.write(MH);
      Serial1.write(ML);  
      Serial1.write(byte(0x00));
      Serial1.write(byte(0x00));
      Serial1.write(byte(0x00));
      Serial1.write(0x20);
      Serial1.write(byte(0x00));  
      Serial1.write(0x0a);
      a+=0x20;                            //address increases 32£?set according to buffer size
}

void StopTakePhotoCmd()
{
      Serial1.write(0x56);
      Serial1.write(byte(0x00));
      Serial1.write(0x36);
      Serial1.write(0x01);
      Serial1.write(0x03);        
}

void changebaudrate()
{
      Serial1.write(0x56);
      Serial1.write(byte(0x00));
      Serial1.write(0x24);
      Serial1.write(0x03);
      Serial1.write(0x01);
      Serial1.write(0xae);
      Serial1.write(0xc8);      
}
my photo just like the attachment,it is always have lots of broken parts in it..
Can anyone help my please?Thanks a lot!


Moderator edit: [code] [/code] tags added.


* TEST16.JPG (47.07 KB, 640x480 - viewed 127 times.)
« Last Edit: November 14, 2012, 01:11:27 am by Coding Badly » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 (http://twarm.com/commerce/product_info.php?products_id=4859),with TX1,RX1 on arduino mega 2560 board
any idea will be appreciated,thanks a  lot
 
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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" smiley-cool
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   smiley-wink .
Logged

From Idea To Invention

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

From Idea To Invention

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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" smiley-cool
 
Solution:change the definition of starting address variable "a" from int to long int.

cheers
Logged

From Idea To Invention

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

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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: )

Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's my pleasure smiley-cool

Enjoy "Art of Logic"
Logged

From Idea To Invention

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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 - downloaded 106 times.)
« Last Edit: February 14, 2013, 07:26:32 am by shaikhmuneer23 » Logged

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

Code:
     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.

Code:
        incomingbyte=mySerial.read();
This gets one of them.

Code:
      }   
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.

Code:
     while(1);   // ??? is that even necessary its already in void loop()
So loop() only iterates once, and you only take one picture.

Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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  smiley

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. smiley-cool
Logged

From Idea To Invention

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12577
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

Quote
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.
Logged

Pages: [1] 2   Go Up
Jump to: