Hello,
I am working on a project that will take a picture based on an accelerometer reading. I had all of the pieces working- the camera storing, the accelerometer input conditions etc, but when I put it all together it's a bit unhappy. Basically whenever I tell it to take a picture, everything restarts. I'm posting a slightly shorter version of my code (it's really long), but I think it might abe a hardware problem, do you know if there is a voltage drop when the microSD card is storing a file? I am using a 5Volt Arduino Promini
Thanks!
#include <Adafruit_VC0706.h>
#include <SD.h>
#include <AcceleroMMA7361.h>
AcceleroMMA7361 accelero; //accelerometer
int xval[5];
int yval[5];
int zval[5];
int xacc[5];
int yacc[5];
int zacc[5];
int orient[5];
int orientloop;
int orientave;
int ytot;
int yave;
// comment out this line if using Arduino V23 or earlier
#include <SoftwareSerial.h>
#define chipSelect 10
// Using SoftwareSerial (Arduino 1.0+) or NewSoftSerial (Arduino 0023 & prior):
#if ARDUINO >= 100
// On Uno: camera TX connected to pin 2, camera RX to pin 3:
SoftwareSerial cameraconnection = SoftwareSerial(2, 3);
#else
NewSoftSerial cameraconnection = NewSoftSerial(2, 3);
#endif
Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);
void setup() {
Serial.begin(9600);
#if !defined(SOFTWARE_SPI)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
if(chipSelect != 53) pinMode(53, OUTPUT); // SS on Mega
#else
if(chipSelect != 10) pinMode(10, OUTPUT); // SS on Uno, etc.
#endif
#endif
Serial.println("VC0706 Camera 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
//cam.setImageSize(VC0706_640x480); // biggest
// cam.setImageSize(VC0706_320x240); // medium
cam.setImageSize(VC0706_160x120); // small
cam.setMotionDetect(false); // turn it off
//initialize accelero
accelero.begin(5, 8, 7, 6, A4, A5, A2);
accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G
accelero.calibrate();
}
void loop() {
for (int i= 0; i < 3; i++) //fill array of latest data
{
xval[i] = accelero.getXRaw();
yval[i] = accelero.getYRaw();
zval[i] = accelero.getZRaw();
xacc[i] = accelero.getXAccel();
yacc[i] = accelero.getYAccel();
zacc[i] = accelero.getZAccel();
orient[i] = accelero.getOrientation();
orientloop += orient[i];
ytot +=yacc[i];
}
orientave = orientloop % 3;
yave= ytot % 3;
delay(100);
Serial.print("\noave: ");
Serial.print(orientave);
Serial.print("\tzave: ");
Serial.print(yave);
if(( yave < 0 ) && (orientave > 1 )){ //flick of wrist and upwards motion
cam.takePicture();
Serial.println("Picture taken!");
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;
}
}
File imgFile = SD.open(filename, FILE_WRITE);
uint16_t jpglen = cam.frameLength();
Serial.print(jpglen, DEC);
Serial.println(" byte image");
Serial.print("Writing image to "); Serial.print(filename);
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);
//Serial.print("Read "); Serial.print(bytesToRead, DEC); Serial.println(" bytes");
jpglen -= bytesToRead;
}
imgFile.close();
Serial.println("...Done!");
// cam.resumeVideo();
}
}

