I need your help how can I use Grove Serial Camera on Arduino wifi rev2.
which I use Arduino UNO it does work but when I use Arduino wifi rev2 it does’nt work.
What is done
-
check SDCARD with (Sketch book file) “Card Info”
result are available to use it.
2 . This is my source code . When I use on Arduino UNO is doesn’ happen . So if something wrong please pointed out my issue.// File SerialCamera_DemoCode_CJ-OV528.ino
// 8/8/2013 Jack Shao
// Demo code for using seeeduino or Arduino board to cature jpg format
// picture from seeed serial camera and save it into sd card. Push the
// button to take the a picture .
// For more details about the product please check http://www.seeedstudio.com/depot/
#include <SPI.h>
#include <arduino.h>
#include <SD.h>
#define PIC_PKT_LEN 128 //data length of each read, dont set this too big because ram is limited
#define PIC_FMT_VGA 7
#define PIC_FMT_CIF 5
#define PIC_FMT_OCIF 3
#define CAM_ADDR 0
#define CAM_SERIAL Serial
#define PIC_FMT PIC_FMT_VGA
File myFile;
const byte cameraAddr = (CAM_ADDR << 5); // addr
//const int buttonPin = A5; // the number of the pushbutton pin
const int buttonPin = 2;
unsigned long picTotalLen = 0; // picture length
int picNameNum = 0;
/*********************************************************************/
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
Serial.println(“Initializing SD card…”);
pinMode(SS,OUTPUT); // CS pin of SD Card Shield “SS”
if (!SD.begin(10))
{
Serial.print("sd init failed");
return;
}
Serial.println("sd init done.");
initialize();
}
//
void loop()
{
int n = 0;
while(1){
Serial.println("\r\nPress the button to take a picture");
while (digitalRead(buttonPin) == LOW); //wait for buttonPin status to HIGH
if(digitalRead(buttonPin) == HIGH){
delay(20); //Debounce
if (digitalRead(buttonPin) == HIGH)
{
Serial.println("\r\nbegin to take picture");
delay(200);
if (n == 0) preCapture();
Capture();
GetData();
}
Serial.print("\r\nTaking pictures success ,number : ");
Serial.println(n);
n++ ;
}
}
}
//
void clearRxBuf()
{
while (Serial.available())
{
Serial.read();
}
}
//
void sendCmd(char cmd[], int cmd_len)
{
for (char i = 0; i < cmd_len; i++) Serial.print(cmd[i]);
}
//
void initialize()
{
char cmd[6];
// = {0xaa,0x0d|cameraAddr,0x00,0x00,0x00,0x00} ;
unsigned char resp[6];
cmd[0] = 0xaa;
cmd[1] = 0x0d|cameraAddr;
cmd[2] = 0x00;
cmd[3] = 0x00;
cmd[4] = 0x00;
cmd[5] = 0x00;
Serial.setTimeout(500);
while (1)
{
// clearRxBuf();
sendCmd(cmd,6);
if (Serial.readBytes((char )resp, 6) != 6)
{
continue;
}
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x0d && resp[4] == 0 && resp[5] == 0)
{
if (Serial.readBytes((char )resp, 6) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0d | cameraAddr) && resp[2] == 0 && resp[3] == 0 && resp[4] == 0 && resp[5] == 0) break;
}
}
cmd[1] = 0x0e | cameraAddr;
cmd[2] = 0x0d;
sendCmd(cmd, 6);
Serial.println("\nCamera initialization done.");
}
/*******************************************************************/
void preCapture()
{
char cmd[6];
// = { 0xaa, 0x01 | cameraAddr, 0x00, 0x07, 0x00, PIC_FMT };
unsigned char resp[6];
cmd[0] = 0xaa;
cmd[1] = 0x01 | cameraAddr;
cmd[2] = 0x00;
cmd[3] = 0x07;
cmd[4] = 0x00;
cmd[5] = PIC_FMT;
Serial.setTimeout(100);
while (1)
{
clearRxBuf();
sendCmd(cmd, 6);
if (Serial.readBytes((char *)resp, 6) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x01 && resp[4] == 0 && resp[5] == 0) break;
}
}
void Capture()
{
char cmd[6];
// = { 0xaa, 0x06 | cameraAddr, 0x08, PIC_PKT_LEN & 0xff, (PIC_PKT_LEN>>8) & 0xff ,0};
unsigned char resp[6];
cmd[0] = 0xaa;
cmd[1] = 0x06 | cameraAddr;
cmd[2] = 0x08;
cmd[3] = PIC_PKT_LEN & 0xff;
cmd[4] = (PIC_PKT_LEN>>8) & 0xff;
cmd[5] = 0;
Serial.setTimeout(100);
while (1)
{
clearRxBuf();
sendCmd(cmd, 6);
if (Serial.readBytes((char *)resp, 6) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x06 && resp[4] == 0 && resp[5] == 0) break;
}
cmd[1] = 0x05 | cameraAddr;
cmd[2] = 0;
cmd[3] = 0;
cmd[4] = 0;
cmd[5] = 0;
while (1)
{
clearRxBuf();
sendCmd(cmd, 6);
if (Serial.readBytes((char *)resp, 6) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x05 && resp[4] == 0 && resp[5] == 0) break;
}
cmd[1] = 0x04 | cameraAddr;
cmd[2] = 0x1;
while (1)
{
clearRxBuf();
sendCmd(cmd, 6);
if (Serial.readBytes((char *)resp, 6) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x04 && resp[4] == 0 && resp[5] == 0)
{
Serial.setTimeout(1000);
if (Serial.readBytes((char *)resp, 6) != 6)
{
continue;
}
if (resp[0] == 0xaa && resp[1] == (0x0a | cameraAddr) && resp[2] == 0x01)
{
// picTotalLen = (resp[3]) | (resp[4] << 8) | (resp[5] << 16);
picTotalLen = resp[5]; // resp[3]~resp[5]の値が上に揚げた例えのとおりだとすると、picTotalLen=0x12になる。この時点では1-8ビット目だが、後でシフトされた17-24ビット目の値になる
picTotalLen = picTotalLen << 8; // 左に8ビットシフトするとpicTotalLen=0x1200になる。
picTotalLen = picTotalLen | resp[4]; // 1-8ビット目にresp[4]の値を入れる(元が0なので、OR演算することでresp[4]と同じ値になります)と、picTotalLen = 0x1234になる。
picTotalLen = picTotalLen << 8; // 左に8ビットシフトするとpicTotalLen=0x123400になる。
picTotalLen = picTotalLen | resp[3]; // 1-8ビット目にresp[3]の値を入れると、picTotalLen=0x123456になる。
Serial.print(“picTotalLen:”);
Serial.println(picTotalLen);
break;
}
}
}
}
/*********************************************************************/
void GetData()
{
unsigned int pktCnt = (picTotalLen) / (PIC_PKT_LEN - 6);
if ((picTotalLen % (PIC_PKT_LEN-6)) != 0) pktCnt += 1;
char cmd[6];
// = { 0xaa, 0x0e | cameraAddr, 0x00, 0x00, 0x00, 0x00 };
unsigned char pkt[PIC_PKT_LEN];
cmd[0] = 0xaa;
cmd[1] = 0x0e | cameraAddr;
cmd[2] = 0x00;
cmd[3] = 0x00;
cmd[4] = 0x00;
cmd[5] = 0x00;
char picName[] = "pic00.jpg";
picName[3] = picNameNum/10 + '0';
picName[4] = picNameNum%10 + '0';
if (SD.exists(picName))
{
SD.remove(picName);
}
myFile = SD.open(picName, FILE_WRITE);
if(!myFile){
Serial.println("myFile open fail...");
}
else{
Serial.setTimeout(1000);
for (unsigned int i = 0; i < pktCnt; i++)
{
cmd[4] = i & 0xff;
cmd[5] = (i >> 8) & 0xff;
int retry_cnt = 0;
retry:
delay(10);
clearRxBuf();
sendCmd(cmd, 6);
uint16_t cnt = Serial.readBytes((char *)pkt, PIC_PKT_LEN);
unsigned char sum = 0;
for (int y = 0; y < cnt - 2; y++)
{
sum += pkt[y];
}
if (sum != pkt[cnt-2])
{
if (++retry_cnt < 100) goto retry;
else break;
}
myFile.write((const uint8_t *)&pkt[4], cnt-6);
//if (cnt != PIC_PKT_LEN) break;
}
cmd[4] = 0xf0;
cmd[5] = 0xf0;
sendCmd(cmd, 6);
}
myFile.close();
picNameNum ++;
}
======================================
I am apprecieate if somebody have same with my experience. Thanks for watching