2020-08-10 00:04:26 +00:00
|
|
|
/*
|
|
|
|
* Sketch containing firmware for the JKMM99
|
|
|
|
* A collaboration between MONO NO AWARE and mcopy.
|
|
|
|
* Compatible with JK106 hardware.
|
|
|
|
*
|
|
|
|
* Uses an Arduino Uno compatible board and a
|
|
|
|
* protoshield.
|
|
|
|
* Relay module for the cam :
|
|
|
|
* Solid state relay module for proj :
|
|
|
|
|
|
|
|
Wiring
|
|
|
|
|
|
|
|
CAMERA + CAMERA_DIR
|
|
|
|
Wire directly to corresponding relay pins.
|
|
|
|
Arduino 2 3 5V GND
|
|
|
|
Relay 1 2 VCC GND
|
|
|
|
|
|
|
|
Relays wired to JK Camera Controller 103/104 models
|
|
|
|
|
|
|
|
PINS FOR CAM WIRE
|
|
|
|
|
|
|
|
(# = Connector #)
|
|
|
|
Arduino # Wire In case Relay
|
|
|
|
=========================================
|
|
|
|
PIN3 1 - Yellow = Grey = IN2
|
|
|
|
5V 2 - Red = Purple = VCC
|
|
|
|
PIN2 3 - Green = White = IN1
|
|
|
|
GND 4 - Black = Black = GND
|
|
|
|
|
|
|
|
PROJECTOR + PROJECTOR_DIR
|
|
|
|
|
|
|
|
Wire to corresponding pins
|
|
|
|
Arduino 9 10 5V GND
|
|
|
|
Relay 1 2 VCC GND
|
|
|
|
|
|
|
|
For controling JK Projectors 106 models
|
|
|
|
Solid state relays connect to:
|
|
|
|
2uf run capacitory
|
|
|
|
400 Ohm Resistor (50W)
|
|
|
|
|
|
|
|
|
|
|
|
PINS FOR PROJ WIRE
|
|
|
|
|
|
|
|
#
|
|
|
|
1 -
|
|
|
|
2 -
|
|
|
|
3 -
|
|
|
|
4 -
|
|
|
|
|
|
|
|
Relay 1 corresponds to FWD
|
|
|
|
Relay 2 corresponse to BWD
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
boolean debug_state = false;
|
|
|
|
|
|
|
|
//unsigned long now; //to be compared to stored values every loop
|
|
|
|
|
|
|
|
//CAMERA CONSTANTS
|
2020-08-18 18:21:27 +00:00
|
|
|
const int CAMERA = 2; //IN1
|
|
|
|
const int CAMERA_DIR = 3; //IN2
|
2020-08-10 00:04:26 +00:00
|
|
|
|
|
|
|
const int CAMERA_MOMENT = 240;
|
|
|
|
const int CAMERA_FRAME = 600;
|
|
|
|
|
|
|
|
|
|
|
|
//PROJECTOR CONSTANTS
|
|
|
|
const int PROJECTOR_MICROSWITCH = 8;
|
|
|
|
|
2020-08-18 18:21:27 +00:00
|
|
|
const int PROJECTOR_FWD = 9; //
|
|
|
|
const int PROJECTOR_BWD = 10; //
|
|
|
|
const int PROJECTOR_ON = 11;
|
2020-08-10 00:04:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
const int PROJECTOR_MOMENT = 240;
|
|
|
|
const int PROJECTOR_FRAME = 600;
|
2020-08-18 18:21:27 +00:00
|
|
|
const int PROJECTOR_MICROSWITCH_CLOSED = 0;
|
|
|
|
const int PROJECTOR_MICROSWITCH_OPENED = 1;
|
|
|
|
const int PROJECTOR_HALF_TIME = 450;
|
2020-08-10 00:04:26 +00:00
|
|
|
|
|
|
|
//CAMERA VARIABLES
|
|
|
|
boolean cam_dir = true;
|
|
|
|
volatile long cam_time = 0;
|
|
|
|
|
|
|
|
//PROJECTOR VARIABLES
|
|
|
|
boolean proj_dir = true;
|
|
|
|
boolean proj_running = false;
|
2020-08-18 18:21:27 +00:00
|
|
|
boolean proj_primed = false;
|
2020-08-10 00:04:26 +00:00
|
|
|
volatile int proj_micro_state = 0;
|
|
|
|
volatile long proj_time = 0;
|
|
|
|
|
|
|
|
//CAMERA COMMANDS
|
|
|
|
const char cmd_camera = 'c';
|
|
|
|
const char cmd_cam_forward = 'e';
|
|
|
|
const char cmd_cam_backward = 'f';
|
|
|
|
|
|
|
|
//PROJECTOR COMMANDS
|
|
|
|
const char cmd_projector = 'p';
|
|
|
|
const char cmd_proj_forward = 'g';
|
|
|
|
const char cmd_proj_backward = 'h';
|
|
|
|
|
|
|
|
const char cmd_debug = 'd';
|
|
|
|
const char cmd_connect = 'i';
|
|
|
|
volatile char cmd_char = 'z';
|
|
|
|
const char cmd_mcopy_identifier = 'm';
|
|
|
|
|
|
|
|
const char cmd_proj_cam_identifier = 's';
|
|
|
|
|
|
|
|
const int serialDelay = 5;
|
|
|
|
|
|
|
|
void setup () {
|
|
|
|
Serial.begin(57600);
|
|
|
|
Serial.flush();
|
|
|
|
Serial.setTimeout(serialDelay);
|
|
|
|
|
|
|
|
pins();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop () {
|
|
|
|
//now = millis();
|
|
|
|
if (!proj_running && Serial.available()) {
|
|
|
|
/* read the most recent byte */
|
|
|
|
cmd_char = (char)Serial.read();
|
|
|
|
}
|
|
|
|
if (proj_running) {
|
|
|
|
proj_microswitch();
|
|
|
|
} else if (cmd_char != 'z') {
|
|
|
|
cmd(cmd_char);
|
|
|
|
cmd_char = 'z';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pins () {
|
|
|
|
//CAMERA RELAYS
|
|
|
|
pinMode(CAMERA, OUTPUT);
|
|
|
|
pinMode(CAMERA_DIR, OUTPUT);
|
|
|
|
|
|
|
|
pinMode(PROJECTOR_MICROSWITCH, INPUT_PULLUP);
|
|
|
|
pinMode(PROJECTOR_FWD, OUTPUT);
|
|
|
|
pinMode(PROJECTOR_BWD, OUTPUT);
|
2020-08-18 18:21:27 +00:00
|
|
|
pinMode(PROJECTOR_ON, OUTPUT);
|
2020-08-10 00:04:26 +00:00
|
|
|
|
2020-09-29 03:47:36 +00:00
|
|
|
//SET DEFAULT
|
2020-08-10 00:04:26 +00:00
|
|
|
digitalWrite(CAMERA, HIGH);
|
|
|
|
digitalWrite(CAMERA_DIR, HIGH);
|
|
|
|
|
|
|
|
digitalWrite(PROJECTOR_FWD, LOW);
|
2020-09-29 03:47:36 +00:00
|
|
|
digitalWrite(PROJECTOR_BWD, LOW);
|
2020-08-18 18:21:27 +00:00
|
|
|
digitalWrite(PROJECTOR_ON, LOW);
|
2020-08-10 00:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmd (char val) {
|
|
|
|
if (val == cmd_debug) {
|
|
|
|
debug();
|
|
|
|
} else if (val == cmd_connect) {
|
|
|
|
connect();
|
|
|
|
} else if (val == cmd_mcopy_identifier) {
|
|
|
|
identify();
|
|
|
|
} else if (val == cmd_cam_forward) {
|
|
|
|
cam_direction(true); //explicit
|
|
|
|
} else if (val == cmd_cam_backward) {
|
|
|
|
cam_direction(false);
|
|
|
|
} else if (val == cmd_camera) {
|
|
|
|
cam_start();
|
|
|
|
} else if (val == cmd_proj_forward) {
|
|
|
|
proj_direction(true); //explicit
|
|
|
|
} else if (val == cmd_proj_backward) {
|
|
|
|
proj_direction(false);
|
|
|
|
} else if (val == cmd_projector) {
|
|
|
|
proj_start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void debug () {
|
|
|
|
debug_state = true;
|
|
|
|
Serial.println(cmd_debug);
|
|
|
|
log("debugging enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
void connect () {
|
|
|
|
Serial.println(cmd_connect);
|
|
|
|
log("connect()");
|
|
|
|
}
|
|
|
|
|
|
|
|
void identify () {
|
|
|
|
Serial.println(cmd_proj_cam_identifier);
|
|
|
|
log("identify()");
|
|
|
|
}
|
|
|
|
|
|
|
|
void cam_start () {
|
|
|
|
if (debug_state) {
|
|
|
|
cam_time = millis();
|
|
|
|
}
|
|
|
|
digitalWrite(CAMERA, LOW);
|
|
|
|
delay(CAMERA_MOMENT);
|
|
|
|
digitalWrite(CAMERA, HIGH);
|
|
|
|
delay(CAMERA_FRAME - CAMERA_MOMENT);
|
|
|
|
cam_stop();
|
|
|
|
if (debug_state) {
|
|
|
|
Serial.println(millis() - cam_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cam_stop () {
|
|
|
|
Serial.println(cmd_camera);
|
|
|
|
log("camera()");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cam_direction (boolean state) {
|
|
|
|
cam_dir = state;
|
|
|
|
if (state) {
|
2020-08-18 18:21:27 +00:00
|
|
|
digitalWrite(CAMERA_DIR, HIGH);
|
2020-08-10 00:04:26 +00:00
|
|
|
Serial.println(cmd_cam_forward);
|
|
|
|
log("cam_direction -> true");
|
|
|
|
} else {
|
2020-08-18 18:21:27 +00:00
|
|
|
digitalWrite(CAMERA_DIR, LOW);
|
2020-08-10 00:04:26 +00:00
|
|
|
Serial.println(cmd_cam_backward);
|
|
|
|
log("cam_direction -> false");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void proj_start () {
|
|
|
|
if (debug_state) {
|
|
|
|
proj_time = millis();
|
|
|
|
}
|
2020-08-18 18:21:27 +00:00
|
|
|
digitalWrite(PROJECTOR_ON, HIGH);
|
2020-08-10 00:04:26 +00:00
|
|
|
if (proj_dir) {
|
|
|
|
digitalWrite(PROJECTOR_FWD, HIGH);
|
|
|
|
} else {
|
|
|
|
digitalWrite(PROJECTOR_BWD, HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
proj_running = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void proj_stop () {
|
|
|
|
//stop both directions
|
2020-08-18 18:21:27 +00:00
|
|
|
delay(10);
|
|
|
|
digitalWrite(PROJECTOR_ON, LOW);
|
2020-08-10 00:04:26 +00:00
|
|
|
digitalWrite(PROJECTOR_FWD, LOW);
|
|
|
|
digitalWrite(PROJECTOR_BWD, LOW);
|
|
|
|
|
2020-09-29 18:00:54 +00:00
|
|
|
//evaluate total time
|
|
|
|
|
2020-08-10 00:04:26 +00:00
|
|
|
Serial.println(cmd_projector);
|
|
|
|
log("projector()");
|
|
|
|
proj_running = false;
|
|
|
|
|
|
|
|
if (debug_state) {
|
|
|
|
Serial.println(millis() - proj_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void proj_direction (boolean state) {
|
|
|
|
proj_dir = state;
|
|
|
|
if (state) {
|
2020-08-18 18:21:27 +00:00
|
|
|
Serial.println(cmd_proj_forward);
|
2020-08-10 00:04:26 +00:00
|
|
|
log("proj_direction -> true");
|
|
|
|
} else {
|
2020-08-18 18:21:27 +00:00
|
|
|
Serial.println(cmd_proj_backward);
|
2020-08-10 00:04:26 +00:00
|
|
|
log("proj_direction -> false");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 18:21:27 +00:00
|
|
|
//LOW=0=CLOSED
|
|
|
|
//HIGH=1=OPEN
|
2020-08-10 00:04:26 +00:00
|
|
|
void proj_microswitch () {
|
|
|
|
int val = digitalRead(PROJECTOR_MICROSWITCH);
|
2020-08-18 18:21:27 +00:00
|
|
|
long now = millis();
|
2020-09-29 18:00:54 +00:00
|
|
|
if (!proj_primed // if not primed
|
|
|
|
&& val != proj_micro_state // AND if state changes
|
|
|
|
&& val == PROJECTOR_MICROSWITCH_OPENED // AND state changes to open
|
|
|
|
&& now - proj_time > PROJECTOR_HALF_TIME) {
|
2020-08-10 00:04:26 +00:00
|
|
|
//prime
|
2020-09-29 03:47:36 +00:00
|
|
|
log("proj_primed => true");
|
2020-08-10 00:04:26 +00:00
|
|
|
proj_micro_state = val;
|
2020-08-18 18:21:27 +00:00
|
|
|
proj_primed = true;
|
2020-09-29 18:00:54 +00:00
|
|
|
} else if (proj_primed //if primed
|
|
|
|
&& val != proj_micro_state //AND if state changes
|
|
|
|
&& val == PROJECTOR_MICROSWITCH_CLOSED //AND state changes to open
|
|
|
|
&& now - proj_time > PROJECTOR_HALF_TIME) { //AND total elapsed time is greater than half frame time
|
|
|
|
//stop
|
2020-08-18 18:21:27 +00:00
|
|
|
proj_primed = false;
|
2020-09-29 18:00:54 +00:00
|
|
|
proj_micro_state = val; //unneeded?
|
2020-08-10 00:04:26 +00:00
|
|
|
proj_stop();
|
|
|
|
} else {
|
2020-09-29 19:13:22 +00:00
|
|
|
//delay(2); //some smothing value
|
2020-08-10 00:04:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void log (String msg) {
|
|
|
|
if (debug_state) {
|
|
|
|
Serial.println(msg);
|
|
|
|
}
|
|
|
|
}
|