Solid state relay version of the mcopy jk firmware

This commit is contained in:
mmcw-dev 2018-01-22 22:05:05 -05:00
parent 68c9a38aa5
commit e3d6144abd
2 changed files with 252 additions and 21 deletions

View File

@ -0,0 +1,225 @@
//MCOPY firmware.
//Projector + Light
//
// LIGHT WIRING
// ARDUINO PIXIE
// GND -----> -
// PIN 3 -----> in
//
// POWER SUPPLY 5V PIXIE
// GND -----> -
// +5VDC -----> +
/*
PROJECTOR WIRING
----------------------------------------------------
Microswitch (use INPUT_PULLUP!!)
GND-----\ | \-----PIN
----------------------------------------------------
*/
//LIGHT HEADERS
#include "SoftwareSerial.h"
#include "Adafruit_Pixie.h"
#define NUMPIXELS 1 // Number of Pixies in the strip
#define PIXIEPIN 6 // Pin number for SoftwareSerial output
SoftwareSerial pixieSerial(-1, PIXIEPIN);
Adafruit_Pixie light = Adafruit_Pixie(NUMPIXELS, &pixieSerial);
//PROJECTOR HEADERS
boolean debug_state = false;
//LIGHT VARIABLES
String color = "000,000,000";
volatile int commaR = 0;
volatile int commaG = 0;
String strR = "000";
String strG = "000";
String strB = "000";
volatile int r = 0;
volatile int g = 0;
volatile int b = 0;
unsigned long light_time;
//PROJECTOR VARIABLES
//const int proj_time = {{proj.time}};
//const int proj_delay = {{proj.delay}};
const int proj_fwd_pin = 8;
const int proj_bwd_pin = 9;
volatile boolean proj_running = false;
const int proj_micro_pin = 4;
volatile int proj_micro_raw;
boolean proj_dir = true;
//APP
unsigned long now; //to be compared to stored values every loop
const char cmd_light = 'l';
const char cmd_projector = 'p';
const char cmd_proj_forward = 'g';
const char cmd_proj_backward = 'h';
const char cmd_mcopy_identifier = 'm';
//for just proj
//const char cmd_proj_identifier = 'j';
//for proj + light
const char cmd_proj_identifier = 'q';
const char cmd_debug = 'd';
const char cmd_connect = 'i';
volatile char cmd_char = 'z';
const int serialDelay = 5;
void setup() {
Serial.begin(57600);
Serial.flush();
Serial.setTimeout(serialDelay);
pixieSerial.begin(115200); // Pixie REQUIRES this baud rate
light.setPixelColor(0, 0, 0, 0);
light.show();
pinMode(proj_micro_pin, INPUT_PULLUP);
pinMode(proj_fwd_pin, OUTPUT);
pinMode(proj_bwd_pin, OUTPUT);
}
void loop() {
if (Serial.available()) {
/* read the most recent byte */
cmd_char = (char)Serial.read();
}
if (cmd_char != 'z') {
cmd(cmd_char);
cmd_char = 'z';
}
now = millis();
if (proj_running) {
proj_reading();
}
//send light signal to pixie every second
if (now - light_time >= 1000) {
light.setPixelColor(0, r, g, b);
light.show();
light_time = now;
}
}
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_projector) {
proj_start();
} else if (val == cmd_proj_forward) {
proj_direction(true);
} else if (val == cmd_proj_backward) {
proj_direction(false);
} else if (val == cmd_light) {
light_set();
}
}
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_identifier);
log("identify()");
}
void light_set () {
while (Serial.available() == 0) {
//Wait for color string
}
color = Serial.readString();
//Serial.println(color);
commaR = color.indexOf(','); //comma trailing R
commaG = color.indexOf(',', commaR + 1);
strR = color.substring(0, commaR);
strG = color.substring(commaR + 1, commaG);
strB = color.substring(commaG + 1);
r = strR.toInt();
g = strG.toInt();
b = strB.toInt();
light.setPixelColor(0, r, g, b);
light.show();
Serial.println(cmd_light);//confirm light change
log(color);
}
void proj_start () {
if (proj_dir) {
digitalWrite(proj_fwd_pin, HIGH);
digitalWrite(proj_bwd_pin, LOW);
} else {
digitalWrite(proj_bwd_pin, HIGH);
digitalWrite(proj_fwd_pin, LOW);
}
proj_running = true;
delay(500); // Let bump pass out of microswitch
//delay(1300); //TEMPORARY DELAY FOR TESTING TIMING
}
void proj_reading () {
proj_micro_raw = digitalRead(proj_micro_pin);
if (proj_micro_raw == 1) {
//do nothing
} else if (proj_micro_raw == 0) {
proj_stop();
}
//delay(1); //needed?
}
void proj_stop () {
digitalWrite(proj_bwd_pin, LOW);
digitalWrite(proj_fwd_pin, LOW);
proj_running = false;
Serial.println(cmd_projector);
log("projector()");
}
void proj_direction (boolean state) {
proj_dir = state;
if (state) {
Serial.println(cmd_proj_forward);
log("proj_direction -> true");
} else {
Serial.println(cmd_proj_backward);
log("proj_direction -> false");
}
//delay(50); //delay after direction change to account for slippage of the belt
}
void log (String msg) {
if (debug_state) {
Serial.println(msg);
}
}

