// https://forum.arduino.cc/t/movie-camera-trap-sketch-problems/1250853
// https://wokwi.com/projects/395926058392791041
const byte IR_sensor = 11; // input from IR sensor
const byte shutter = 3; // led pin
const byte filming_led = 9;
unsigned long shutterOnInterval = 333;
unsigned long filmPeriod = 5000;
unsigned long startShutter = 0;
unsigned long startFilming = 0;
unsigned long now;
enum fsmState {IDLE, START, FILM, FINI, DONE} theState;
void setup() {
pinMode(IR_sensor, INPUT);
pinMode(shutter, OUTPUT);
pinMode (filming_led, OUTPUT);
Serial.begin(9600);
Serial.println("IR_sensor_V3_X.ino");
theState = IDLE;
}
void loop() {
bool irHit = digitalRead(IR_sensor) == HIGH; // pulled_down pushbutton, PRESST
now = millis();
switch (theState) {
case IDLE :
if (irHit) {
Serial.print("start 1 ");
startShutter = now;
startFilming = now;
digitalWrite(shutter, HIGH);
digitalWrite(filming_led, HIGH);
Serial.println("signal and filming started");
theState = START;
}
break;
case START :
if (now - startShutter >= shutterOnInterval) {
digitalWrite(shutter, LOW);
theState = FILM;
Serial.println("2 signal stopped BUT Still filming");
}
// intentional no break here - check filming timer too, then exclusively
case FILM :
if (irHit) startFilming = now; // activity moves the marker along
if (now - startFilming > filmPeriod) {
Serial.print("start 3 ");
digitalWrite(shutter, HIGH);
digitalWrite(filming_led, LOW);
startShutter = now;
theState = FINI;
Serial.println("3 filming ended and signal to stop is started");
}
break;
case FINI :
if (now - startShutter >= shutterOnInterval) {
Serial.print("start 4 ");
digitalWrite(shutter, LOW);
theState = DONE;
Serial.println("4 filming stopped and signal SENT");
}
break;
case DONE :
// auto re-arm. motion had shown long quiet period, so unconditionally.
Serial.println("5 system idle awaits motion");
theState = IDLE;
break;
}
}