Hello. I'm building a timelapse camera using the Arduino Nano, TTL JPEG camera, and micro sd card module. I am successfully capturing and storing photos at the interval desired. However, the photos being stored seem to have a remnant of a previously stored photo of the same filename. The windows photo viewer will flash the image of a photo that was stored and deleted before displaying the most recent. My code is mostly modified from the Adafruit tutorial for the camera module. I have deduced that the problem is a storage issue on the the Nano itself because the problem persist when I switch cameras, sd cards, and sd modules, but keep the same Nano board. If there's a way to "hard reset" the Nano board to clear left over memory between timelapse sessions, I'm not finding it with a search. Any advice for this newb or link to advice would be greatly appreciated.
#include <Adafruit_VC0706.h>
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>
#define chipSelect 4
#if ARDUINO >= 100
SoftwareSerial cameraconnection = SoftwareSerial(2, 3);
#else
NewSoftSerial cameraconnection = NewSoftSerial(2, 3);
#endif
Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);
long fileIndex = 0;
int ledIR = 8;
int photoRead = A1;
void setup() {
#if !defined(SOFTWARE_SPI)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
if(chipSelect != 53) pinMode(53, OUTPUT); // SS on Mega
#else
if(chipSelect != 4) pinMode(4, OUTPUT); // SS on Uno, etc.
#endif
#endif
pinMode(ledIR,OUTPUT);
Serial.begin(9600);
// Create an image with the name 0000000.JPG
char filename[13];
strcpy(filename, "00000000.JPG");
for (long i = 0; i < 1000000; i++) {
long value = i;
for (int pos=7; pos >= 0; pos--) {
filename[pos] = '0' + value%10;
value = value/10;
}
// create if does not exist, do not open existing, write, sync after write
if (! SD.exists(filename)) {
fileIndex = i;
break;
}
}
}
void loop() {
Serial.println("VC0706 Camera snapshot test");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
//don't do anything more:
return;
}
// Try to locate the camera
if (cam.begin()) {
Serial.println("Camera Found:");
} else {
Serial.println("No camera found?");
return;
}
// Print out the camera version information (optional)
char *reply = cam.getVersion();
if (reply == 0) {
Serial.print("Failed to get version");
} else {
Serial.println("-----------------");
Serial.print(reply);
Serial.println("-----------------");
}
// Set the picture size - you can choose one of 640x480, 320x240 or 160x120
// Remember that bigger pictures take longer to transmit!
//cam.setImageSize(VC0706_640x480); // biggest
//cam.setImageSize(VC0706_320x240); // medium
cam.setImageSize(VC0706_160x120); // small
delay(200);
cam.setDownsize(0x22);
// You can read the size back from the camera (optional, but maybe useful?)
uint8_t imgsize = cam.getImageSize();
Serial.print("Image size: ");
//if (imgsize == VC0706_640x480) Serial.println("640x480");
//if (imgsize == VC0706_320x240) Serial.println("320x240");
if (imgsize == VC0706_160x120) Serial.println("160x120");
Serial.println(analogRead(photoRead));
Serial.println("Snap in 3 secs...");
delay(2750);
if(analogRead(photoRead)>500) digitalWrite(ledIR,HIGH);
delay(250);
if (! cam.takePicture())
Serial.println("Failed to snap!");
else
Serial.println("Picture taken!");
digitalWrite(ledIR,LOW);
// Create an image with the name 0000000.JPG
char filename[13];
strcpy(filename, "00000000.JPG");
long value = fileIndex;
for (int pos=7; pos >= 0; pos--) {
filename[pos] = '0' + value%10;
value = value/10;
}
fileIndex = (fileIndex+1) % 10000000;
// Open the file for writing
File imgFile = SD.open(filename, FILE_WRITE);
Serial.print(filename);
// Get the size of the image (frame) taken
uint16_t jpglen = cam.frameLength();
// Serial.print("Storing ");
// Serial.print(jpglen, DEC);
// Serial.print(" byte image.");
int32_t time = millis();
pinMode(8, OUTPUT);
// Read all the data up to # bytes!
byte wCount = 0; // For counting # of writes
while (jpglen > 0) {
// read 32 bytes at a time;
uint8_t *buffer;
uint8_t bytesToRead = min(64, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
buffer = cam.readPicture(bytesToRead);
imgFile.write(buffer, bytesToRead);
if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up
//Serial.print('.');
wCount = 0;
}
//Serial.print("Read "); Serial.print(bytesToRead, DEC); Serial.println(" bytes");
jpglen -= bytesToRead;
}
imgFile.close();
time = millis() - time;
Serial.flush();
//Serial.println("done!");
//Serial.print(time); Serial.println(" ms elapsed");
//digitalWrite(6, LOW);
delay(1000);
}