Complete the mcopy relay sketch without variable exposure.

This commit is contained in:
Matthew McWilliams 2023-04-11 01:13:44 -04:00
parent daece3b45a
commit 087fd3f763
1 changed files with 54 additions and 99 deletions

View File

@ -1,141 +1,96 @@
/* /*
Control a camera with a single relay that
triggers a shutter release.
Hardware
Arduino Nano
Relay module
LED
480 Ohm resistor
Wiring Wiring
CAMERA + CAMERA_DIR CAMERA
Wire directly to corresponding relay pins. Wire directly to corresponding relay pins.
Arduino 2 3 Arduino 2 5V GND
Relay 1 2 Relay 1 VCC GND
*/ */
boolean debug_state = false; #include "McopySerial.h"
//unsigned long now; //to be compared to stored values every loop
//CAMERA CONSTANTS //CAMERA CONSTANTS
const int CAMERA = 2; const int CAMERA = 2;
const int CAMERA_DIR = 3; const int LED = 8;
const int CAMERA_MOMENT = 240; const int CAMERA_MOMENT = 240;
const int CAMERA_FRAME = 600;
//CAMERA VARIABLES
boolean cam_dir = true;
//CAMERA COMMANDS //VARIABLES
const char cmd_camera = 'c'; volatile int cameraFrame = 1200;
const char cmd_cam_forward = 'e'; volatile char cmdChar = 'z';
const char cmd_cam_backward = 'f'; volatile long now;
const char cmd_debug = 'd'; McopySerial mc;
const char cmd_connect = 'i';
volatile char cmd_char = 'z';
const char cmd_mcopy_identifier = 'm';
const char cmd_cam_identifier = 'k';
const int serialDelay = 5;
void setup () { void setup () {
Serial.begin(57600);
Serial.flush();
//Serial.setTimeout(serialDelay);
pins(); pins();
digitalWrite(LED, HIGH);
mc.begin(mc.CAMERA_IDENTIFIER);
delay(42);
digitalWrite(LED, LOW);
} }
void loop () { void loop () {
//now = millis(); now = millis();
if (Serial.available()) { cmdChar = mc.loop();
/* read the most recent byte */ cmd(cmdChar);
cmd_char = (char)Serial.read();
}
if (cmd_char != 'z') {
cmd(cmd_char);
cmd_char = 'z';
}
} }
void pins () { void pins () {
//RELAYS
pinMode(CAMERA, OUTPUT); pinMode(CAMERA, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(CAMERA_DIR, OUTPUT);
//SET LOW
digitalWrite(CAMERA, HIGH); digitalWrite(CAMERA, HIGH);
digitalWrite(LED, LOW);
digitalWrite(CAMERA_DIR, HIGH);
} }
void cmd (char val) { void cmd (char val) {
if (val == cmd_debug) { if (val == mc.CAMERA_FORWARD) {
debug(); camera_direction(true);
} else if (val == cmd_connect) { } else if (val == mc.CAMERA_BACKWARD) {
connect(); camera_direction(false);
} else if (val == cmd_mcopy_identifier) { } else if (val == mc.CAMERA) {
identify(); camera();
} else if (val == cmd_cam_forward) { } else if (val == mc.STATE) {
cam_direction(true); //explicit state();
} else if (val == cmd_cam_backward) {
cam_direction(false);
} else if (val == cmd_camera) {
cam_start();
} }
} }
void debug () { //null route direction
debug_state = true; void camera_direction (boolean state) {
Serial.println(cmd_debug); if (state) {
log("debugging enabled"); mc.confirm(mc.CAMERA_FORWARD);
} mc.log("camera_direction(true)");
void connect () {
Serial.println(cmd_connect);
log("connect()");
}
void identify () {
Serial.println(cmd_cam_identifier);
log("identify()");
}
void setDir (int pin, boolean dir) {
if (!dir) {
digitalWrite(pin, LOW);
} else { } else {
digitalWrite(pin, HIGH); mc.confirm(mc.CAMERA_BACKWARD);
mc.log("camera_direction(false)");
} }
} }
void cam_start () { void camera () {
digitalWrite(CAMERA, LOW); digitalWrite(CAMERA, LOW);
digitalWrite(LED, HIGH);
delay(CAMERA_MOMENT); delay(CAMERA_MOMENT);
digitalWrite(CAMERA, HIGH); digitalWrite(CAMERA, HIGH);
delay(CAMERA_FRAME - CAMERA_MOMENT); delay(cameraFrame - CAMERA_MOMENT);
cam_stop(); digitalWrite(LED, LOW);
} }
void cam_stop () { void state () {
Serial.println(cmd_camera); String stateString = String(mc.CAMERA_EXPOSURE);
log("camera()"); stateString += String(cameraFrame);
} stateString += String(mc.STATE);
mc.print(stateString);
void cam_direction (boolean state) {
cam_dir = state;
if (state) {
digitalWrite(CAMERA_DIR, LOW);
Serial.println(cmd_cam_forward);
log("cam_direction -> true");
} else {
digitalWrite(CAMERA_DIR, HIGH);
Serial.println(cmd_cam_backward);
log("cam_direction -> false");
}
}
void log (String msg) {
if (debug_state) {
Serial.println(msg);
}
} }