View File

@ -1,6 +1,7 @@
/*
Wiring
HOLD OFF FOR NOW
For "MONITOR" pins with INPUT_PULLUP resistors:
GND-----\ | \-----PIN
No additional resistors/caps needed.
@ -19,7 +20,8 @@ boolean debug_state = false;
//CAMERA CONSTANTS
const int CAMERA = 2;
const int CAMERA_DIR = 3;
const int CAMERA_FWD = 3;
const int CAMERA_BWD = 4;
const int CAMERA_MOMENT = 200;
const int CAMERA_FRAME = 800;
//CAMERA VARIABLES
@ -27,7 +29,8 @@ boolean cam_dir = true;
//PROJECTOR CONSTANTS
const int PROJECTOR = 8;
const int PROJECTOR_DIR = 9;
const int PROJECTOR_FWD = 9;
const int PROJECTOR_BWD = 10;
const int PROJECTOR_MOMENT = 200;
const int PROJECTOR_FRAME = 800;
//PROJECTOR VARIABLES
@ -83,14 +86,20 @@ void pins () {
pinMode(CAMERA, OUTPUT);
pinMode(PROJECTOR, OUTPUT);
pinMode(CAMERA_DIR, OUTPUT);
pinMode(PROJECTOR_DIR, OUTPUT);
pinMode(CAMERA_FWD, OUTPUT);
pinMode(CAMERA_BWD, OUTPUT);
pinMode(PROJECTOR_FWD, OUTPUT);
pinMode(PROJECTOR_BWD, OUTPUT);
//SET LOW
digitalWrite(CAMERA, LOW);
digitalWrite(CAMERA_DIR, LOW);
digitalWrite(PROJECTOR, LOW);
digitalWrite(PROJECTOR_DIR, LOW);
digitalWrite(CAMERA_FWD, HIGH);
digitalWrite(CAMERA_BWD, LOW);
digitalWrite(PROJECTOR_FWD, HIGH);
digitalWrite(PROJECTOR_BWD, LOW);
}
void cmd (char val) {
@ -140,30 +149,18 @@ void setDir (int pin, boolean dir) {
}
void proj_start () {
if (proj_dir == false) {
setDir(PROJECTOR_DIR, false);
}
digitalWrite(PROJECTOR, HIGH);
delay(PROJECTOR_MOMENT);
digitalWrite(PROJECTOR, LOW);
delay(PROJECTOR_FRAME);
if (proj_dir == false) {
setDir(PROJECTOR_DIR, true);
}
proj_stop();
}
void cam_start () {
if (cam_dir == false) {
setDir(CAMERA_DIR, false);
}
digitalWrite(CAMERA, HIGH);
delay(CAMERA_MOMENT);
digitalWrite(CAMERA, LOW);
delay(CAMERA_FRAME);
if (cam_dir == false) {
setDir(CAMERA_DIR, true);
}
cam_stop();
}
@ -179,12 +176,16 @@ void cam_stop () {
void proj_direction (boolean state) {
proj_dir = state;
//UNO can only set 2 relays to high at a time
//setDir(PROJECTOR_DIR, state);
if (state) {
digitalWrite(PROJECTOR_BWD, LOW);
delay(10);
digitalWrite(PROJECTOR_FWD, HIGH);
Serial.println(cmd_proj_forward);
log("proj_direction -> true");
} else {
digitalWrite(PROJECTOR_FWD, LOW);
delay(10);
digitalWrite(PROJECTOR_BWD, HIGH);
Serial.println(cmd_proj_backward);
log("proj_direction -> false");
}
@ -192,11 +193,16 @@ void proj_direction (boolean state) {
void cam_direction (boolean state) {
cam_dir = state;
//setDir(CAMERA_DIR, state);
if (state) {
digitalWrite(CAMERA_BWD, LOW);
delay(10);
digitalWrite(CAMERA_FWD, HIGH);
Serial.println(cmd_cam_forward);
log("cam_direction -> true");
} else {
digitalWrite(CAMERA_FWD, LOW);
delay(10);
digitalWrite(CAMERA_BWD, HIGH);
Serial.println(cmd_cam_backward);
log("cam_direction -> false");
}