G'day everyone! I was wondering if someone could shed some light for me. I have a Seriall TTL Jpg camera taking a pic everytime a button is pressed, stores the image on SD in chronological order ex: IMAGE00.jpg, IMAGE01.jpg, IMAGE02.jpg and so on. when the image is saved to the SD card I can then call up the sd card to open a file and print file to serial monitor. BAMMM that's all good to go. What I am needing to do is when the file is stored on SD card after button is pressed, cam takes pic, stores in chronological order, i need to print that file (the new Pic everytime) to serial. In my sketch I can only open the file that I name in the code ex: File myfile = SD.open("IMAGE00.txt");......but I need to open the newest pic taken everytime it gets saved on SD card. Cam snaps pic, saves to SD, open that file on serial monitor. Is there someway to save on SD then open the newest file? Here is the part that I have written already, I have tried to search extensively on internet with no success, any help would be appreciated.
#include <Adafruit_VC0706.h>
#include <SD.h>
#include <SPI.h>
#include <Adafruit_NeoPixel.h>
#define SS_SD 4 //SD card CS pin
const int buttonPin = 6;
int buttonState = 0; // variable for reading the pushbutton status
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, 7, NEO_GRB + NEO_KHZ400);
// Using hardware serial on DUE: camera TX to RX1 DUE,camera RX to TX1 DUE
Adafruit_VC0706 cam = Adafruit_VC0706(&Serial1);
void setup()
{
//Serial3.begin(57600); //Seperate RX and TX
Serial.begin(57600); //Monitor
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
strip.begin(); //initialize Light
delay(5); //Have to put this in or PIXEL stays on
strip.show(); //set light to off
Serial.println("TEST");
// Try to locate the camera
if (cam.begin())
{
Serial.println("Camera Found:");
}
else
{
Serial.println("No camera found?");
return;
}
// see if the card is present and can be initialized:
if (!SD.begin(SS_SD))
{
Serial.println("SD Card init failed...");
strip.setPixelColor(0, strip.Color(255, 255, 0)); // bright yellow color.
strip.show();
return;
}
cam.reset(); //Delay Needed or it won't work
delay(500);
//cam.setImageSize(VC0706_640x480);
//cam.setImageSize(VC0706_320x240);
cam.setImageSize(VC0706_160x120);
delay(100);
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");
cam.setCompression(255); //srts cam comp 0-255 (255 being most)
delay(500);
}
void loop()
{
Trigger();
}
void Trigger()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
Serial.println("Snap in 1 sec...");
delay(1000);
takePicture();
//delay (500);
//takePicture();
delay(2000);
sendToMonitor();
}
}
void takePicture()
{
if (! cam.takePicture())
{
Serial.println("Failed to snap!");
}
else
{
Serial.println("Picture taken!");
}
// Create an image with the name IMAGExx.JPG
char filename[13];
strcpy(filename, "IMAGE00.jpg");
for (int i = 0; i < 100; i++)
{
filename[5] = '0' + i / 10;
filename[6] = '0' + i % 10;
// create if does not exist, do not open existing, write, sync after write
if (! SD.exists(filename))
{
break;
}
}
// Open the file for writing
File imgFile = SD.open(filename, FILE_WRITE);
Serial.print("create ");
Serial.println(filename);
// Get the size of the image (frame) taken
uint16_t jpglen = cam.frameLength();
Serial.print("Read Bytes ");
Serial.print(jpglen, DEC);
Serial.print(" byte image.");
int32_t time = millis();
pinMode(8, OUTPUT);
byte wCount = 0; // For counting # of writes
//cam.getPicture(jpglen);
uint8_t *buffer;
while (jpglen != 0)
{
uint8_t bytesToRead = min(64, jpglen);
buffer = cam.readPicture(bytesToRead);
imgFile.write(buffer, bytesToRead);
//Serial.print("Read "); Serial.print(bytesToRead, DEC); Serial.println(" bytes");
jpglen -= bytesToRead;
}
imgFile.close();
time = millis() - time;
Serial.println("Done!");
Serial.print("Took ");
Serial.print(time);
Serial.println(" ms");
Serial.println();
//Resume Camera
cam.resumeVideo();
}
void sendToMonitor ()
{
File myfile = SD.open("IMAGE00.jpg"); //need to change this so I can open newest image
if (myfile)
{
Serial.println("Start");
while (myfile.available())
{
Serial.write(myfile.read());
}
Serial.println("Finished");
myfile.close();
Serial.println();
Serial.println("FILE SENT TO MONITOR");
strip.setPixelColor(0, strip.Color(0, 150, 0)); // bright green color.
strip.show();
delay(4000);
// turn LED off:
strip.setPixelColor(0, strip.Color(0, 0, 0));
strip.show();
}
else
{
Serial.println("error opening the file");
strip.setPixelColor(0, strip.Color(255, 0, 0)); // Red color.
strip.show();
}
}