I am trying to make a camera trap system for a friend. i.e. A system to automatically start and stop a movie camera when triggered by a wild animal. I have used an IF beam for the sensor and this seems to work well. The camera needs two contacts to be shorted to start a movie and the same contact shorted again to stop the recording. I will be using optocoupler on pin 3 to do the shorting.
Below is the code that I am having problems with.
The LED on pin 9 is there just to indicate when filming is taking place. I dont have the actual camera with me.
long shutterOnInterval is the period that the contacts will be shorted (probably dosn't need to be this long)
long filmPeriod is the time of filming
So the idea is that when the IR beam is broken the short is made by the shutterpin, the camera then films for the period set, then another short is sent to stop the filming.
At the moment it works ok so long as I re trigger in less than 5 seconds following a filming period. If I dont, the shorting signal is sent every 5 seconds even without any beam breaking.
If I dont break the beam at all the shorting signal is again sent every 5 seconds.
I suspect my problem is due to some timer related command not being in the right place but I just cant see it. Any help much appreciated.
Eventually I would like it if the filming period could be extended if further beam breaks occur during the initial filming period, but that's for later.
int IR_sensor = 11; // input from IR sensor
int shutter = 3; // led pin
int filming_led = 9;
long shutterOnInterval = 500;
long filmPeriod = 5000;
int timerRunning = 0;
int filming = 0;
unsigned long startTime = 0;
unsigned long timeNow = 0;
void setup() {
pinMode(IR_sensor, INPUT);
pinMode(shutter, OUTPUT);
pinMode (filming_led, OUTPUT);
Serial.begin(9600);
}
void loop() {
//read the IR sensor. If HIGH, start the on/off signal
if ((digitalRead(IR_sensor) == HIGH) && (timerRunning == 0))
{
startTime = millis();
digitalWrite(shutter, HIGH);
digitalWrite(filming_led, HIGH);
timerRunning = 1;
filming = 1;
Serial.println("signal and filming started");
}
//stop the on/off signalafter the lamponinterval. filming still running
if ((timerRunning == 1) && (timeNow - startTime) >= shutterOnInterval )
{
digitalWrite(shutter, LOW);
timerRunning = 0;
Serial.println("signal stopped BUT Still filming");
}
//if filming period is done, stop filming and send on/off signal
timeNow = millis();
if (((timeNow - startTime) > filmPeriod) && (filming = 1))
{
digitalWrite(shutter, HIGH);
digitalWrite(filming_led, LOW);
filming = 0;
timerRunning = 1;
startTime = millis();
Serial.println("filming ended and signal to stop is started");
}
//stop the on/off signal after the lamponinterval and after the filmPeriod
timeNow = millis();
if ((timerRunning == 1) && (timeNow - startTime) >= shutterOnInterval )
{
digitalWrite(shutter, LOW);
timerRunning = 0;
Serial.println("filming stopped and signal SENT");
}
}