Work on re-write to class-based intval2.4 firmware. Reduced memory footprint by using properly-sized ints. Improving naming to reduce confusion among timing variables.

This commit is contained in:
Matt McWilliams 2025-11-24 06:38:02 -08:00
parent bcc6b78371
commit 91bd002074
4 changed files with 764 additions and 415 deletions

View File

@ -0,0 +1,222 @@
#include "Intval2.h"
void Intval2::begin () {
PinsInit();
ButtonsInit();
//timed_open = OpenTiming();
}
void Intval2::loop () {
timer = millis();
Button(0);
Button(1);
Button(2);
Button(3);
if (timelapse && delaying) {
TimelapseWatchDelay();
}
if (running) {
if (timed_exposure) {
TimedExposureReadMicroswitch();
} else {
ReadMicroswitch();
}
}
if (!running && !timelapse && !delaying){
delay(LOOP_DELAY);
}
}
void Intval2::PinsInit () {
pinMode(PIN_MOTOR_FORWARD, OUTPUT);
pinMode(PIN_MOTOR_BACKWARD, OUTPUT);
pinMode(PIN_MICROSWITCH, INPUT_PULLUP);
pinMode(PIN_INDICATOR, OUTPUT);
}
void Intval2::ButtonsInit () {
for (int i = 0; i < 4; i++) {
pinMode(BUTTONS[i], INPUT_PULLUP);
}
}
void Intval2::TimelapseWatchDelay () {
if (timer - delay_start >= timelapse_delay) {
delaying = false;
Camera();
}
}
boolean Intval2::WatchMicroswitchDelay () {
if (timer - frame_start >= MICROSWITCH_DELAY) {
return true;
}
return false;
}
void Intval2::ReadMicroswitch () {
if (WatchMicroswitchDelay()) {
microswitch_position = digitalRead(PIN_MICROSWITCH);
if (microswitch_position == LOW
&& microswitch_primed == false) {
microswitch_primed = true;
} else if (microswitch_position == HIGH
&& microswitch_primed == true) {
Stop();
}
delay(2);//smooths out signal
}
}
void Intval2::TimedExposureReadMicroswitch () {
//TODO: FIX
if (!timed_exposure_open) {
if (timer - frame_start > timed_open
&& timer - frame_start < timed_open + timed_delay) {
TimedExposurePause();
} else if (timer - frame_start > timed_open + timed_delay) {
microswitch_position = digitalRead(PIN_MICROSWITCH);
if (microswitch_position == HIGH) {
Stop();
}
delay(2);//smooths out signal
}
}
//TODO: FIX
if (timed_exposure_open && timer - frame_start > timed_open + timed_delay) {
TimedExposureStart();
}
}
void Intval2::Stop () {
delay(10); //examine
analogWrite(PIN_MOTOR_FORWARD, 0);
analogWrite(PIN_MOTOR_BACKWARD, 0);
running = false;
microswitch_primed = false;
if (direction) {
counter += 1;
} else {
counter -= 1;
}
exposure = timer - frame_start;
if (timed_exposure) {
timed_exposure_avg = round((timed_exposure_avg + exposure) / 2);
} else {
avg = round((avg + exposure) / 2);
}
if (timelapse) {
delaying = true;
delay_start = millis();
}
}
void Intval2::TimedExposureStart () {
timed_exposure_open = false;
if (direction) {
analogWrite(PIN_MOTOR_FORWARD, MOTOR_PWM);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else {
analogWrite(PIN_MOTOR_BACKWARD, MOTOR_PWM);
analogWrite(PIN_MOTOR_FORWARD, 0);
}
}
void Intval2::TimedExposurePause () {
timed_exposure_open = true;
analogWrite(PIN_MOTOR_FORWARD, 0);
analogWrite(PIN_MOTOR_BACKWARD, 0);
}
void Intval2::Button (uint8_t index) {
int val = digitalRead(BUTTONS[index]); // ;)
if (val != button_states[index]) {
if (val == LOW) { // pressed
button_times[index] = millis();
} else if (val == HIGH) { // not pressed
button_time = millis() - button_times[index]; //time?
ButtonEnd(index, button_time);
}
}
button_states[index] = val;
}
void Intval2::ButtonEnd (uint8_t index, long time) {
if (index == 0) {
if (time > 1000) {
if (!timelapse && !running) {
timelapse = true;
Output(2, 75);
Camera();
}
} else {
if (timelapse) {
timelapse = false;
//Output(2, 75);
} else {
Camera();
}
}
} else if (index == 1) { //set delay
if (time < 42) {
timelapse_delay = 42;
Output(1, 500);
} else {
timelapse_delay = time;
Output(2, 250);
}
} else if (index == 2) { // set speed
if (time >= 1000) {
//timed_delay = time - BOLEX_C;
timed_exposure = true;
Output(2, 250);
} else if (time < 1000) {
//timed_delay = 0;
timed_exposure = false;
Output(1, 500);
}
} else if (index == 3) { //set direction
if (time < 1000) {
direction = true;
Output(1, 500);
} else if (time > 1000) {
direction = false;
Output(2, 250);
}
}
time = 0;
}
void Intval2::Output (uint8_t number, uint16_t len) {
for (int i = 0; i < number; i++) {
Indicator(true);
delay(len);
Indicator(false);
delay(42);
}
}
void Intval2::Indicator (boolean state) {
if (state) {
digitalWrite(PIN_INDICATOR, HIGH);
} else {
digitalWrite(PIN_INDICATOR, LOW);
}
}
String Intval2::State () {
if (timed_exposure) {
return String(timed_exposure_avg);
}
return String(avg);
}

View File

@ -0,0 +1,82 @@
#ifndef INTVAL2
#define INTVAL2
#include <Arduino.h>
class Intval2 {
private:
// PIN CONST
const uint8_t PIN_INDICATOR = 13; //Arduino Nano LED
const uint8_t PIN_MOTOR_FORWARD = 9;
const uint8_t PIN_MOTOR_BACKWARD = 10;
const uint8_t PIN_MICROSWITCH = 19; //A5
const uint8_t BUTTONS[4] = {3, 4, 5, 6}; //trigger, delay, speed, direction
//MOTOR CONST
const uint16_t MOTOR_RPM = 120;
const float MOTOR_OPEN_FORWARD = 0.25;
const float MOTOR_OPEN_BACKWARD = 0.75;
const uint8_t MOTOR_PWM = 255; // Not varying this for now
const uint16_t MICROSWITCH_DELAY = 50;
//ETC CONST
const uint16_t LOOP_DELAY = 10;
//STATE VAR
volatile uint32_t counter = 0;
volatile boolean direction = true;
volatile boolean running = false;
volatile boolean timelapse = false;
volatile boolean timed_exposure = false;
volatile boolean delaying = false;
volatile boolean open = false; //is the shutter open
volatile boolean timed_exposure_open = false; //is the shutter open only during a timed exposure
volatile uint8_t microswitch_position = 0;
volatile boolean microswitch_primed = false;
//BUTTON VAR
volatile int button_states[4] = {1, 1, 1, 1};
volatile long button_times[4] = {0, 0, 0, 0};
volatile long button_time = 0;
//TIME VAR
volatile unsigned long timer;
volatile unsigned long frame_start = 0;
volatile unsigned long delay_start = 0;
volatile unsigned long timelapse_delay = 42; //time between frames during timelapse
volatile String timed_exposure_str = "600";
volatile unsigned long timed_exposure_val = 600;
volatile unsigned long timed_open = 100; //ms after start_frame to pause
volatile unsigned long timed_delay = 0;
volatile unsigned long exposure = 0;
volatile unsigned long avg = 600;
volatile unsigned long timed_exposure_avg = 600;
void PinsInit();
void ButtonsInit();
void Button (uint8_t index);
void ButtonEnd (uint8_t index, long time);
boolean WatchMicroswitchDelay();
void TimelapseWatchDelay();
void ReadMicroswitch();
void TimedExposureReadMicroswitch();
void TimedExposureStart();
void TimedExposurePause();
void Stop();
void Output(uint8_t number, uint16_t len);
void Indicator(boolean state);
public:
void begin();
void loop();
void Camera();
String State();
};
#endif

View File

@ -1,445 +1,44 @@
#include "McopySerial.h"
#include "Intval2.h"
//Can be controlled via serial, with mcopy and filmout_manager
//Buttons are optional
//Exposure controls assumes use of a 120RPM motor
//Uses L298N H-bridge breakout board
//Target board is an Arduino Nano
/*
----------------------------------------------------
Microswitch (use INPUT_PULLUP!!)
GND-----\ | \-----PIN
----------------------------------------------------
*/
const int MOTOR_RPM = 120;
const int BOLEX_C = round((133 / (1.66 * 360)) * 1000); //bolex exposure constant
const int FAST_PWM = 255;
/* ------------------------------------------------
* pins
* ------------------------------------------------*/
//Arduino Nano
const int PIN_INDICATOR = 13;
const int PIN_MOTOR_FORWARD = 9;
const int PIN_MOTOR_BACKWARD = 10;
const int PIN_MICRO = 19;
//6, 5, 4
//1, 2, 3
const int BUTTON[4] = {3, 4, 5, 6}; //trigger, delay, speed, direction
const float FWD_OPEN = 0.4;
const float BWD_OPEN = 0.7;
/* ------------------------------------------------
* loop
* ------------------------------------------------*/
const int LOOP_DELAY = 10;
/* ------------------------------------------------
* state
* ------------------------------------------------*/
volatile int button_state[4] = {1, 1, 1, 1};
volatile long button_time[4] = {0, 0, 0, 0};
volatile long buttontime = 0;
volatile boolean sequence = false;
volatile boolean running = false;
volatile boolean cam_dir = true;
volatile boolean delaying = false;
volatile boolean timed = false;
volatile int counter = 0;
volatile int micro_position = 0;
volatile boolean micro_primed = false;
unsigned long timer = 0;
unsigned long frame_start = 0;
unsigned long delay_start = 0;
String timed_str = "600";
unsigned long timed_val = 600;
unsigned long timed_open = 100; //ms after start_frame to pause
volatile boolean timed_paused = false;
unsigned long timed_delay = 0;
unsigned long timed_last = 0;
unsigned long timed_avg = 600;
unsigned long i_avg = 600;
volatile int fwd_speed = FAST_PWM;
volatile int bwd_speed = FAST_PWM;
volatile long seq_delay = 42;
volatile boolean is_open = false;
/* ------------------------------------------------
* serial
* ------------------------------------------------*/
McopySerial mc;
Intval2 intval;
volatile char cmd_char = 'z';
void setup() {
mc.begin(mc.CAMERA_IDENTIFIER);
PinsInit();
ButtonsInit();
intval.begin();
}
void loop() {
void loop () {
cmd_char = mc.loop();
cmd(cmd_char);
timer = millis();
timed_open = OpenTiming();
Button(0);
Button(1);
Button(2);
Button(3);
if (sequence && delaying) {
WatchDelay();
}
if (running) {
if (timed) {
ReadTimed();
} else {
ReadMicro();
}
}
if (!running && !sequence && !delaying){
delay(LOOP_DELAY);
}
intval.loop();
}
void cmd (char val) {
if (val == mc.CAMERA) {
Camera();
//intval::Camera();
} else if (val == mc.CAMERA_FORWARD) {
CameraDirection(true);
//CameraDirection(true);
} else if (val == mc.CAMERA_BACKWARD) {
CameraDirection(false);
//CameraDirection(false);
} else if (val == mc.CAMERA_OPEN) {
CameraOpen();
//CameraOpen();
} else if (val == mc.CAMERA_CLOSE) {
CameraClose();
//CameraClose();
} else if (val == mc.CAMERA_EXPOSURE) {
CameraExposure();
//CameraExposure();
} else if (val == mc.STATE) {
State();
}
}
//sending "0" will reset to default exposure time
void CameraExposure () {
timed_str = mc.getString();
timed_val = timed_str.toInt();
if (timed_val < 600) {
timed_val = 600;
timed_str = "600";
timed = false;
timed_open = OpenTiming();
} else {
//timed_delay = timed_val - BOLEX_C;
timed = true;
}
mc.confirm(mc.CAMERA_EXPOSURE);
mc.log("Set exposure time to: ");
mc.log(timed_str);
}
void PinsInit () {
pinMode(PIN_MOTOR_FORWARD, OUTPUT);
pinMode(PIN_MOTOR_BACKWARD, OUTPUT);
pinMode(PIN_MICRO, INPUT_PULLUP);
pinMode(PIN_INDICATOR, OUTPUT);
}
void ButtonsInit () {
for (int i = 0; i < 4; i++) {
pinMode(BUTTON[i], INPUT_PULLUP);
}
}
void Button (int index) {
int val = digitalRead(BUTTON[index]);
if (val != button_state[index]) {
if (val == LOW) { // pressed
button_time[index] = millis();
//button_start(index);
} else if (val == HIGH) { // not pressed
buttontime = millis() - button_time[index];
ButtonEnd(index, buttontime);
}
}
button_state[index] = val;
}
/*
* dormant for now
* void button_start (int index) {
if (index == 0) {
}
}*/
void ButtonEnd (int index, long buttontime) {
if (index == 0) {
if (buttontime > 1000) {
if (!sequence && !running) {
sequence = true;
Output(2, 75);
Camera();
}
} else {
if (sequence) {
sequence = false;
//Output(2, 75);
} else {
Camera();
}
}
} else if (index == 1) { //set delay
if (buttontime < 42) {
seq_delay = 42;
Output(1, 500);
} else {
seq_delay = buttontime;
Output(2, 250);
}
} else if (index == 2) { // set speed
if (buttontime >= 1000) {
//timed_delay = buttontime - BOLEX_C;
timed = true;
Output(2, 250);
} else if (buttontime < 1000) {
//timed_delay = 0;
timed = false;
Output(1, 500);
}
} else if (index == 3) { //set direction
if (buttontime < 1000) {
cam_dir = true;
Output(1, 500);
} else if (buttontime > 1000) {
cam_dir = false;
Output(2, 250);
}
}
buttontime = 0;
}
void Indicator (boolean state) {
if (state) {
digitalWrite(PIN_INDICATOR, HIGH);
} else {
digitalWrite(PIN_INDICATOR, LOW);
}
}
void Output (int number, int len) {
for (int i = 0; i < number; i++) {
Indicator(true);
delay(len);
Indicator(false);
delay(42);
}
}
void Camera () {
frame_start = millis();
if (cam_dir) {
analogWrite(PIN_MOTOR_FORWARD, fwd_speed);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else {
analogWrite(PIN_MOTOR_BACKWARD, bwd_speed);
analogWrite(PIN_MOTOR_FORWARD, 0);
}
running = true;
micro_primed = false;
}
long OpenTiming () {
return cam_dir ? (long) (FWD_OPEN * i_avg) : (long) (BWD_OPEN * i_avg);
}
void CameraOpen () {
if (cam_dir) {
analogWrite(PIN_MOTOR_FORWARD, fwd_speed);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else {
analogWrite(PIN_MOTOR_BACKWARD, bwd_speed);
analogWrite(PIN_MOTOR_FORWARD, 0);
}
running = true;
micro_primed = false;
delay(OpenTiming());
analogWrite(PIN_MOTOR_FORWARD, 0);
analogWrite(PIN_MOTOR_BACKWARD, 0);
micro_position = digitalRead(PIN_MICRO);
if (micro_position == LOW) {
micro_primed = true;
}
mc.confirm(mc.CAMERA_OPEN);
mc.log("camera_open()");
is_open = true;
running = false;
}
void CameraClose () {
bool microswitch_open = false;
if (is_open) {
if (cam_dir) {
analogWrite(PIN_MOTOR_FORWARD, fwd_speed);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else {
analogWrite(PIN_MOTOR_BACKWARD, bwd_speed);
analogWrite(PIN_MOTOR_FORWARD, 0);
}
while (!microswitch_open) {
micro_position = digitalRead(PIN_MICRO);
if (micro_position == HIGH) {
microswitch_open = true;
}
delay(2);
}
delay(10);
analogWrite(PIN_MOTOR_FORWARD, 0);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else {
micro_position = digitalRead(PIN_MICRO);
if (micro_position == HIGH) {
mc.log("WARNING: Camera already closed");
}
}
mc.confirm(mc.CAMERA_CLOSE);
mc.log("camera_close()");
is_open = false;
}
boolean ReadDelay () {
if (timer - frame_start >= timed_open) {
return true;
}
return false;
}
void WatchDelay () {
if (timer - delay_start >= seq_delay) {
delaying = false;
Camera();
}
}
void ReadTimed () {
if (!timed_paused) {
if (timer - frame_start > timed_open
&& timer - frame_start < timed_open + timed_delay) {
PauseTimed();
} else if (timer - frame_start > timed_open + timed_delay) {
micro_position = digitalRead(PIN_MICRO);
if (micro_position == HIGH) {
Stop();
}
delay(2);//smooths out signal
}
}
if (timed_paused && timer - frame_start > timed_open + timed_delay) {
StartTimed();
}
}
void PauseTimed () {
timed_paused = true;
analogWrite(PIN_MOTOR_FORWARD, 0);
analogWrite(PIN_MOTOR_BACKWARD, 0);
}
void StartTimed () {
timed_paused = false;
if (cam_dir) {
analogWrite(PIN_MOTOR_FORWARD, fwd_speed);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else {
analogWrite(PIN_MOTOR_BACKWARD, bwd_speed);
analogWrite(PIN_MOTOR_FORWARD, 0);
}
}
void ReadMicro () {
if (ReadDelay()) {
micro_position = digitalRead(PIN_MICRO);
if (micro_position == LOW
&& micro_primed == false) {
micro_primed = true;
} else if (micro_position == HIGH
&& micro_primed == true) {
Stop();
}
delay(2);//smooths out signal
}
}
void Stop () {
delay(10);
analogWrite(PIN_MOTOR_FORWARD, 0);
analogWrite(PIN_MOTOR_BACKWARD, 0);
running = false;
micro_primed = false;
if (cam_dir) {
counter += 1;
} else {
counter -= 1;
}
timed_last = timer - frame_start;
if (timed) {
timed_avg = round((timed_avg + timed_last) / 2);
} else {
i_avg = round((i_avg + timed_last) / 2);
}
mc.confirm(mc.CAMERA);
mc.log("Camera completed");
mc.log(String(timed_last));
if (sequence) {
delaying = true;
delay_start = millis();
}
}
void CameraDirection (boolean state) {
cam_dir = state;
if (state) {
timed_open = OpenTiming();
mc.confirm(mc.CAMERA_FORWARD);
mc.log("camera_direction(true)");
} else {
timed_open = OpenTiming();
mc.confirm(mc.CAMERA_FORWARD);
mc.log("camera_direction(false)");
//State();
}
}
void State () {
String stateString = String(mc.STATE);
stateString += String(mc.CAMERA_EXPOSURE);
if (timed) {
stateString += String(timed_avg);
} else {
stateString += String(i_avg);
}
//stateString += intval2.State();
stateString += String(mc.STATE);
mc.sendString(stateString);
}
}

View File

@ -0,0 +1,446 @@
#include "McopySerial.h"
//Can be controlled via serial, with mcopy and filmout_manager
//Buttons are optional
//Exposure controls assumes use of a 120RPM motor
//Uses L298N H-bridge breakout board
//Target board is an Arduino Nano
/*
----------------------------------------------------
Microswitch (use INPUT_PULLUP!!)
GND-----\ | \-----PIN
----------------------------------------------------
*/
const int MOTOR_RPM = 120;
const int BOLEX_C = round((133 / (1.66 * 360)) * 1000); //bolex exposure constant
const int FAST_PWM = 255;
/* ------------------------------------------------
* pins
* ------------------------------------------------*/
//Arduino Nano
const int PIN_INDICATOR = 13;
const int PIN_MOTOR_FORWARD = 9;
const int PIN_MOTOR_BACKWARD = 10;
const int PIN_MICRO = 19;
//6, 5, 4
//1, 2, 3
const int BUTTONS[4] = {3, 4, 5, 6}; //trigger, delay, speed, direction
const float FWD_OPEN = 0.4;
const float BWD_OPEN = 0.7;
/* ------------------------------------------------
* loop
* ------------------------------------------------*/
const int LOOP_DELAY = 10;
/* ------------------------------------------------
* state
* ------------------------------------------------*/
volatile int button_states[4] = {1, 1, 1, 1};
volatile long button_times[4] = {0, 0, 0, 0};
volatile long buttontime = 0;
volatile boolean timelapse = false;
volatile boolean running = false;
volatile boolean direction = true;
volatile boolean delaying = false;
volatile boolean timed_exposure = false;
volatile int counter = 0;
volatile int microswitch_position = 0;
volatile boolean microswitch_primed = false;
unsigned long timer = 0;
unsigned long frame_start = 0;
unsigned long delay_start = 0;
String timed_exposure_str = "600";
unsigned long timed_exposure_val = 600;
unsigned long timed_open = 100; //ms after start_frame to pause
volatile boolean timed_exposure_open = false;
unsigned long timed_delay = 0;
unsigned long timed_exposure_last = 0;
unsigned long timed_avg = 600;
unsigned long i_avg = 600;
volatile int fwd_speed = FAST_PWM;
volatile int bwd_speed = FAST_PWM;
volatile long timelapse_delay = 42;
volatile boolean open = false;
/* ------------------------------------------------
* serial
* ------------------------------------------------*/
McopySerial mc;
volatile char cmd_char = 'z';
void setup() {
mc.begin(mc.CAMERA_IDENTIFIER);
PinsInit();
ButtonsInit();
timed_open = OpenTiming();
}
void loop() {
cmd_char = mc.loop();
cmd(cmd_char);
timer = millis();
Button(0);
Button(1);
Button(2);
Button(3);
if (timelapse && delaying) {
WatchDelay();
}
if (running) {
if (timed_exposure) {
ReadTimed();
} else {
ReadMicro();
}
}
if (!running && !timelapse && !delaying){
delay(LOOP_DELAY);
}
}
void cmd (char val) {
if (val == mc.CAMERA) {
Camera();
} else if (val == mc.CAMERA_FORWARD) {
CameraDirection(true);
} else if (val == mc.CAMERA_BACKWARD) {
CameraDirection(false);
} else if (val == mc.CAMERA_OPEN) {
CameraOpen();
} else if (val == mc.CAMERA_CLOSE) {
CameraClose();
} else if (val == mc.CAMERA_EXPOSURE) {
CameraExposure();
} else if (val == mc.STATE) {
State();
}
}
//sending "0" will reset to default exposure time
void CameraExposure () {
timed_exposure_str = mc.getString();
timed_exposure_val = timed_exposure_str.toInt();
if (timed_exposure_val < 600) {
timed_exposure_val = 600;
timed_exposure_str = "600";
timed_exposure= false;
timed_open = OpenTiming();
} else {
//timed_delay = timed_exposure_val - BOLEX_C;
timed_exposure = true;
}
mc.confirm(mc.CAMERA_EXPOSURE);
mc.log("Set exposure time to: ");
mc.log(timed_exposure_str);
}
void PinsInit () {
pinMode(PIN_MOTOR_FORWARD, OUTPUT);
pinMode(PIN_MOTOR_BACKWARD, OUTPUT);
pinMode(PIN_MICRO, INPUT_PULLUP);
pinMode(PIN_INDICATOR, OUTPUT);
}
void ButtonsInit () {
for (int i = 0; i < 4; i++) {
pinMode(BUTTONS[i], INPUT_PULLUP);
}
}
void Button (int index) {
int val = digitalRead(BUTTONS[index]);
if (val != button_states[index]) {
if (val == LOW) { // pressed
button_times[index] = millis();
//button_start(index);
} else if (val == HIGH) { // not pressed
buttontime = millis() - button_times[index];
ButtonEnd(index, buttontime);
}
}
button_states[index] = val;
}
/*
* dormant for now
* void button_start (int index) {
if (index == 0) {
}
}*/
void ButtonEnd (int index, long buttontime) {
if (index == 0) {
if (buttontime > 1000) {
if (!timelapse && !running) {
timelapse = true;
Output(2, 75);
Camera();
}
} else {
if (timelapse) {
timelapse = false;
//Output(2, 75);
} else {
Camera();
}
}
} else if (index == 1) { //set delay
if (buttontime < 42) {
timelapse_delay = 42;
Output(1, 500);
} else {
timelapse_delay = buttontime;
Output(2, 250);
}
} else if (index == 2) { // set speed
if (buttontime >= 1000) {
//timed_delay = buttontime - BOLEX_C;
timed_exposure = true;
Output(2, 250);
} else if (buttontime < 1000) {
//timed_delay = 0;
timed_exposure = false;
Output(1, 500);
}
} else if (index == 3) { //set direction
if (buttontime < 1000) {
direction = true;
Output(1, 500);
} else if (buttontime > 1000) {
direction = false;
Output(2, 250);
}
}
buttontime = 0;
}
void Indicator (boolean state) {
if (state) {
digitalWrite(PIN_INDICATOR, HIGH);
} else {
digitalWrite(PIN_INDICATOR, LOW);
}
}
void Output (int number, int len) {
for (int i = 0; i < number; i++) {
Indicator(true);
delay(len);
Indicator(false);
delay(42);
}
}
void Camera () {
frame_start = millis();
if (direction) {
analogWrite(PIN_MOTOR_FORWARD, fwd_speed);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else {
analogWrite(PIN_MOTOR_BACKWARD, bwd_speed);
analogWrite(PIN_MOTOR_FORWARD, 0);
}
running = true;
microswitch_primed = false;
}
long OpenTiming () {
return direction ? (long) (FWD_OPEN * i_avg) : (long) (BWD_OPEN * i_avg);
}
void CameraOpen () {
if (direction) {
analogWrite(PIN_MOTOR_FORWARD, fwd_speed);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else {
analogWrite(PIN_MOTOR_BACKWARD, bwd_speed);
analogWrite(PIN_MOTOR_FORWARD, 0);
}
running = true;
microswitch_primed = false;
delay(OpenTiming());
analogWrite(PIN_MOTOR_FORWARD, 0);
analogWrite(PIN_MOTOR_BACKWARD, 0);
microswitch_position = digitalRead(PIN_MICRO);
if (microswitch_position == LOW) {
microswitch_primed = true;
}
mc.confirm(mc.CAMERA_OPEN);
mc.log("camera_open()");
open = true;
running = false;
}
void CameraClose () {
bool microswitch_open = false;
if (open) {
if (direction) {
analogWrite(PIN_MOTOR_FORWARD, fwd_speed);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else {
analogWrite(PIN_MOTOR_BACKWARD, bwd_speed);
analogWrite(PIN_MOTOR_FORWARD, 0);
}
while (!microswitch_open) {
microswitch_position = digitalRead(PIN_MICRO);
if (microswitch_position == HIGH) {
microswitch_open = true;
}
delay(2);
}
delay(10);
analogWrite(PIN_MOTOR_FORWARD, 0);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else {
microswitch_position = digitalRead(PIN_MICRO);
if (microswitch_position == HIGH) {
mc.log("WARNING: Camera already closed");
}
}
mc.confirm(mc.CAMERA_CLOSE);
mc.log("camera_close()");
open = false;
}
boolean ReadDelay () {
if (timer - frame_start >= timed_open) {
return true;
}
return false;
}
void WatchDelay () {
if (timer - delay_start >= timelapse_delay) {
delaying = false;
Camera();
}
}
void ReadTimed () {
if (!timed_exposure_open) {
if (timer - frame_start > timed_open
&& timer - frame_start < timed_open + timed_delay) {
PauseTimedExposure();
} else if (timer - frame_start > timed_open + timed_delay) {
microswitch_position = digitalRead(PIN_MICRO);
if (microswitch_position == HIGH) {
Stop();
}
delay(2);//smooths out signal
}
}
if (timed_exposure_open && timer - frame_start > timed_open + timed_delay) {
StartTimedExposure();
}
void PauseTimedExposure () {
timed_exposure_open = true;
analogWrite(PIN_MOTOR_FORWARD, 0);
analogWrite(PIN_MOTOR_BACKWARD, 0);
}
void StartTimedExposure () {
timed_exposure_open = false;
if (direction) {
analogWrite(PIN_MOTOR_FORWARD, fwd_speed);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else {
analogWrite(PIN_MOTOR_BACKWARD, bwd_speed);
analogWrite(PIN_MOTOR_FORWARD, 0);
}
}
void ReadMicro () {
if (ReadDelay()) {
microswitch_position = digitalRead(PIN_MICRO);
if (microswitch_position == LOW
&& microswitch_primed == false) {
microswitch_primed = true;
} else if (microswitch_position == HIGH
&& microswitch_primed == true) {
Stop();
}
delay(2);//smooths out signal
}
}
void Stop () {
delay(10);
analogWrite(PIN_MOTOR_FORWARD, 0);
analogWrite(PIN_MOTOR_BACKWARD, 0);
running = false;
microswitch_primed = false;
if (direction) {
counter += 1;
} else {
counter -= 1;
}
timed_exposure_last = timer - frame_start;
if (timed_exposure) {
timed_avg = round((timed_avg + timed_exposure_last) / 2);
} else {
i_avg = round((i_avg + timed_exposure_last) / 2);
}
mc.confirm(mc.CAMERA);
mc.log("Camera completed");
mc.log(String(timed_exposure_last));
if (timelapse) {
delaying = true;
delay_start = millis();
}
}
void CameraDirection (boolean state) {
direction = state;
if (state) {
timed_open = OpenTiming();
mc.confirm(mc.CAMERA_FORWARD);
mc.log("camera_direction(true)");
} else {
timed_open = OpenTiming();
mc.confirm(mc.CAMERA_FORWARD);
mc.log("camera_direction(false)");
}
}
void State () {
String stateString = String(mc.STATE);
stateString += String(mc.CAMERA_EXPOSURE);
if (timed_exposure) {
stateString += String(timed_avg);
} else {
stateString += String(i_avg);
}
stateString += String(mc.STATE);
mc.sendString(stateString);
}