Take pictures at regular intervals


I'm using an Arduino Uno to control a motion activated camera in Alaska. I've adapted a simple script I found on the internet and it works great. It basically says "if object is detected, then focus camera and take picture". I just got a new flash that powers off if not used for 30 minutes. I need to adapt my code so the camera takes a picture every 29 minutes, too keep the flash awake.

Here's the abridged version of the the loop that is doing the work in my current code:

void loop(){ if (digitalRead(PIRPin)) { takePicture(); //function that activates focus and shutter pins to take picture } }

I would greatly appreciated any advice on how to modify this code so that it does it's current job, but also takes pictures at fixed intervals.

Thanks a lot

Link to pictures from this project: http://www.flickr.com/photos/j-armstrong/

Use the function millis() (http://arduino.cc/en/Reference/Millis) to note the time when a picture is taken. Then add a clause to your if statement so that it checks for PIRPin or ( ( millis() - timeOfLastPicture ) >= ( 29 * 60 * 1000 ) )

[code]void loop(){
    if (digitalRead(PIRPin)) {   
        takePicture();    //function that activates focus and shutter pins to take picture
             } }

Use the millis() function to activate every 29 mins:

unsigned long timestamp;
unsigned long interval = 1740000;  //29 X 60 X1000 = millis in 29 mins

void setup() { 
     timestamp = millis();

void loop()

     if (digitalRead(PIRPin))
        takePicture();    //function that activates focus and shutter pins to take picture

     if ((millis() - timestamp > interval))
        takePicture();    //take a picture if 29 mins has elapsed
        timestamp = millis();


Darn - beat me to it - I like coding deadman switches…

One small change I’d suggest - reset the counter whenever you take a picture via PIRPin; that way you maximize your battery/picture memory:


     if (digitalRead(PIRPin))

       takePicture();    //function that activates focus and shutter pins to take picture
timestamp = millis(); // <— rest counter now we’ve taken a pic

Good point.

I would also suggest a possible minimum and simple project upgrade would be to use a simple LDR sensor wired as a voltage divider to a analog input pin. Read the light level and don’t perform a 29 min flash if it’s daytime, thus saving battery/memory resources. Note sure how you recover from a powered down flash unit, but it it’s just a matter of taking a ‘waste’ picture, then restart the 29 auto timer when ambient light level is at near sunset?


Thanks so much for all the quick feedback! The example code is really helpful. The ambient light sensor is a good idea… I actually like some flash during the day too, but saving power is always good.

I see you're using Canon gear, and I assume your flash is a 420EX or similar, without the SE (Save Energy) mode override. My 420EX sleeps after just 90s, but it's possible to wake up these speedlites without taking a wasted picture via the "half-press" pin of the remote.

You could do this just before taking a picture, but you'd have to wait a few seconds to allow the flash to re-charge. If you need a faster response time then you can keep the flash awake by pulsing the "half-press" pin every minute or so.

PS. You've got some great pictures there. Incidentally, it was building a photo-trap that led me to the Arduino project.

pulsing the “half-press” pin every minute or so.

…here’s a way to do this using timer interrupts, which leaves your main loop free to do other stuff.

#include <Streaming.h>
#define DEBUG 1
volatile int ticks = 0;

ISR(TIMER1_COMPA_vect) { // Timer-1 OC1A-match interrupt routine
  if (++ticks==60) {
    pulseHalfPressPin(); // do the half-press every minute
  if (DEBUG) Serial << "Ticks = " << ticks << endl;
  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);

void setup() {
  pinMode(13, OUTPUT); digitalWrite(13, LOW); // pulse LED on pin-13 every second
  if (DEBUG) Serial.begin(19200);

  // setup 16-bit Timer-1 to generate an interrupt every second
  // assumes a system clock of 16MHz
  TCCR1A = 0; // set timer mode: clear on compare match
  TCCR1B = 13; // set timer mode: clear on compare match / set prescaler: timer clock = 16MHz/1024 = 15625 Hz
  OCR1A  = 15625; // set compare value: reset timer when it reaches 15625
  TIMSK1 = 2; // set OC1A match interrupt
  sei(); // enable interrupts

void loop() {
  // do stuff

There is a fantastic program that adds multiple features to supported Canon cameras. The program is CHDK. This software adds features to relatively inexpensive Canon point and shoot cameras and turns them into a powerhouse. No Arduino is involved. Google CHDK to learn about its features.