#include <SoftwareSerial.h>
#include <SD.h>
/* SD Card */
const int chipSelect = 10;
/* Linksprite */
byte incomingbyte;
int sizeMessage[9], readtest[2];
long a=0x0000; //Read Starting address
int i=0, j=0, b = 0, packetcount=0, imageSize, test;
boolean ImageEnd = 0, packetcycle = 0;
uint8_t MH,ML;
File dataFile;
void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();
void GetImageSize();
SoftwareSerial mySerial(4,5); //Configure pin 4 and 5 as soft serial port
void setup()
{
Serial.begin(38400);
mySerial.begin(38400);
// Set up SD Card
pinMode(chipSelect, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println(" Card failed, or not present"); // don't do anything more:
return;
}
Serial.println("card initialized.");
// Create image filename
int index = millis()/1000;
Serial.print("index: ");
Serial.println(index);
String filename = "CAM_"+String(index)+".jpg"; // Create new filename
int strlen=filename.length()+1; // Get the length of the string including the null character
char charBuf[strlen]; // initialize a charachter array to fold the filename
filename.toCharArray(charBuf, strlen); // convert the String filename to character array
dataFile = SD.open(charBuf, FILE_WRITE); // Open new file on SD Card
Serial.println("file created: ");
Serial.println(dataFile);
// Reset Camera *************************************
Serial.print("Clearing input buffer\n");
while(mySerial.available()>0)
{
incomingbyte=mySerial.read();
}
// Reset Camera *************************************
Serial.print("Sending Reset\n");
SendResetCmd();
while(mySerial.available()>0)
{
incomingbyte=mySerial.read();
}
// After reset, wait 2-3 second to send take picture command
delay(2500);
// Take Picture **************************************
Serial.print("Sending Take Photo\n");
SendTakePhotoCmd();
while(mySerial.available()>0)
{
incomingbyte=mySerial.read();
}
// Clear Serial Buffer ********************************
while(mySerial.available()>0)
{
incomingbyte=mySerial.read();
}
// Get Image Size **************************************
Serial.print("Sending Image Size\n");
GetImageSize();
Serial.println("Get Image Size: ");
i=0;
while(mySerial.available()<9) // wait for 9 bytes
{
}
while(mySerial.available()>0) // Read the serial buffer contents
{
incomingbyte=mySerial.read();
// Serial.println(incomingbyte,HEX);
sizeMessage = incomingbyte;
i++;
}
imageSize=word(sizeMessage[7], sizeMessage[8]);
Serial.println(imageSize);
}
void loop()
{
byte packet[42];
// Read Data Packet ********************************
while(!ImageEnd)
{
while(mySerial.available()>0) // Read the serial buffer contents
{
incomingbyte=mySerial.read();
}
Serial.print("Sending Read Data\n");
Serial.print("Read Address: ");
Serial.print(a);
SendReadDataCmd();
packetcount++;
while(mySerial.available()<42)
{
}
i = 0;
while(i < 42) // Read 42 bytes in from serial buffer
{
while(mySerial.available()>0)
{
incomingbyte=mySerial.read();
packet = incomingbyte;
i++;
}
}
// Test packet to see if preable 5 bytes equals the trailing 5 bytes
test = 0;
for ( i=0; i <5; i++)
{
if( !(packet == packet[i+37])) test += 1;
}
if( test>0)
{
Serial.print("Bad Packet*************************");
Serial.println(test);
}
// Loop through data packet
for ( i=5; i <37; i++) //read the image data out of the packet
{
b++;
// Write data to packet to SD card
if (dataFile)
{
dataFile.write(packet);
Serial.print(packet, HEX);
Serial.print(" ");
}
else
{
Serial.println("error creating file");
}
if((packet[i-1]==0xFF)&&(packet==0xD9)) // Test for end of image tag
{
dataFile.close();
Serial.print("Image End: ");
Serial.print(packet[i-1],HEX);
Serial.print(" ");
Serial.println(packet,HEX);
Serial.print("packets: ");
Serial.print( packetcount);
Serial.print(" bytes read: ");
Serial.print( b);
Serial.print(" bytes remaining: ");
Serial.println( imageSize-b);
packetcount++;
ImageEnd=1;
return;
}
}
Serial.println(" ");
}
}
//Send Reset command
void SendResetCmd()
{
mySerial.write(0x56);
mySerial.write(byte(0x00));
mySerial.write(0x26);
mySerial.write(byte(0x00));
}
//Send take picture command
void SendTakePhotoCmd()
{
mySerial.write(0x56);
mySerial.write(byte(0x00));
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write(byte(0x00));
}
//Read data
void SendReadDataCmd()
{
MH=a/0x100;
ML=a%0x100;
mySerial.write(0x56);
mySerial.write(byte(0x00));
mySerial.write(0x32);
mySerial.write(0x0c);
mySerial.write(byte(0x00));
mySerial.write(0x0a);
mySerial.write(byte(0x00));
mySerial.write(byte(0x00));
mySerial.write(MH);
mySerial.write(ML);
mySerial.write(byte(0x00));
mySerial.write(byte(0x00));
mySerial.write(byte(0x00));
mySerial.write(0x20);
mySerial.write(byte(0x00));
mySerial.write(0x0a);
a+=0x20; //address increases 32£¬set according to buffer size
}
void StopTakePhotoCmd()
{
mySerial.write(0x56);
mySerial.write(byte(0x00));
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write(0x03);
}
void GetImageSize()
{
mySerial.write(0x56);
mySerial.write(byte(0x00));
mySerial.write(0x34);
mySerial.write(0x01);
mySerial.write(byte(0x00));
}
error
sketch_feb07b.ino: In function 'void setup()':
sketch_feb07b:112: error: incompatible types in assignment of 'byte' to 'int [9]'
sketch_feb07b.ino: In function 'void loop()':
sketch_feb07b:153: error: incompatible types in assignment of 'byte' to 'byte [42]'
sketch_feb07b:163: error: ISO C++ forbids comparison between pointer and integer
sketch_feb07b:182: error: call of overloaded 'write(byte [42])' is ambiguous
C:\Program Files\Arduino\libraries\SD/SD.h:35: note: candidates are: virtual size_t File::write(uint8_t)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/Print.h:49: note: size_t Print::write(const char*)
sketch_feb07b:183: error: call of overloaded 'print(byte [42], int)' is ambiguous
C:\Program Files\Arduino\hardware\arduino\cores\arduino/Print.h:59: note: candidates are: size_t Print::print(unsigned char, int)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/Print.h:60: note: size_t Print::print(int, int)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/Print.h:61: note: size_t Print::print(unsigned int, int)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/Print.h:62: note: size_t Print::print(long int, int)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/Print.h:63: note: size_t Print::print(long unsigned int, int)
sketch_feb07b:191: error: ISO C++ forbids comparison between pointer and integer
sketch_feb07b:198: error: call of overloaded 'println(byte [42], int)' is ambiguous
C:\Program Files\Arduino\hardware\arduino\cores\arduino/Print.h:71: note: candidates are: size_t Print::println(unsigned char, int)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/Print.h:72: note: size_t Print::println(int, int)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/Print.h:73: note: size_t Print::println(unsigned int, int)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/Print.h:74: note: size_t Print::println(long int, int)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/Print.h:75: note: size_t Print::println(long unsigned int, int)