I am trying to call a function that takes a picture with a mini spy camera, but the function won't be called and no picture is taken

#include <SPI.h>
#include <SD.h>

File myFile;

int trig = 0;
int led = 1;

void TakePicture() {

digitalWrite(trig, LOW);
digitalWrite(led, LOW);

digitalWrite(trig, HIGH);
digitalWrite(led, HIGH);
}

// (c) Michael Schoeffler 2017, http://www.mschoeffler.de

#include "Wire.h" // This library allows you to communicate with I2C devices.

const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.

int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
int16_t temperature; // variables for temperature data

char tmp_str[7]; // temporary variable used in convert function

char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
sprintf(tmp_str, "%6d", i);
return tmp_str;
}

void setup() {

// put your setp code here, to run once:
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);

while (!Serial) {
; //wait for serial port to connect
}
Serial.print("Initializing SD card...");

if (!SD.begin(4)) {
Serial.println("initalization failed!");
}

// Spycam code
// initialize the digital pins as output.
pinMode(led, OUTPUT);
pinMode(trig, OUTPUT);

digitalWrite(led, HIGH);
digitalWrite(trig, HIGH);

TakePicture();

}

// Hold HIGH and trigger quick (<250ms) LOW to take a photo. Holding LOW and trigger HIGH starts/stops video recording

void loop() {

Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
Wire.requestFrom(MPU_ADDR, 7 * 2, true); // request a total of 7*2=14 registers
{
myFile = SD.open("report.txt", FILE_WRITE);
if (myFile) {
myFile.print(convert_int16_to_str(accelerometer_x)); // aX
myFile.print(","); myFile.print(convert_int16_to_str(accelerometer_y)); // aY
myFile.print(","); myFile.print(convert_int16_to_str(accelerometer_z)); // aZ
// the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
myFile.print(", "); myFile.print(temperature / 340.00 + 36.53); // temp
myFile.print(","); myFile.print(convert_int16_to_str(gyro_x)); // gX
myFile.print(","); myFile.print(convert_int16_to_str(gyro_y)); // gY
myFile.print(","); myFile.print(convert_int16_to_str(gyro_z)); // gZ
myFile.println();
myFile.close();
}
// "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
accelerometer_x = Wire.read() << 8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
accelerometer_y = Wire.read() << 8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
accelerometer_z = Wire.read() << 8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
temperature = Wire.read() << 8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
gyro_x = Wire.read() << 8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
gyro_y = Wire.read() << 8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
gyro_z = Wire.read() << 8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)

// print out data
Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));
Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));
// the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
Serial.print(" | tmp = "); Serial.print(temperature / 340.00 + 36.53);
Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));
Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));
Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));
Serial.println();

}
if (accelerometer_y > 500)
{TakePicture();
delay(500);
}
{
myFile = SD.open("report.txt", FILE_WRITE);
if (myFile) {
myFile.print(",");
myFile.close();
}

}

}Preformatted text

After reading the "How to get the best out of this forum" post, please edit your post to add code tags.

Explain what "won't work" means. What did you expect to happen, and what happens instead?

2 possible issues here:

  1. If you are using an UNO/Nano or most of the other ATmega328P-based Arduino boards then you shouldn't use pins 0 and 1 unless you are NOT using the serial interface.
  2. I don't know what the trig pin is for but I assume it causes the picture to be taken. Does it need to stay LOW for a certain amount of time to be detected by the camera?

Please let us know what hardware you are using.

If you hold the camera on HIGH (which is what we did) and quickly trigger LOW, it will take a photo. We tested this exact code in the function TakePicture on its own and it worked and took a picture. Also we are using an Arduino UNO R3.

Just get used to the idea that you cannot use a Uno's pin 0 and 1. If you want to know why, research will give you the answer.

CODE TAGS, where are your code tags? Why can't you find the code tags and use them? Is the CODE TAG bin empty? Did someone forget to order new CODE TAGS?

#include <SPI.h>
#include <SD.h>

File myFile;

int trig = 0;
int led = 1;

void TakePicture() {

digitalWrite(trig, LOW);
digitalWrite(led, LOW);

digitalWrite(trig, HIGH);
digitalWrite(led, HIGH);
}

// (c) Michael Schoeffler 2017, [http://www.mschoeffler.de](http://www.mschoeffler.de/)

#include "Wire.h" // This library allows you to communicate with I2C devices.

const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.

int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
int16_t temperature; // variables for temperature data

char tmp_str[7]; // temporary variable used in convert function

char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
sprintf(tmp_str, "%6d", i);
return tmp_str;
}

void setup() {

// put your setp code here, to run once:
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);

while (!Serial) {
; //wait for serial port to connect
}
Serial.print("Initializing SD card...");

if (!SD.begin(4)) {
Serial.println("initalization failed!");
}

// Spycam code
// initialize the digital pins as output.
pinMode(led, OUTPUT);
pinMode(trig, OUTPUT);

digitalWrite(led, HIGH);
digitalWrite(trig, HIGH);

TakePicture();

}

// Hold HIGH and trigger quick (<250ms) LOW to take a photo. Holding LOW and trigger HIGH starts/stops video recording

void loop() {

Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
Wire.requestFrom(MPU_ADDR, 7 * 2, true); // request a total of 7*2=14 registers
{
myFile = SD.open("report.txt", FILE_WRITE);
if (myFile) {
myFile.print(convert_int16_to_str(accelerometer_x)); // aX
myFile.print(","); myFile.print(convert_int16_to_str(accelerometer_y)); // aY
myFile.print(","); myFile.print(convert_int16_to_str(accelerometer_z)); // aZ
// the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
myFile.print(", "); myFile.print(temperature / 340.00 + 36.53); // temp
myFile.print(","); myFile.print(convert_int16_to_str(gyro_x)); // gX
myFile.print(","); myFile.print(convert_int16_to_str(gyro_y)); // gY
myFile.print(","); myFile.print(convert_int16_to_str(gyro_z)); // gZ
myFile.println();
myFile.close();
}
// "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
accelerometer_x = Wire.read() << 8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
accelerometer_y = Wire.read() << 8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
accelerometer_z = Wire.read() << 8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
temperature = Wire.read() << 8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
gyro_x = Wire.read() << 8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
gyro_y = Wire.read() << 8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
gyro_z = Wire.read() << 8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)


// print out data
Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));
Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));
// the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
Serial.print(" | tmp = "); Serial.print(temperature / 340.00 + 36.53);
Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));
Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));
Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));
Serial.println();


}
if (accelerometer_y > 500)
{TakePicture();
delay(500);
}
{
myFile = SD.open("report.txt", FILE_WRITE);
if (myFile) {
myFile.print(",");
myFile.close();
}

}

done here is with code tags

Do you understand that using pin 0 and pin 1 of the MCU is not really the route to go?