I recently purchased a Link Sprite LS-Y201-2MP TTL camera and successfully connected it to my Mega 2560. I can take a single picture without issue. However, when I attempt to take multiple pictures, the initialization fails after the first picture. I am using the standard code from the tutorial found here:
for the second picture, I added
StopTakePhotoCmd();
delay(5000);
SendResetCmd();
setup();
after the myFile.close at the bottom of void loop.
Any ideas what else would need to be added to get the second picture to be taken?
Any assistance with this issue would be greatly appreciated.
system
November 28, 2014, 11:19pm
2
for the second picture, I added
StopTakePhotoCmd();
delay(5000);
SendResetCmd();
setup();
after the myFile.close at the bottom of void loop.
You really do not want to be calling setup() a second time. And certainly not on every pass through loop().
You DO want to post all of your code. You know you do. So, just do it.
Thank you for the reply, below is the code that I am using. I changed it up to make it more useful to what I am doing later.
//*******************************************************
// www.linksprite.com
// Note:
// 1. SD must be formated to FAT16
// 2. As the buffer of softserial has 64 bytes,
// so the code read 32 bytes each time
// 3. Please add the libaray to the lib path
//
// * SD card attached to SPI bus as follows:
// * MOSI - pin 11
// * MISO - pin 12
// * CLK - pin 13
// * CS - pin 4
//*******************************************************
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
SoftwareSerial mySerial(11,12); // Set Arduino pin 4 and 5 as softserial
byte ZERO = 0x00;
byte incomingbyte;
long int j=0,k=0,count=0,i=0x0000;
uint8_t MH,ML;
boolean EndFlag=0;
File myFile;
int SecondPic=0;
int TakePic=0;
void SendResetCmd()
{
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x26);
mySerial.write(ZERO);
}
/*************************************/
/* Set ImageSize :
/* <1> 0x22 : 160*120
/* <2> 0x11 : 320*240
/* <3> 0x00 : 640*480
/* <4> 0x1D : 800*600
/* <5> 0x1C : 1024*768
/* <6> 0x1B : 1280*960
/* <7> 0x21 : 1600*1200
/************************************/
void SetImageSizeCmd(byte Size)
{
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x54);
mySerial.write(0x01);
mySerial.write(Size);
}
/*************************************/
/* Set BaudRate :
/* <1>¡¡0xAE : 9600
/* <2>¡¡0x2A : 38400
/* <3>¡¡0x1C : 57600
/* <4>¡¡0x0D : 115200
/* <5>¡¡0xAE : 128000
/* <6>¡¡0x56 : 256000
/*************************************/
void SetBaudRateCmd(byte baudrate)
{
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x24);
mySerial.write(0x03);
mySerial.write(0x01);
mySerial.write(baudrate);
}
void SendTakePhotoCmd()
{
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write(ZERO);
i+=0x0000;
}
void SendReadDataCmd()
{
MH=i/0x100;
ML=i%0x100;
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x32);
mySerial.write(0x0c);
mySerial.write(ZERO);
mySerial.write(0x0a);
mySerial.write(ZERO);
mySerial.write(ZERO);
mySerial.write(MH);
mySerial.write(ML);
mySerial.write(ZERO);
mySerial.write(ZERO);
mySerial.write(ZERO);
mySerial.write(0x20);
mySerial.write(ZERO);
mySerial.write(0x0a);
i+=0x20;
}
void StopTakePhotoCmd()
{
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write(0x03);
SecondPic = HIGH;
}
void setup()
{
TakePic=1;
}
void loop()
{
if (TakePic == 1) {
CameraSetup();
}
}
void CameraSetup()
{
Serial.begin(115200);
pinMode(53, OUTPUT);
while (!Serial)
Serial.print("Initializing SD card...");
if (!SD.begin(53))
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
Serial.println("please waiting ....");
mySerial.begin(115200);
delay(100);
SendResetCmd();
delay(2000);
SetBaudRateCmd(0x0D);
delay(500);
SetImageSizeCmd(0x22);
delay(500);
mySerial.begin(38400);
delay(100);
CameraLoop();
}
void CameraLoop ()
{
byte a[32];
int ii;
SendResetCmd();
delay(2000); //Wait 2-3 second to send take picture command
SendTakePhotoCmd();
delay(1000);
while(mySerial.available()>0)
{
incomingbyte=mySerial.read();
}
if (SecondPic == LOW) {
myFile = SD.open("pic1.jpg", FILE_WRITE); //The file name should not be too long
}
else {
myFile = SD.open("pic2.jpg", FILE_WRITE); //The file name should not be too long
}
while(!EndFlag)
{
j=0;
k=0;
count=0;
SendReadDataCmd();
delay(20);
while(mySerial.available()>0)
{
incomingbyte=mySerial.read();
k++;
delay(1); //250 for regular
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(" ");
}
for(ii=0; ii<count; ii++)
myFile.write(a[ii]);
Serial.println();
}
myFile.close();
Serial.print("Finished writing data to file");
delay(500);
StopTakePhotoCmd();
delay(500);
SendResetCmd();
EndFlag=0;
delay(5000);
if (SecondPic=HIGH){
CameraSetup();
SecondPic=LOW;
TakePic=0;
}
while(1);
}
system
November 29, 2014, 12:53pm
4
SoftwareSerial mySerial(11,12); // Set Arduino pin 4 and 5 as softserial
If you are going to have useless comments, you MUST keep them accurate!
i+=0x0000;
WTF?
MH=i/0x100;
ML=i%0x100;
Getyourspacebarfixed!
SecondPic = HIGH;
This suggests that you are using the wrong type variable and/or the wrong value!
void loop()
{
if (TakePic == 1) {
CameraSetup();
}
}
This is plain stupid. If you don't want to work with the Arduino's setup() and loop() structure, get a different microcontroller.
I quit reading at this point. Good luck with your project.
Well PaulS, you need to tone it down a bit.
This forum is for people to ask assistance not to be berated by someone who thinks he is the smartest person in the room. If you would have checked the first post you would see that most of your comments are from the code that was posted in the tutorial. If you also do not understand something I would recommend not commenting on. Clearly you did not in this case.
I did however get this functioning properly this morning so if anyone who is struggling would actually like some assistance from someone is actually here to help, not just shamelessly trying to boost their ego, please feel free to pm me.
hello, i am using lsy-201 with arduino mega
but i am not getting a valid jpeg i.e i am not getting FFD8 in the beginning, previously i was getting correct image but i kept that camera aside for few months and now when i tried ,i got this problem.is my camera damaged?
what should i do?
I have the same problem with this camera module LS-Y201.I can not take multiple pictures & can not save them in the sd card as I do not know how to change the name of the file where pic is being saved.Kindly respond & thanks in advance.