Camera time lapse with fixed start time.

My son is building a cabin and wanted to document the build. So I am trying to hack a cheap SJ4000 clone action camera to do that. I am using an Arduino nano, an RTC module, and a 4 relay module.

IN1 controls the on/off switch and mode selection. IN2 controls the shutter. Both are hard-wired to their prospective switches on the SJ4000 clone. IN1 fires to turn the camera on, after a short delay it fires again selecting the still photo mode of the SJ4000 clone. Again after a short delay IN2 fires triggering the shutter. Another short delay and IN1 fires again turning off the camera.

The plan is to turn the camera on at 0800 and take a photo every hour until 1800, then turn on again at 0800 the next day and repeat ad infinitum.

I copied the basis for this sketch from [Arduino] DIY (stupid and cheap) Plants Irrigator | Hack | Lenotta by Edoardo Odorico and Lorenzo Farnararo.

From their page, I also made appropriate changes to RTClib.h and RTClib.cpp that they posted.

CameraTimer4.ino (1.13 KB)

Add the following function to RTClib.cpp.txt (312 Bytes)

Modify the class DateTime in RTClib.h.txt (965 Bytes)

One thing I see, in setup(), you might want to, instead of:

pinMode(CH1, OUTPUT);
pinMode(CH2, OUTPUT);
digitalWrite(CH1, HIGH);
digitalWrite(CH2, HIGH);

Do:

digitalWrite(CH1, HIGH);
digitalWrite(CH2, HIGH);
pinMode(CH1, OUTPUT);
pinMode(CH2, OUTPUT);

That should keep the relays from blinking on at startup.

I would do something like this (compiles but untested):

//

#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 RTC;
int CH1 = 7;
int CH2 = 8;
bool done = false;

void setup () {
  //Initialize the serial port, wire library and RTC module
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
  digitalWrite(CH1, HIGH);
  digitalWrite(CH2, HIGH);
  pinMode(CH1, OUTPUT);
  pinMode(CH2, OUTPUT);

  //IF YOU USE RTC FOR THE FIRST TIME: Remove the comment from the following line to set up the module time.
  //RTC.adjust(DateTime( __TIME__));
}

void loop () {
  DateTime now = RTC.now();

  //We check the time and sent through the serial port every 3s

  if (!done && now.minute() == 00 && now.hour() >= 8 && now.hour() <= 18)
  {
    done = true; // do the following once on the hour

    digitalWrite(CH1, LOW);
    delay(250);
    digitalWrite(CH1, HIGH);
    delay(5000);

    digitalWrite(CH1, LOW);
    delay(250);
    digitalWrite(CH1, HIGH);
    delay(2000);

    digitalWrite(CH2, LOW);
    delay(250);
    digitalWrite(CH2, HIGH);
    delay(3000);

    digitalWrite(CH1, LOW);
    delay(250);
    digitalWrite(CH1, HIGH);
  }
  if(now.minute() >= 2) //wait 'til minute > 0 so no repeat
    done = false;
}

You can just remember the previous minute you read, and check for it having changed since last read, then you
don't have the crufty stuff to reset done to false.

if (now.minute() != prev_minute)
{
  prev_minute = now.minute() ;
  if (now.minute() == 0 and now.hour() == ......)
  {
    .....
  }
}

Howdy,

being rather new at this stuff, I don't understand that at all. could you explain it a bit more?

Thanks

I am not really clear on looping in Android, but from what I understand this ino will take photos for 10 hours, and then since it's inside the void loop will go back to check the time. It will keep doing this forever. Is that a correct assumption?

I am not really clear on looping in Android, but from what I understand this ino will take photos for 10 hours, and then since it’s inside the void loop will go back to check the time. It will keep doing this forever. Is that a correct assumption?

Yes, that was the intention, since the delays in your loop take 11 seconds, I removed the “crufty stuff” at the bottom, it should run the loop every hour between 8:00 and 18:00 inclusive, like I said, compiles but untested, give it a try and customize as needed.

#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 RTC;
int CH1 = 7;
int CH2 = 8;
bool done = false;

void setup () {
  //Initialize the serial port, wire library and RTC module
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
  digitalWrite(CH1, HIGH);
  digitalWrite(CH2, HIGH);
  pinMode(CH1, OUTPUT);
  pinMode(CH2, OUTPUT);

  //IF YOU USE RTC FOR THE FIRST TIME: Remove the comment from the following line to set up the module time.
  //RTC.adjust(DateTime( __TIME__));
}

void loop () {
  DateTime now = RTC.now();

  //We check the time and sent through the serial port every 3s

  if (!done && now.minute() == 0 && now.hour() >= 8 && now.hour() <= 18) // else jump to start of loop()
  {                                                   
    done = true; // do the following once on the hour

    digitalWrite(CH1, LOW);
    delay(250);
    digitalWrite(CH1, HIGH);
    delay(5000);

    digitalWrite(CH1, LOW);
    delay(250);
    digitalWrite(CH1, HIGH);
    delay(2000);

    digitalWrite(CH2, LOW);
    delay(250);
    digitalWrite(CH2, HIGH);
    delay(3000);

    digitalWrite(CH1, LOW);
    delay(250);
    digitalWrite(CH1, HIGH);
    done = false;         // reset "done" for next time
  }
}

@MarkT: “crufty”? :slight_smile:

After testing for more than 24 hours, it appears to work as intended.

Thank You, All for your help.

Attached is the final version of the sketch.

CameraTimer.ino.ino (1.91 KB)

Add the following function to RTClib.cpp.txt (312 Bytes)

Modify the class DateTime in RTClib.h.txt (965 Bytes)

Well, things have gone bad. This morning I tried to modify the sketch so that it would take a photo every half hour instead of each hour. Before making any changes I verified the sketch and got the following error:

error: no 'uint8_t DateTime::dayOfYear() const' member function declared in class 'DateTime'
uint8_t DateTime::dayOfYear() const {

I didn't change anything to the sketch (that I'm aware of).

I re-downloaded the RTClib and applied the required modifications and still got the same er

It compiles correctly in Arduino Create.

Don't see anything about "dayOfYear" in your sketch, just use the original RTClib library.
Please post your latest code and the full error text.

edgemoron:
Don't see anything about "dayOfYear" in your sketch, just use the original RTClib library.
Please post your latest code and the full error text.

That fixed that problem. :slight_smile: That's what I get for not knowing much about this, and copying other folks work. :frowning:

Great! A Karma point for persistence. :slight_smile: