Hi all, guess what, I'm new to all this!
I have written a sketch (see below) to control the starting and stopping of a digital SLR camera in video mode built into a trap. The sketch works fine if I use delay() in the final function to control the stopping of the recording after a set time period, but if I try to use the millis() function instead it just doesn't seem to carry out the stop rec function. I have tried all manner of things to get it working but after days of going round in circles I thought it best to ask for help from those who know better.
The original sketch I used as a starting block was for an Intervalometer, Loading..., my thanks to Joe!
#include <LiquidCrystal.h> //library for LCD control
// LiquidCrystal display with:
// RS, EN, D4, D5, D6, D7
LiquidCrystal lcd( 8, 9, 10, 11, 12, 13); //Set which LCD pins are used for data
//I-O setup
const int potentiometer = 0; //Potentiometer analog input pin
const int mode = 16; //Mode button input pin
const int shutter = 2; //Shutter relay output pin
const int trig = 18; //trigger switch input pin
const int stoprec = 3; //stop record output pin
//Variables
long interval = 0; //time between shutter presses
long previousMillis = 0; //previous millisecond value
int hold = 1000; //time shutter should be held open
int M = 0; //Mode button state
int Mstate = 0; //previous Mode button state
int StartSet = 0; //Mode state
int pot; //potentiometer reading
long range; //potentiometer position
int count = 0; //picture count
int ModeSW = 0; //Low-High set mode toggle
int T = 0; //trigger state
int prevT = 0; // previous trigger state
int ModeT = 0; //low high trig mode toggle
//Press & Hold variables
#define debounce 50 // ms debounce period to prevent flickering when pressing or releasing the button
#define holdTime 1000 // ms hold period: how long to wait for press+hold event
long btnDnTime; // time the button was pressed down
long btnUpTime; // time the button was released
boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered
long trigDnTime; // time the trigger was pressed
long trigUpTime; // time the trigger was released
boolean ignoreTrigUp = false; // whether to ignore the trigger release because the click+hold was triggered
//Initialize I-O
void setup() {
pinMode(shutter, OUTPUT);
pinMode(stoprec, OUTPUT);
pinMode(mode, INPUT);
digitalWrite(mode, HIGH);
pinMode(trig, INPUT);
digitalWrite(trig, HIGH);
lcd.begin(16, 2); //set LCD for 16 columns & 2 rows of display
}
void loop(){
M = digitalRead(mode); //read Mode button state
//Test for button pressed and store the down time
if (M == HIGH && Mstate == LOW && (millis() - btnUpTime) > long(debounce))
{
btnDnTime = millis();
}
//Test for button release and store the up time
if (M == LOW && Mstate == HIGH && (millis() - btnDnTime) > long(debounce))
{
btnUpTime = millis();
}
//Test for button held down for longer than the hold time
if (M == HIGH && (millis() - btnDnTime) > long(holdTime))
{
StartSet = !StartSet; //toggle Set mode to Timing mode or vice versa
ignoreUp = true;
btnDnTime = millis();
}
Mstate = M;
//Test for Set Mode
if(StartSet == LOW){
lcd.home(); //set cursor to LCD column 1, row 1
lcd.print("Set Mode "); //display "Set Mode" on LCD
count = 0; //reset picture count
LowRange(); // run set mode function
}
//Test for Timing Mode
else{
lcd.home(); //set cursor to LCD column 1, row 1
lcd.print("Timing Mode"); //display "Timing Mode" on LCD
lcd.setCursor(0, 1); //set cursor to LCD column 1, row 2
lcd.print(count); //display picture count number on LCD
lcd.print(" Pictures "); //display "Pictures" on LCD
TestTrig(); // call test trig function
}
}
// Set timing function
long LowRange(){
long seconds; //shutter timing interval in seconds
pot = analogRead(potentiometer); //read potentiometer value
range = map(pot, 0, 1023, 24, 1); //divide potentiometer range into 5 second steps
interval = 5000 * range; //convert steps into 60 second range in milliseconds
seconds = interval/1000; //convert milliseconds to seconds
lcd.setCursor(0, 1); //set cursor to LCD column 1, row 2
lcd.print(seconds); //display shutter timing interval in seconds on LCD
lcd.print(" seconds "); //display "seconds" on LCD
return interval; //return shutter timing interval value to loop
}
//Test for trigger function
void TestTrig(){
T = digitalRead(trig); //read trigger button state
//Test for trigger pressed and store the down time
if (T == HIGH && prevT == LOW && (millis() - trigUpTime) > long(debounce))
{
trigDnTime = millis();
}
//Test for button release and store the up time
if (T == LOW && prevT == HIGH && (millis() - trigDnTime) > long(debounce))
{
trigUpTime = millis();
}
//Test for button held down for longer than the hold time
if (StartSet != LOW && T == HIGH && (millis() - trigDnTime) > long(holdTime))
{
StartRec(); //Call the start rec function
ignoreTrigUp = true;
trigDnTime = millis();
}
prevT = T;
}
//Start recording function
int StartRec(){
previousMillis = millis(); //record the time the recording was triggered
digitalWrite(shutter, HIGH); //trigger recording start relay
lcd.clear(); //clear the lcd
lcd.home(); //set cursor to LCD column 1, row 1
lcd.print("Start Recording"); //display "Start Recording" on LCD
delay(hold); //delay for the set delay time
lcd.clear(); //clear the lcd
lcd.home(); //set cursor to column 1, row,1
lcd.print("Recording!"); //display "Recording! " on the lcd
digitalWrite(shutter, LOW); //turn off shutter relay
delay(3000); //delay 3 seconds
StopRec(); //call the stop rec function
}
//Stop recording function
int StopRec(){
unsigned long currentMillis = millis(); //define currentMillis as unsigned long
if(currentMillis - previousMillis > interval){ //test that timing interval has passed
digitalWrite(stoprec, HIGH); //trigger shutter relay coil
lcd.clear();
lcd.home(); //set cursor to LCD column 1, row 1
lcd.setCursor(0, 1);
lcd.print("Stop Recording"); //display "Stop Recording" on LCD
Serial.print("Stop Recording"); //print "stop recording" to serial monitor
delay(hold); //wait preset shutter hold time
lcd.clear();
digitalWrite(stoprec, LOW); //release shutter relay coil
++count; //increment picture count
return count; //return picture count value to loop
}
else return count;
}
I'm sure its something bleedin obvious but any help would be greatly appreciated.
Thanks XD