Skip to content

Instantly share code, notes, and snippets.

@muhammadyaseen
Created June 25, 2014 11:35
Show Gist options
  • Save muhammadyaseen/752b4d79bcd5902f55af to your computer and use it in GitHub Desktop.
Save muhammadyaseen/752b4d79bcd5902f55af to your computer and use it in GitHub Desktop.
Adapted example from linksprite site, it transfers JPEG image taken from camrea to Arduino Serial port and from there -through USB- to Computer
//*******************************************************
// www.linksprite.com
// Note:
// 1. SD must be formated to FAT16
// 2. As the buffer of softSerial1 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
//
// Connections : @Yaseen
// RX from MAX3232 to Arduino Serial 1 (TX 1)
// TX from MAX3232 to Arduino Serial 1 (RX 1)
// Vcc from camera to ARduino +5v
// Gnd from camera to Arduino Gnd
//*******************************************************
byte ZERO = 0x00;
byte incomingbyte;
long int j=0,k=0,count=0,i=0x0000;
uint8_t MH,ML;
boolean EndFlag=0;
//File myFile;
void SendResetCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x26);
Serial1.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)
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x54);
Serial1.write(0x01);
Serial1.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)
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x24);
Serial1.write(0x03);
Serial1.write(0x01);
Serial1.write(baudrate);
}
void SendTakePhotoCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x36);
Serial1.write(0x01);
Serial1.write(ZERO);
}
void SendReadDataCmd()
{
MH=i/0x100;
ML=i%0x100;
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x32);
Serial1.write(0x0c);
Serial1.write(ZERO);
Serial1.write(0x0a);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(MH);
Serial1.write(ML);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(0x20);
Serial1.write(ZERO);
Serial1.write(0x0a);
i+=0x20;
}
void StopTakePhotoCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x36);
Serial1.write(0x01);
Serial1.write(0x03);
}
void setup()
{
Serial.begin(38400);
while (!Serial)
{
; // wait for Serial1 port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
///pinMode(10, OUTPUT);
/*if (!SD.begin(4))
{
Serial1.println("initialization failed!");
return;
}*/
Serial.println("initialization done.");
Serial.println("please waiting ....");
Serial1.begin(115200);
delay(100);
SendResetCmd();
delay(2000);
SetBaudRateCmd(0x2A);
delay(500);
// Serial1.begin(115200);
Serial1.begin(38400);
delay(100);
}
void loop()
{
byte a[32];
int ii;
SendResetCmd();
delay(2000); //Wait 2-3 second to send take picture command
SendTakePhotoCmd();
delay(1000);
while(Serial1.available()>0)
{
incomingbyte=Serial1.read();
}
//myFile = SD.open("pic.jpg", FILE_WRITE); //The file name should not be too long
while(!EndFlag)
{
j=0;
k=0;
count=0;
//Serial1.flush();
SendReadDataCmd();
delay(20);
while(Serial1.available()>0)
{
incomingbyte=Serial1.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 Serial1 port
//Serial.print("");
}
//for(ii=0; ii<count; ii++)
//myFile.write(a[ii]);
Serial.println();
}
//myFile.close();
Serial.print("Finished writing data to file");
Serial.println("End of image");
while(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment