Offline
Newbie
Karma: 0
Posts: 6
|
 |
« on: November 13, 2012, 11:14:23 pm » |
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: #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.
|
|
|
|
« Last Edit: November 14, 2012, 01:11:27 am by Coding Badly »
|
Logged
|
|
|
|
|
|
|
Offline
Full Member
Karma: 4
Posts: 187
|
 |
« Reply #2 on: November 14, 2012, 12:08:58 am » |
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"  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  .
|
|
|
|
|
Logged
|
From Idea To Invention
|
|
|
|
Offline
Full Member
Karma: 4
Posts: 187
|
 |
« Reply #3 on: November 14, 2012, 01:19:39 am » |
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
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #4 on: November 14, 2012, 05:04:46 am » |
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
Full Member
Karma: 4
Posts: 187
|
 |
« Reply #5 on: November 14, 2012, 06:42:25 am » |
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" Solution:change the definition of starting address variable "a" from int to long int. cheers
|
|
|
|
|
Logged
|
From Idea To Invention
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #6 on: November 14, 2012, 06:44:22 am » |
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
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #7 on: November 14, 2012, 09:42:29 am » |
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
Full Member
Karma: 4
Posts: 187
|
 |
« Reply #8 on: November 14, 2012, 10:08:45 pm » |
It's my pleasure  Enjoy "Art of Logic"
|
|
|
|
|
Logged
|
From Idea To Invention
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #9 on: February 14, 2013, 07:22:55 am » |
I am not able to generate the image  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.
|
|
|
|
« Last Edit: February 14, 2013, 07:26:32 am by shaikhmuneer23 »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #10 on: February 14, 2013, 07:38:47 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 4
Posts: 187
|
 |
« Reply #11 on: February 14, 2013, 09:44:02 am » |
I am not able to generate the image  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  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. 
|
|
|
|
|
Logged
|
From Idea To Invention
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #12 on: February 14, 2013, 11:51:47 am » |
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
Tesla Member
Karma: 89
Posts: 6388
-
|
 |
« Reply #13 on: February 14, 2013, 12:45:38 pm » |
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
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #14 on: February 14, 2013, 12:46:13 pm » |
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
|
|
|
|
|
|