Hello.
I started using the Serial camera kit of grove on my Arduino MEGA, i already checked the subject of someone having the same problem as me but his code don't work even if he said that it was the case on his side, as i don't fully understand everything in loop i hope i can get some help/explanation to fix the issue.
Since i use the last code the guy have posted, i use the serial but i know i don't need to so I need some help to make the cam work with the hardware serials.
Thx in advance for your help.
I reference back the code i used:
#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
#include <SoftwareSerial.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 Serial1
#define PIC_FMT PIC_FMT_VGA
File myFile;
const int rxPin = 18;
const int txPin = 19;
const byte cameraAddr = (CAM_ADDR << 5); // addr
const int buttonPin = 5; // the number of the pushbutton pin
unsigned long picTotalLen = 0; // picture length
int picNameNum = 0;
/*********************************************************************/
void setup()
{
Serial.begin(115200);
CAM_SERIAL.begin(9600); //cant be faster than 9600, maybe difference with diff board.
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
Serial.println("Initializing SD card....");
pinMode(4,OUTPUT); // CS pin of SD Card Shield
if (!SD.begin(4)) {
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
Serial.println("taking");
if(digitalRead(buttonPin) == HIGH){
delay(20); //Debounce
if (digitalRead(buttonPin) == HIGH)
{
delay(200);
if (n == 0) preCapture();
Capture();
Serial.print("Saving picture...");
GetData();
}
Serial.print("\r\nDone ,number : ");
Serial.println(n);
n++ ;
}
}
}
/*********************************************************************/
void clearRxBuf()
{
while (CAM_SERIAL.available())
{
CAM_SERIAL.read();
}
}
/*********************************************************************/
void sendCmd(char cmd[], int cmd_len)
{
for (int i = 0; i < cmd_len; i++) CAM_SERIAL.write(cmd[i]);
}
/*********************************************************************/
int readBytes(char *dest, int len, unsigned int timeout)
{
int read_len = 0;
unsigned long t = millis();
while (read_len < len)
{
while (CAM_SERIAL.available()<1)
{
if ((millis() - t) > timeout)
{
return read_len;
}
}
*(dest+read_len) = CAM_SERIAL.read();
Serial.write(*(dest+read_len));
read_len++;
}
return read_len;
}
/*********************************************************************/
void initialize()
{
char cmd[] = {0xaa,0x0d|cameraAddr,0x00,0x00,0x00,0x00} ;
unsigned char resp[6];
Serial.print("initializing camera...");
while (1)
{
sendCmd(cmd,6);
if (readBytes((char *)resp, 6,1000) != 6)
{
Serial.print(".");
continue;
}
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x0d && resp[4] == 0 && resp[5] == 0)
{
if (readBytes((char *)resp, 6,500) != 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[] = { 0xaa, 0x01 | cameraAddr, 0x00, 0x07, 0x00, PIC_FMT };
unsigned char resp[6];
while (1)
{
clearRxBuf();
sendCmd(cmd, 6);
if (readBytes((char *)resp, 6, 100) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x01 && resp[4] == 0 && resp[5] == 0) break;
}
}
void Capture()
{
char cmd[] = { 0xaa, 0x06 | cameraAddr, 0x08, PIC_PKT_LEN & 0xff, (PIC_PKT_LEN>>8) & 0xff ,0};
unsigned char resp[6];
while (1)
{
clearRxBuf();
sendCmd(cmd, 6);
if (readBytes((char *)resp, 6, 100) != 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 (readBytes((char *)resp, 6, 100) != 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 (readBytes((char *)resp, 6, 100) != 6) continue;
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x04 && resp[4] == 0 && resp[5] == 0)
{
if (readBytes((char *)resp, 6, 1000) != 6)
{
continue;
}
if (resp[0] == 0xaa && resp[1] == (0x0a | cameraAddr) && resp[2] == 0x01)
{
picTotalLen = (resp[3]) | (resp[4] << 8) | (resp[5] << 16);
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[] = { 0xaa, 0x0e | cameraAddr, 0x00, 0x00, 0x00, 0x00 };
unsigned char pkt[PIC_PKT_LEN];
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{
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 = readBytes((char *)pkt, PIC_PKT_LEN, 200);
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 ++;
}
That is never a good explanation. How is it not working? What do you see on the Serial Monitor? Does the camera initialize? Does the SD card initialize? How is your button wired up? The code demands that the button have a pull-down resistor attached. Is one attached? A schematic is always helpful
+1 for posting your code using code tags!
That is never a good explanation. How is it not working? What do you see on the Serial Monitor? Does the camera initialize? Does the SD card initialize? How is your button wired up? The code demands that the button have a pull-down resistor attached. Is one attached? A schematic is always helpful
Well i'm stuck at "initializing camera..." and the dot continue appearing, the camera seems not to be able to initialize, the SD card initialize correctly. For now I haven't wired the button, I don't have a spare one to connect to 5 and I plan using a PIR sensor but i need to see that the camera initialize before changing the rest of the code, this way I can see the problem one after another.
Then it is time to put some Serial.print() statements in your initialize() function to debug what you are sending and what you are getting back and figure out how that is different than what the code expects.
Are you sure you have it wired correctly?
I'll try as i don't fully understand to what the value i'm getting correspond in the "void initialize",
I'm not sure if it is well wired since even if reverse the software serial port pin, the SD still initialize correctly and the cam is still in it init state, and since nothing seems to change i can't say if it do something or not.
I just tried to use the Serial.println just after the first readBytes as followed:
void initialize()
{
char cmd[] = {0xaa,0x0d|cameraAddr,0x00,0x00,0x00,0x00} ;
unsigned char resp[6];
Serial.print("initializing camera...");
while (1)
{
sendCmd(cmd,6);
if (readBytes((char *)resp, 6,1000) != 6)
Serial.println(readBytes((char*)resp, 6,1000)); // just here i added Serial.println
{
Serial.print(".");
continue;
}
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x0d && resp[4] == 0 && resp[5] == 0)
{
if (readBytes((char *)resp, 6,500) != 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.");
}
and i get 0 everytime in the Serial monitor and i need to get 6 if i understand well the condition so i'm basically blocked at the first step if it's the case
You need to learn a bit of C/C++
Where you placed that Serial.println() call makes it the body of the if() statement and everything after that is no longer part of the if() block.
Your readBytes() call inside the if() statement reads from camera and then your Serial.println() tries to read from the camera again. The camera won't magically resend some data.
void initialize()
{
char cmd[] = {0xaa, 0x0d | cameraAddr, 0x00, 0x00, 0x00, 0x00} ;
unsigned char resp[6];
int ret = -1;
Serial.print("initializing camera...");
while (1)
{
sendCmd(cmd, 6);
ret = readBytes((char *)resp, 6, 1000);
Serial.print("Return value is ");
Serial.println(ret);
for (int i = 0; i < ret; ++i ) {
Serial.print(resp[i], HEX);
Serial.print(",");
}
Serial.println()
if (ret != 6)
{
Serial.println(".");
continue;
}
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x0d && resp[4] == 0 && resp[5] == 0)
{
if (readBytes((char *)resp, 6, 500) != 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.");
}
My guess is that is it always reading 0 bytes because some hardware isn't correct.
I understand, i'm actually right into it, i'll try your code and I send you back what i get if it has changed. Also there is no more pin available on the cam so on the hardware side the only thing i think about is maybe I inverted RX and TX since there is no indications on the cam's pin for this
I just checked and i was connecting the RX to the RX this whole time -_-
So i changed it and now I test to see if there is some changing in the output of the value
And it still output 0...
Idk from where the prob come, maybe a picture may help you see on the Hardware side if there is a problem ? ^^
I finally got some values !
But I need to spam the connect disonnect, what I mean is that I connect the sensor to the cable and I get 1 value (between 1 and 5 for the moment) before it go back to 0 so I need to connect and disconnect the cable repetitively to get multiple value and I'm sur it's not good for the sensor so idk what I can do.
There is the new connectic also:
What is the code you are currently using to see this behavior?
Right now i'm using the following code :
// 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
unsigned long picTotalLen = 0; // picture length
int picNameNum = 0;
/*********************************************************************/
void setup()
{
Serial.begin(115200);
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
Serial.println("Initializing SD card....");
pinMode(4,OUTPUT); // CS pin of SD Card Shield
if (!SD.begin(4))
{
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[] = {0xaa,0x0d|cameraAddr,0x00,0x00,0x00,0x00} ;
unsigned char resp[6];
int ret = -1
int ret2 = -1
Serial.setTimeout(500);
while (1)
{
clearRxBuf();
sendCmd(cmd,6);
ret = Serial.readBytes((char *)resp, 6);
if (ret != 6)
{
Serial.print("Return value is");
Serial.println(ret);
continue;
}
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x0d && resp[4] == 0 && resp[5] == 0)
{
ret2 = Serial.readBytes((char *)resp, 6);
if (ret2 != 6)
{
Serial.print("2nd return value is");
Serial.println(ret2);
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[] = { 0xaa, 0x01 | cameraAddr, 0x00, 0x07, 0x00, PIC_FMT };
unsigned char resp[6];
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[] = { 0xaa, 0x06 | cameraAddr, 0x08, PIC_PKT_LEN & 0xff, (PIC_PKT_LEN>>8) & 0xff ,0};
unsigned char resp[6];
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);
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[] = { 0xaa, 0x0e | cameraAddr, 0x00, 0x00, 0x00, 0x00 };
unsigned char pkt[PIC_PKT_LEN];
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 used the base code provided by the seeed wiki (got it on github since the wiki sent me there) and i just tweaked the SD card pin to allow it to initialize and then put a part of your code into the initialize changing what needed to be changed. I was still blocked at the 1st step tho and i can't test the program any further for now since I don't have everyhting i use at home (it's a school project and we can't take it home to work on it).
That code suggests the camera is connected to Serial, the same as the serial monitor. Doubtful that will work. The original code has the camera connected to Serial1.
I used Serial1 from arduino MEGA at school I just forgot to change it here
When communicating with the camera, your code should be using CAM_SERIAL. That is the whole point of that constant. You can not simultaneously talk to the camera and the serial monitor.
For example, your code should look like this:
void sendCmd(char cmd[], int cmd_len)
{
for (char i = 0; i < cmd_len; i++) CAM_SERIAL.print(cmd[i]);
}
And that applies to ALL the places you communicate with the camera.
I replaced Serial1 by CAM_SERIAL everywhere I thought it was communicating with the cam but I still get the same behavior as before from the cam
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.