mcopy/ino/mcopy_cam_relay/mcopy_cam_relay.ino

120 lines
2.2 KiB
Arduino
Raw Normal View History

2023-04-11 04:38:06 +00:00
/*
Control a camera with a single relay that
triggers a shutter release.
Hardware
Arduino Nano
Relay module
LED
480 Ohm resistor
2023-04-11 04:38:06 +00:00
Wiring
CAMERA
2023-04-11 04:38:06 +00:00
Wire directly to corresponding relay pins.
Arduino 2 5V GND
Relay 1 VCC GND
2023-04-11 04:38:06 +00:00
*/
#include "McopySerial.h"
2023-04-11 04:38:06 +00:00
//CAMERA CONSTANTS
const int CAMERA = 2;
2023-04-12 03:43:35 +00:00
const int BUTTON = 7;
const int LED = 8;
2023-04-11 04:38:06 +00:00
const int CAMERA_MOMENT = 240;
//VARIABLES
2023-04-12 19:15:41 +00:00
volatile int cameraFrame = 2000;
volatile char cmdChar = 'z';
volatile long now;
2023-04-11 04:38:06 +00:00
volatile String exposureString;
volatile long exposureTarget = 2000;
McopySerial mc;
2023-04-11 04:38:06 +00:00
void setup () {
pins();
digitalWrite(LED, HIGH);
mc.begin(mc.CAMERA_IDENTIFIER);
delay(42);
digitalWrite(LED, LOW);
2023-04-11 04:38:06 +00:00
}
void loop () {
now = millis();
cmdChar = mc.loop();
cmd(cmdChar);
2023-04-12 03:43:35 +00:00
if (digitalRead(BUTTON) == LOW) {
camera();
}
2023-04-11 04:38:06 +00:00
}
void pins () {
pinMode(CAMERA, OUTPUT);
pinMode(LED, OUTPUT);
2023-04-12 03:43:35 +00:00
pinMode(BUTTON, INPUT_PULLUP);
2023-04-11 04:38:06 +00:00
2023-04-12 19:15:41 +00:00
digitalWrite(CAMERA, LOW);
digitalWrite(LED, LOW);
2023-04-11 04:38:06 +00:00
}
void cmd (char val) {
if (val == mc.CAMERA_FORWARD) {
camera_direction(true);
} else if (val == mc.CAMERA_BACKWARD) {
camera_direction(false);
} else if (val == mc.CAMERA) {
camera();
} else if (val == mc.CAMERA_EXPOSURE) {
exposure();
} else if (val == mc.STATE) {
state();
2023-04-11 04:38:06 +00:00
}
}
void exposure () {
exposureString = mc.getString();
parseExposureString();
cameraFrame = exposureTarget;
mc.confirm(mc.CAMERA_EXPOSURE);
}
void parseExposureString () {
exposureTarget = exposureString.toInt();
}
//null route direction
void camera_direction (boolean state) {
if (state) {
mc.confirm(mc.CAMERA_FORWARD);
mc.log("camera_direction(true)");
2023-04-11 04:38:06 +00:00
} else {
mc.confirm(mc.CAMERA_BACKWARD);
mc.log("camera_direction(false)");
2023-04-11 04:38:06 +00:00
}
}
void camera () {
2023-04-12 19:15:41 +00:00
digitalWrite(CAMERA, HIGH);
digitalWrite(LED, HIGH);
2023-04-11 04:38:06 +00:00
delay(CAMERA_MOMENT);
2023-04-12 19:15:41 +00:00
digitalWrite(CAMERA, LOW);
delay(cameraFrame - CAMERA_MOMENT);
digitalWrite(LED, LOW);
mc.confirm(mc.CAMERA);
2023-04-11 04:38:06 +00:00
}
void state () {
String stateString = String(mc.STATE);
stateString += String(mc.CAMERA_EXPOSURE);
stateString += String(cameraFrame);
stateString += String(mc.STATE);
mc.sendString(stateString);
2023-04-11 04:38:06 +00:00
}