/* camera.ino, D. Brooks, October 2019 Uses miniature TTL JPEG camera, Adafuirt ID 1386, to snap pics and save them on SD card. Will also work (without any modification?) with larger version, ID 397. Note that it takes ~30 s to write 640x480 images to the SD card, so give enough time between taking a pic, using delay(). Be careful with the wiring, to get the RX and RX pins wired correctly and to avoid solder bridges on the very small board when you connect wires or headers. TX pin through 10k resistor to digital 3 and another 10k from TX pin to ground. RX pin directly to digital 3. By setting the CLOCK constant, you can choose consecutively numbered file names from 0-99 (CLOCK 0) or files named with the day, hour, minute, and second, DDHHMMSS.JPG, (CLOCK1), using the DS1307 clock module. (If required, set date/time with RTC_2 before using.) */ #define CLOCK 1 #include #include #include #if CLOCK #include #include "RTClib.h" RTC_DS1307 RTC; // clock on old data logger shield //RTC_PCF8523 RTC; // clock on new data logger shield #endif const int SDpin=10; File picfile; SoftwareSerial cameraconnection = SoftwareSerial(2, 3); Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection); void setup() { Serial.begin(9600); if (! SD.begin(SDpin)) {Serial.println(F("Card failed.")); delay(50); exit(0); } Serial.println(F("SD card initialized.")); #if CLOCK Wire.begin(); RTC.begin(); #endif } void loop() { if (cam.begin()) { // 640x480, 320x240, or 160x120 cam.reset(); cam.setImageSize(VC0706_320x240); Serial.println(F("Camera found and reset.")); } else { Serial.println(F("No camera found.")); } delay(3000) ; if (!cam.takePicture()) { Serial.println(F("Failed to snap.")); delay(50); exit(0); } Serial.println(F("Picture taken.")); uint16_t jpglen=cam.frameLength(); Serial.print(F("Writing ")); Serial.print(jpglen, DEC); Serial.print(F(" byte image.")); #if CLOCK // Opens file named DDHHMMSS.CSV DateTime now=RTC.now(); int DD=now.day(),HH=now.hour(),MM=now.minute(),SS=now.second(); String filename=""; if (DD<10) filename ='0'+DD; else filename =DD; if (HH<10) filename+='0'+HH; else filename+=HH; if (MM<10) filename+='0'+MM; else filename+=MM; if (SS<10) filename+='0'+SS; else filename+=SS; filename+=".JPG"; picfile=SD.open(filename,FILE_WRITE); #else // Stores up to 100 pics in files numbered 0-99. char filename[]="PICS__00.JPG"; for (uint8_t i=0; i<100; i++) { filename[6]=i/10+'0'; filename[7]=i%10+'0'; if (!SD.exists(filename)) { picfile=SD.open(filename,FILE_WRITE); break; } } #endif Serial.print(F("Writing to ")); Serial.print(filename); int32_t time = millis(); // 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; // change 32 to 64 for speedup may not work with all setups! uint8_t bytesToRead = min(32, jpglen); buffer = cam.readPicture(bytesToRead); picfile.write(buffer, bytesToRead); // Every 2K, give a little feedback so it doesn't appear locked up if(++wCount >= 64) { Serial.print('.'); wCount = 0;} jpglen -= bytesToRead; } picfile.close(); time=millis()-time; Serial.print("done, "); Serial.print(time); Serial.println(" ms"); delay(120000L); // two minutes between pics }