// No servo one press to end?
// https://forum.arduino.cc/t/learning-about-fsm-what-are-the-states-here/905546/30
const byte trigPin = 6; // Low input from button to D6
const byte yellowLEDPin = A0; // LED flashes at intervals for KA photo
const byte redLEDPin = A1; // Lit while video recording
enum { Off = LOW, On = HIGH }; // easier than rewiriing the LEDs
const int WaitBeforeRelease = 200;
const int WaitAfterRelease = 200;
byte trigPinState;
int trigCounter = 0;
# define VideoTime 3500;
// unused # define Snapshotime 5000;
unsigned long interval = 5000;
enum { ST_SNAPSHOT, ST_VIDEO };
// -----------------------------------------------------------------------------
void
singleCUBEServoPress (
int cnt )
{
while (cnt--) {
digitalWrite (yellowLEDPin, On);
delay (WaitBeforeRelease);
digitalWrite (yellowLEDPin, Off);
delay (WaitAfterRelease);
}
}
// -----------------------------------------------------------------------------
void
snapshot (void)
{
singleCUBEServoPress (1);
}
// -----------------------------------------------------------------------------
void
videoStart (void)
{
singleCUBEServoPress (2);
trigCounter++;
Serial.print ("Started video ");
Serial.print ("trigCounter = ");
Serial.println (trigCounter);
digitalWrite (redLEDPin, On);
// digitalWrite (redLEDPin, Off); // Stays low until next video record
}
// -----------------------------------------------------------------------------
void
videoEnd (void)
{
singleCUBEServoPress (1);
Serial.println (" End video ");
digitalWrite (redLEDPin, Off);
}
// -----------------------------------------------------------------------------
void loop ()
{
static unsigned long msecExpire = interval;
static int state = ST_SNAPSHOT;
unsigned long msec = millis ();
if (msec > msecExpire) {
switch (state) {
case ST_VIDEO:
videoEnd ();
state = ST_SNAPSHOT;
msecExpire = msec + interval;
break;
case ST_SNAPSHOT:
default:
snapshot ();
msecExpire = msec + interval;
break;
}
}
byte trig= digitalRead (trigPin);
if (trigPinState != trig) {
trigPinState = trig;
delay(50); // poor man's debounce
if (trig == LOW) {
if (state != ST_VIDEO) {
msecExpire = msec + VideoTime;
state = ST_VIDEO;
videoStart ();
} else {
Serial.println("new motion - extend video period");
msecExpire = msec + VideoTime; // extend video duration
}
}
}
}
// -----------------------------------------------------------------------------
void setup ()
{
Serial.begin (115200); // it's 2021
Serial.println("hello trigger video");
pinMode (trigPin, INPUT_PULLUP);
trigPinState = digitalRead (trigPin);
pinMode (redLEDPin, OUTPUT);
pinMode (yellowLEDPin, OUTPUT);
digitalWrite (redLEDPin, Off);
digitalWrite (yellowLEDPin, Off);
}