Call Function

Hello,

Would you please look at my codes below and give me a correction. I want a camera takes 10 images before jumping to “void RemoveFile”. But why it jumps directly to the “void RemoveFile” function after first taking image? However, if I common the “RemoveFile” call function, then it will take 10 images and stop. I’m not sure because the “for loop” is not correct or open and close brackets. Please help.

#include <SdFat.h>
#include “CameraC329.h”
#define CAMERA_BAUD 14400

SdFatSdio SD; //Using teensy 3.6

File photoFile;

CameraC329 camera;

uint32_t writtenPictureSize;
void getPicture_callback(uint32_t pictureSize, uint16_t packetSize, uint32_t packetStartPosition, byte* packet)
{
photoFile.write(packet, packetSize);
Serial.write (packet, packetSize);
writtenPictureSize += packetSize;
if (writtenPictureSize >= pictureSize)
{
photoFile.flush();
photoFile.close();
Serial.println(F(“SUCCESS”));
}
}

void setup()
{
Serial.begin(9600);
while (!Serial);
Serial.println(F("************************************************************"));
Serial.println(F(“Serial READY”));
}

void loop() {
Serial1.begin(CAMERA_BAUD);
while (!Serial1)
Serial.println(F(“Camera connected”));
delay(2000);

if (!SD.begin()) {
Serial.println(F(“SD initialization failed or missing SD card”));
return;
}
else {
Serial.println(F(“Began SD Card”));
}
delay(1000);// wait for SD open file

char filename = “C329Ph00.jpg”;
for (uint8_t i = 0; i < 10; i++) {
filename[6] = i / 10 + ‘0’;
filename[7] = i % 10 + ‘0’;
if (! SD.exists(filename)) { // only open a new file if it doesn’t exist
photoFile = SD.open(filename, FILE_WRITE);
photoFile.seek(0);
writtenPictureSize = 0;
Serial.println(F(“opened picture file”));
break; // leave the loop!
}
}
if (! photoFile) {
Serial.println(F(“couldn’t create file or SD card full”));
return;
Serial.println(F(“File created.”));
Serial.print(F(“Logging to: “));
Serial.println(filename);
delay (1000);
}
if (!camera.sync()) {
Serial.println(F(“Camera Sync Failed”));
return;
}
else
{
Serial.println(F(“Camera ON”));
delay(1000);
}
if (!camera.initialize(CameraC329::BAUD14400, CameraC329::CT_JPEG, CameraC329::PR_160x120, CameraC329::JR_320x240)) {
Serial.println(F(“Camera Failed Initialized”));
return;
}
else
{
Serial.println(F(“Camera Initialized”));
}
if (!camera.setQuality(CameraC329::QL_BEST)) {
Serial.println(F(“Fail to Set quality Good”));
}
if (!camera.takeSnapshot(CameraC329::PT_SNAPSHOT)) {
Serial.println(F(“Fail to take snapshot”));
}
if (!camera.getPicture(CameraC329::PT_JPEG_PREVIEW, &getPicture_callback)) {
Serial.println(F(“Fail to get Picture “));
}
else {
Serial.println(F(“Pictured Successfully”));
}
if (!camera.reset(CameraC329::RT_STATE))
photoFile.close(); {
Serial.println(F(“Finished writing data to file”));
Serial.println(F(”-------------------------------------------------”));
delay(500);
RemoveFile();
}
}
//RemoveFile ******************************************************
void RemoveFile() {
Serial.println(F(”(1)Remove the file…”)); //debug.
Serial.println(“Removing example.txt…”);
char filename[13];
for (uint8_t i = 0; i < 3; i++)
{
sprintf(filename, “C329Ph%02d.jpg”, i);
SD.remove(filename);
if (SD.exists(filename))
{
Serial.print(F(“Could not delete file”));
Serial.println(filename);
}
else
{
Serial.println(F(“All files deleted”));
}
if (!SD.exists(filename)) {
Serial.println(F(“All .jpg extension files gone”));
Serial.println(F("******************************************"));
}
}
}

  for (uint8_t i = 0; i < 10; i++) {
    filename[6] = i / 10 + '0';
    filename[7] = i % 10 + '0';
    if (! SD.exists(filename)) { // only open a new file if it doesn't exist
      photoFile = SD.open(filename, FILE_WRITE);
      photoFile.seek(0);
      writtenPictureSize = 0;
      Serial.println(F("opened picture file"));
      break;  // leave the loop!
    }

Does it work as expected if you remove that break command?

  Serial1.begin(CAMERA_BAUD);
  while (!Serial1)
    Serial.println(F("Camera connected"));

This does not belong in loop().

 if (!SD.begin()) {
    Serial.println(F("SD initialization failed or missing SD card"));
    return;
  }
  else {
    Serial.println(F("Began SD Card"));
  }

Nor does this. Put this stuff in setup() where it belongs.

Use Tools + Auto Format to properly indent your code. It's a mess.

   photoFile.close(); {

Why is there a curly brace at the end of this line? There is no excuse for that.

What do your Serial.print()s tell you is happening?

Thanks for reply. unfortunately it doesn't work as expected even I tried to remove the "break" command.

Thanks all Bros for help. I tried all your sugguestions however it doesn't help anything...Do you have any thoughts and suggestions?

Do you have any thoughts and suggestions?

I think you should answer ALL of the questions posted so far.

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

…R

Sorry all Bros. I’m a newbie with Arduino and programing. I’ve never done any programmings before. I have tried all your suggestions and changed the codes below but it doesn’t help at all. I greatly appreciate all your help.

#include <SdFat.h>
#include "CameraC329.h"
#define CAMERA_BAUD 14400
SdFatSdio SD; //Using teensy 3.6

File photoFile;
CameraC329 camera;

uint32_t writtenPictureSize;
void getPicture_callback(uint32_t pictureSize, uint16_t packetSize, uint32_t packetStartPosition, byte* packet)
{
photoFile.write(packet, packetSize);
Serial.write (packet, packetSize);
writtenPictureSize += packetSize;
if (writtenPictureSize >= pictureSize)
{
photoFile.flush();
photoFile.close();
Serial.println(F("SUCCESS"));
}
}

void setup(){
Serial.begin(9600);
while (!Serial);
Serial.println(F("************************************************************"));
Serial.println(F("Serial READY"));

Serial1.begin(CAMERA_BAUD);
while (!Serial1)
Serial.println(F("Camera connected"));
delay(2000);

if (!SD.begin()) {
Serial.println(F("SD initialization failed or missing SD card"));
return;
}
else {
Serial.println(F("Began SD Card"));
}
delay(1000);// wait for SD open file
}

void loop() {

char filename[] = "C329Ph00.jpg";
for (uint8_t i = 0; i < 10; i++) {
filename[6] = i / 10 + '0';
filename[7] = i % 10 + '0';
if (! SD.exists(filename)) { // only open a new file if it doesn't exist
photoFile = SD.open(filename, FILE_WRITE);
photoFile.seek(0);
writtenPictureSize = 0;
Serial.println(F("opened picture file"));
break;  // leave the loop!
}
}
if (! photoFile)
{
Serial.println(F("couldn't create file or SD card full"));
return;
Serial.println(F("File created."));
Serial.print(F("Logging to: "));
Serial.println(filename);
delay (1000);
}
if (!camera.sync()) {
Serial.println(F("Camera Sync Failed"));
return;
}
else
{
Serial.println(F("Camera ON"));
delay(1000);
}
if (!camera.initialize(CameraC329::BAUD14400, CameraC329::CT_JPEG, CameraC329::PR_160x120, CameraC329::JR_320x240)) {
Serial.println(F("Camera Failed Initialized"));
return;
}
else
{
Serial.println(F("Camera Initialized"));
}
if (!camera.setQuality(CameraC329::QL_BEST)) {
Serial.println(F("Fail to Set quality Good"));
}
if (!camera.takeSnapshot(CameraC329::PT_SNAPSHOT)) {
Serial.println(F("Fail to take snapshot"));
}
if (!camera.getPicture(CameraC329::PT_JPEG_PREVIEW, &getPicture_callback)) {
Serial.println(F("Fail to get Picture "));
}
else {
Serial.println(F("Pictured Successfully"));
}
if (!camera.reset(CameraC329::RT_STATE))
photoFile.close(); {
Serial.println(F("Finished writing data to file"));
Serial.println(F("-------------------------------------------------"));
delay(500);
RemoveFile();
}
}
//RemoveFile ******************************************************
void RemoveFile() {
Serial.println(F("(1)Remove the file...")); //debug.
Serial.println("Removing example.txt...");
char filename[13];
for (uint8_t i = 0; i < 3; i++)
{
sprintf(filename, "C329Ph%02d.jpg", i);
SD.remove(filename);
if (SD.exists(filename))
{
  Serial.print(F("Could not delete file"));
  Serial.println(filename);
}
else
{
  Serial.println(F("All files deleted"));
}
if (!SD.exists(filename)) {
  Serial.println(F("All .jpg extension files gone"));
  Serial.println(F("******************************************"));
}
}
}

I have tried all your suggestions

Not all of them. I suggested that you show us what your serial output looks like. You've steadfastly ignored that.

Debugging by guesswork sucks. Why do you insist that we do it that way?

Sorry, I quite understand your question (What do your Serial.print()s tell you is happening?), if you can help me understand your question?

I think you’ve simply cut & pasted some pretty ‘iffy’ code from a quick & dirty example - maybe instructable...

A good place to start in this case is to break down the code you have, and understand what each couple of lines actually achieve - then you’ll be in a much stronger place to rework or rewrite it to your requirement.

Unless this is simply for a hobby project, I reckon the existing code is too fragile to be reliable - even if it did work.

It never hurts to start again, and will be much faster to write - and you will understand why it does what it does!

He wants to know what show up on the Serial Monitor, the program sends messages to it, according to it's progress, so to know what it says on the Serial Monitor gives you and us a much better idea what is going on. I can also speculate that if a file exists already you the program just tries to create another one with the same name over and over again, but that is speculation.

jdle:
I have tried all your suggestions

You did not use the AutoFormat tool to indent your code to make it easier to read. The way it is now it is almost impossible to tell where the different parts start and end.

...R

Thanks for help me understand. Attached you can see the Serial Output on monitor. It moves the file save in SD card right after 1st snapshot and keep run over and over. I stop program and look at the SD it shows blank because it deleted. What I want is, the camera will take 10 snapshots and save in SD before removing files. Because I want to use the LabVIEW to download all 10 images to PC before removing files from SD.

If I common the “RemoveFile();” function, it will take 10 snapshots and stop.

//RemoveFile();

Looks like I didn’t attach Serial output

I tried Autoformat code below

#include <SdFat.h>
#include "CameraC329.h"
#define CAMERA_BAUD 14400
//SdFat SD; // arduino or Leonardo
SdFatSdio SD; //Using teensy 3.6
//const int chipSelect = 4;//arduino or Leonardo
File photoFile;

CameraC329 camera;

uint32_t writtenPictureSize;
void getPicture_callback(uint32_t pictureSize, uint16_t packetSize, uint32_t packetStartPosition, byte* packet)
{
  photoFile.write(packet, packetSize);
  Serial.write (packet, packetSize);
  writtenPictureSize += packetSize;
  if (writtenPictureSize >= pictureSize)
  {
    photoFile.flush();
    photoFile.close();
    Serial.println(F("SUCCESS"));
  }
}

void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println(F("************************************************************"));
  Serial.println(F("Serial READY"));
  /////////////////
  Serial1.begin(CAMERA_BAUD);
  while (!Serial1)
    Serial.println(F("Camera connected"));
  delay(2000);

  if (!SD.begin()) {
    Serial.println(F("SD initialization failed or missing SD card"));
    return;
  }
  else {
    Serial.println(F("Began SD Card"));
  }
  delay(1000);// wait for SD open file
}

void loop() {

  char filename[] = "C329Ph00.jpg";
  for (uint8_t i = 0; i < 10; i++) {
    filename[6] = i / 10 + '0';
    filename[7] = i % 10 + '0';
    if (! SD.exists(filename)) { // only open a new file if it doesn't exist
      photoFile = SD.open(filename, FILE_WRITE);
      photoFile.seek(0);
      writtenPictureSize = 0;
      Serial.println(F("opened picture file"));
      break;  // leave the loop!
    }
  }
  if (! photoFile)
  {
    Serial.println(F("couldn't create file or SD card full"));
    return;
    Serial.println(F("File created."));
    Serial.print(F("Logging to: "));
    Serial.println(filename);
    delay (1000);
  }
  if (!camera.sync()) {
    Serial.println(F("Camera Sync Failed"));
    return;
  }
  else
  {
    Serial.println(F("Camera ON"));
    delay(1000);
  }
  if (!camera.initialize(CameraC329::BAUD14400, CameraC329::CT_JPEG, CameraC329::PR_160x120, CameraC329::JR_320x240)) {
    Serial.println(F("Camera Failed Initialized"));
    return;
  }
  else
  {
    Serial.println(F("Camera Initialized"));
  }
  if (!camera.setQuality(CameraC329::QL_BEST)) {
    Serial.println(F("Fail to Set quality Good"));
  }
  if (!camera.takeSnapshot(CameraC329::PT_SNAPSHOT)) {
    Serial.println(F("Fail to take snapshot"));
  }
  if (!camera.getPicture(CameraC329::PT_JPEG_PREVIEW, &getPicture_callback)) {
    Serial.println(F("Fail to get Picture "));
  }
  else {
    Serial.println(F("Pictured Successfully"));
  }
  if (!camera.reset(CameraC329::RT_STATE))
    photoFile.close(); {
    Serial.println(F("Finished writing data to file"));
    Serial.println(F("-------------------------------------------------"));
    delay(500);
    RemoveFile();
  }
}
//RemoveFile ******************************************************
void RemoveFile() {
  Serial.println(F("(1)Remove the file...")); //debug.
  Serial.println("Removing example.txt...");
  char filename[13];
  for (uint8_t i = 0; i < 3; i++)
  {
    sprintf(filename, "C329Ph%02d.jpg", i);
    SD.remove(filename);
    if (SD.exists(filename))
    {
      Serial.print(F("Could not delete file"));
      Serial.println(filename);
    }
    else
    {
      Serial.println(F("All files deleted"));
    }
    if (!SD.exists(filename)) {
      Serial.println(F("All .jpg extension files gone"));
      Serial.println(F("******************************************"));
    }
  }
}

If you had added a debugging serial.print(filename) statement at the point where you create the filename in the for() loop you will see that you you are not creating a new file name each time.

The for loop should be changed for an if conditional like this

void setup() {
  Serial.begin(115200);
}

void loop() {
  char filename[] = "C329Ph00.jpg";
  static byte i = 0;
  if (i <= 10)
  {
    filename[6] = i / 10 + '0';
    filename[7] = i % 10 + '0';
    Serial.println(filename);
    delay(1000);
    i++;
  }
}

This does not make sense. There is a ELSE missing

    if (! photoFile)
    {
        Serial.println(F("couldn't create file or SD card full"));
        return;
        Serial.println(F("File created."));
        Serial.print(F("Logging to: "));
        Serial.println(filename);
        delay (1000);
    }

...R

This does not make sense. There is a ELSE missing

There's also useless code after the return statement.

PaulS:
There's also useless code after the return statement.

Perhaps it would not be useless if there was an ELSE

...R

Robin2:
Perhaps it would not be useless if there was an ELSE

...R

Now, I see what you mean. The else, and curly braces, needs to be between the return and the rest of the code in the block.

Or, maybe the OP means something completely different.