Linksprite LS-Y201 Camera unable to take more than one picture

Hello,

I have a linksprite LS-Y201 camera - https://www.sparkfun.com/products/retired/10061

I have got it working and it is capturing one image…but any attempt to get it take more than one image, fails.

I’m using a Mega, with hardware serial.

Here is my code - Please bear in mind that the code contains a lot of stuff that shouldn’t be there! -

#include <SoftwareSerial.h>

int count=0;
int count2=0;   
int GPScount=0;
unsigned char buffer[64]; // buffer array for data recieve over serial port
int buffercount=0;     // counter for buffer array 
int datatosend=0;     // counter for buffer array 
String indata;

int a=0x0000,j=0,k=0;                    //Read Starting address       
uint8_t MH,ML;

byte incomingbyte;
                    
void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup(){

Serial.begin(115200);

  pinMode(22, OUTPUT);  

}

void loop(){
  int EndFlag=0;
 
 
  digitalWrite(22, HIGH);    ////// Turn camera/relay on
  delay(5000);
  Serial2.begin(38400);
  delay(5000);
  
  while (Serial2.available()>0){
  Serial.print(Serial2.read());
  }
      int flag = 0;
      int count=0;
      int totalcount=0;

     SendTakePhotoCmd();
     delay(500);
      while (Serial2.available()>0){
      Serial.print(Serial2.read());
  }
  
      byte a[32];
      EndFlag=0;
      while(EndFlag != 1)
      
      {  
         j=0;
         k=0;
         count=0;
         SendReadDataCmd();
         delay(50);
         while(Serial2.available()>0)
          {
               incomingbyte=Serial2.read();
               k++;
               if((k>5)&&(j<32)&&(EndFlag != 1))
               {
               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(" ");
            
          }              

      }
  digitalWrite(22, LOW);   ////// Turn camera/relay off
    while (Serial2.available()>0){
  Serial.print(Serial2.read());
     }
     
delay(5000);
Serial.println("Ended");



}





//Send Reset command
void SendResetCmd()
{
      Serial2.write(0x56);
      Serial2.write((byte)0x00);
      Serial2.write(0x26);
      Serial2.write((byte)0x00);
      delay(5000);
      
      Serial.println("Reset = ");
      Serial.print(Serial2.read());
}

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

//Read data
void SendReadDataCmd()
{
      MH=a/0x100;
      ML=a%0x100;
      Serial2.write(0x56);
      Serial2.write((byte)0x00);
      Serial2.write(0x32);
      Serial2.write(0x0c);
      Serial2.write((byte)0x00); 
      Serial2.write(0x0a);
      Serial2.write((byte)0x00);
      Serial2.write((byte)0x00);
      Serial2.write(MH);
      Serial2.write(ML);   
      Serial2.write((byte)0x00);
      Serial2.write((byte)0x00);
      Serial2.write((byte)0x00);
      Serial2.write(0x20);
      Serial2.write((byte)0x00);  
      Serial2.write(0x0a);
      a+=0x20;                            //address increases 32£¬set according to buffer size
//      Serial.println("photo");

}

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

void SetSize()
{
      Serial2.write(0x56);
      Serial2.write((byte)0x00);
      Serial2.write(0x31);
      Serial2.write(0x05);
      Serial2.write(0x04); 
      Serial2.write(0x01);
      Serial2.write((byte)0x00);  
      Serial2.write(0x19);    
      Serial2.write(0x22);    // unrem this if you want 640x480
      // 640x480     Serial2.write((byte)0x00);
}


void SetCompression()
{
      Serial2.write(0x56);
      Serial2.write((byte)0x00);
      Serial2.write(0x31);
      Serial2.write(0x05);
      Serial2.write(0x01); 
      Serial2.write(0x01);
      Serial2.write(0x12);
      Serial2.write(0x04);
      Serial2.write(0xFF);
}


void GetSize()
{
      Serial2.write(0x56);
      Serial2.write((byte)0x00);
      Serial2.write(0x34);
      Serial2.write(0x01);
     Serial2.write((byte)0x00); 

}

As you can see, I’m using a relay to turn the camera on then off.
So each loop, the camera is effectively starting afresh.
But this is the data I get back in the serial window -
First pass = 11805400FF D8 FF FE …loads of picture data… AF B1 FF FF D9 Ended
Second pass = 0255118054007F FF FF D9 Ended

As you can see, the second pass is completely missing all the picture data and appears to just be the end of another pic (because of the FF D9).

The strange thing is that if I perform a reset of the Arduino by using the reset button, I get a picture, which leads me to believe that it isn’t the camera or arduino needing a full power cycle. I suspect it’s more of a case of the Arduino holding some rubbish data in the buffers, which passes over onto subsequent loops.

Thank you for your assistance with this head scratcher :o)

The loops to read the response don't look right to me - you seem to assume that the 'end of data' byte sequence can't occur within the data (perhaps that's valid) and that they will both be available at the same time (that doesn't seem valid) and that they will both arrive within the first 32 bytes of contiguous data (I have no idea whether that's valid).

How about just printing every single byte you get back, and see what's there? I'd suggest also logging the sent commands so it is obvious which output is the response to which command.

what are the pin connection u used?