Arduino phototrap howto

this was my first “serious” project using Arduino.

Here it goes.. what I used:

I also added a led to indicate when a photo is taken (blink) or issues in execution (turned ON).

SD slave is IO 10. SPI (camera) slave is IO 9. PIR sensor input is plugged into IO 3 and triggers interrupts that will take photos once motion is detected – and keeps taking as long as sensor is HIGH.

cam wiring:

f47f35e8-2499-11e5-8d05-2a768cc76818.jpg

Source code (debug code – serial print – is comment out for performance):

#include <UTFT_SPI.h>
#include <SD.h>
#include <Wire.h>
#include <ArduCAM.h>
#include <SPI.h>
#include “memorysaver.h”

#if defined(__arm__)
#include <itoa.h>
#endif

//PIR int
const byte pirInterruptPin = 3;
uint8_t start_capture = 0;
const int LED = 5;
#define SD_CS 10
// set pin 9 as the slave select for SPI:
const int SPI_CS = 9;

ArduCAM myCAM(OV2640,SPI_CS);
UTFT myGLCD(SPI_CS);
boolean takePic = false;
boolean discardFirst = false;
int blinkTime;
boolean completed = false;

void setup()
{
uint8_t vid,pid;
uint8_t temp;
#if defined(__SAM3X8E__)
Wire1.begin();
#else
Wire.begin();
#endif
Serial.begin(115200);
Serial.println(“ArduCAM Start!”);
// set the SPI_CS as an output:
pinMode(SPI_CS, OUTPUT);
pinMode(LED, OUTPUT);
//pinMode(BUTTON, INPUT);

// initialize SPI:
SPI.begin();
//Check if the ArduCAM SPI bus is OK
myCAM.write_reg(ARDUCHIP_TEST1, 0x55);
temp = myCAM.read_reg(ARDUCHIP_TEST1);
if(temp != 0x55)
{
Serial.println(“SPI interface Error!”);
digitalWrite(LED, HIGH);
while(1);
}

//Change MCU mode
myCAM.set_mode(MCU2LCD_MODE);

myGLCD.InitLCD();

//Check if the camera module type is OV2640
myCAM.rdSensorReg8_8(OV2640_CHIPID_HIGH, &vid);
myCAM.rdSensorReg8_8(OV2640_CHIPID_LOW, &pid);
if((vid != 0x26) || (pid != 0x42))
{
Serial.println(“Can’t find OV2640 module!”);
digitalWrite(LED, HIGH);
while(1);
}
else
Serial.println(“OV2640 detected”);

//Change to BMP capture mode and initialize the OV2640 module
myCAM.set_format(BMP);

myCAM.InitCAM();

//Initialize SD Card
if (!SD.begin(SD_CS))
{
Serial.println(“SD Card Error”);
digitalWrite(LED, HIGH);
while (1);    //If failed, stop here
}
else
Serial.println(“SD Card detected!”);

delay(5000);

Serial.println(“set int…”);
pinMode(pirInterruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pirInterruptPin), intPic, RISING);
blinkLed();
}

void blinkLed()
{
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
}

void intPic() {
//Serial.println(“Start capture!”);
takePic = true;
completed = true;
}

void loop()
{
String fileName;
File outFile;
byte buf[256];
static int i = 0;
static int k = 0;
static int n = 0;
uint8_t temp,temp_last;
int total_time = 0;
//int current = millis();
//Wait trigger from shutter buttom
//  if(myCAM.get_bit(ARDUCHIP_TRIG , SHUTTER_MASK))
if(takePic)
{
myCAM.set_mode(MCU2LCD_MODE);
myCAM.set_format(JPEG);
myCAM.InitCAM();

//myCAM.OV2640_set_JPEG_size(OV2640_640x480);
myCAM.OV2640_set_JPEG_size(OV2640_1600x1200);
//myCAM.OV2640_set_JPEG_size(OV2640_800x600);
//myCAM.OV2640_set_JPEG_size(OV2640_1024x768);
//Wait until buttom released
while(myCAM.get_bit(ARDUCHIP_TRIG, SHUTTER_MASK));
//delay(1000);
start_capture = 2;
takePic = false;
}else
if(digitalRead(pirInterruptPin) == HIGH)
{
start_capture = 1;
}

if(start_capture && completed)
{
//Flush the FIFO
myCAM.flush_fifo();
//Clear the capture done flag
myCAM.clear_fifo_flag();
//Start capture
myCAM.start_capture();

//Serial.println(“Start Capture: ” + start_capture);
completed = false;
}else
if(myCAM.get_bit(ARDUCHIP_TRIG ,CAP_DONE_MASK))
{

//Serial.println(“Capture Done!”);

//Construct a file name
k++;
fileName = “z”;
fileName += k;
fileName += “.jpg”;

//Open the new file
outFile = SD.open(fileName, O_WRITE | O_CREAT | O_TRUNC);
if (!outFile)
{
Serial.println(“open file failed”);
Serial.println(fileName);
digitalWrite(LED, HIGH);
while(1);
}
//total_time = millis();
i = 0;
temp = myCAM.read_fifo();
//Write first image data to buffer
buf[i++] = temp;

//Read JPEG data from FIFO
while( (temp != 0xD9) | (temp_last != 0xFF) )
{
temp_last = temp;
temp = myCAM.read_fifo();
//Write image data to buffer if not full
if(i < 256)
buf[i++] = temp;
else
{
//Write 256 bytes image data to file
outFile.write(buf,256);
i = 0;
buf[i++] = temp;
}
}
//Write the remain bytes in the buffer
if(i > 0)
outFile.write(buf,i);

//Close the file
outFile.close();
//total_time = millis() – total_time;
//Serial.print(“Total time used:”);
//Serial.print(total_time, DEC);
//Serial.println(” millisecond”);
//Clear the capture done flag
myCAM.clear_fifo_flag();
//Clear the start capture flag
//start_capture–;
if(start_capture– == 1)
{
//pinMode(pirInterruptPin, OUTPUT);
//digitalWrite(pirInterruptPin, LOW);
//pinMode(pirInterruptPin, INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(pirInterruptPin), intPic, RISING);
blinkLed();
}
completed = true;

//myCAM.set_format(BMP);
//myCAM.InitCAM();
//isShowFlag = true;
}
}

enjoy!

cheers.

Anúncios
Esse post foi publicado em Não categorizado. Bookmark o link permanente.

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair / Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair / Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair / Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair / Alterar )

Conectando a %s