2016-04-19 01:25:18 +00:00
|
|
|
//Test lighting with NeoPixels instead of Pixies to prevent eye damage.
|
2016-04-21 12:33:16 +00:00
|
|
|
|
2016-04-19 01:25:18 +00:00
|
|
|
//LIGHT HEADERS
|
2016-05-25 19:58:49 +00:00
|
|
|
#include "SoftwareSerial.h"
|
|
|
|
#include "Adafruit_Pixie.h"
|
|
|
|
//#include <Adafruit_NeoPixel.h>
|
2016-04-19 01:25:18 +00:00
|
|
|
#define NUMPIXELS 1 // Number of Pixies in the strip
|
2016-05-25 19:58:49 +00:00
|
|
|
#define PIXIEPIN 6 // Pin number for SoftwareSerial output
|
|
|
|
//#define PIXELPIN 3 // Pin number for SoftwareSerial output
|
|
|
|
SoftwareSerial pixieSerial(-1, PIXIEPIN);
|
|
|
|
Adafruit_Pixie light = Adafruit_Pixie(NUMPIXELS, &pixieSerial);
|
|
|
|
//Adafruit_NeoPixel light = Adafruit_NeoPixel(1, PIXELPIN, NEO_GRB + NEO_KHZ800);
|
2016-04-19 01:25:18 +00:00
|
|
|
|
2016-05-09 13:26:10 +00:00
|
|
|
/*
|
|
|
|
----------------------------------------------------
|
|
|
|
Microswitch (use INPUT_PULLUP!!)
|
|
|
|
GND-----\ | \-----PIN
|
|
|
|
----------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2016-04-19 01:25:18 +00:00
|
|
|
//PROJECTOR HEADERS
|
|
|
|
|
2016-04-21 12:33:16 +00:00
|
|
|
boolean debug_state = false;
|
|
|
|
|
|
|
|
//LIGHT VARIABLES
|
2016-04-19 01:25:18 +00:00
|
|
|
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;
|
|
|
|
|
2016-04-22 03:37:37 +00:00
|
|
|
unsigned long light_time;
|
|
|
|
|
2016-04-21 12:33:16 +00:00
|
|
|
//PROJECTOR VARIABLES
|
2016-04-18 16:59:42 +00:00
|
|
|
//const int proj_time = {{proj.time}};
|
|
|
|
//const int proj_delay = {{proj.delay}};
|
|
|
|
|
2016-05-25 19:58:49 +00:00
|
|
|
const int proj_fwd_pin = 8;
|
|
|
|
const int proj_bwd_pin = 9;
|
2016-04-22 03:37:37 +00:00
|
|
|
volatile boolean proj_running = false;
|
2016-05-09 13:26:10 +00:00
|
|
|
const int proj_micro_pin = 4;
|
|
|
|
volatile int proj_micro_raw;
|
2016-04-18 16:59:42 +00:00
|
|
|
boolean proj_dir = true;
|
|
|
|
|
2016-04-22 03:37:37 +00:00
|
|
|
//APP
|
|
|
|
unsigned long now; //to be compared to stored values every loop
|
|
|
|
|
2016-04-19 01:25:18 +00:00
|
|
|
const char cmd_light = 'l';
|
2016-04-18 16:59:42 +00:00
|
|
|
const char cmd_projector = 'p';
|
|
|
|
const char cmd_proj_forward = 'g';
|
|
|
|
const char cmd_proj_backward = 'h';
|
2016-04-19 01:25:18 +00:00
|
|
|
const char cmd_mcopy_identifier = 'm';
|
2017-12-28 23:32:03 +00:00
|
|
|
//for just proj
|
|
|
|
//const char cmd_proj_identifier = 'j';
|
|
|
|
//for proj + light
|
|
|
|
const char cmd_proj_identifier = 'q';
|
2016-04-18 16:59:42 +00:00
|
|
|
|
|
|
|
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);
|
2016-04-22 03:37:37 +00:00
|
|
|
|
2016-05-25 19:58:49 +00:00
|
|
|
pixieSerial.begin(115200); // Pixie REQUIRES this baud rate
|
|
|
|
//light.begin(); //neopixel only
|
2016-04-19 01:25:18 +00:00
|
|
|
light.setPixelColor(0, 0, 0, 0);
|
|
|
|
light.show();
|
2016-04-21 12:33:16 +00:00
|
|
|
|
2016-05-09 13:26:10 +00:00
|
|
|
pinMode(proj_micro_pin, INPUT_PULLUP);
|
2016-04-22 03:37:37 +00:00
|
|
|
pinMode(proj_fwd_pin, OUTPUT);
|
|
|
|
pinMode(proj_bwd_pin, OUTPUT);
|
2016-04-18 16:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
2016-04-22 03:37:37 +00:00
|
|
|
now = millis();
|
|
|
|
if (proj_running) {
|
2016-04-22 18:34:41 +00:00
|
|
|
proj_reading();
|
2016-04-22 03:37:37 +00:00
|
|
|
}
|
|
|
|
//send light signal to pixie every second
|
|
|
|
if (now - light_time >= 1000) {
|
|
|
|
light.setPixelColor(0, r, g, b);
|
|
|
|
light.show();
|
|
|
|
light_time = now;
|
2016-04-21 12:33:16 +00:00
|
|
|
}
|
2016-04-18 16:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmd (char val) {
|
|
|
|
if (val == cmd_debug) {
|
|
|
|
debug();
|
|
|
|
} else if (val == cmd_connect) {
|
|
|
|
connect();
|
2016-04-19 01:25:18 +00:00
|
|
|
} else if (val == cmd_mcopy_identifier) {
|
|
|
|
identify();
|
2016-04-18 16:59:42 +00:00
|
|
|
} else if (val == cmd_projector) {
|
2016-04-22 03:37:37 +00:00
|
|
|
proj_start();
|
2016-04-18 16:59:42 +00:00
|
|
|
} else if (val == cmd_proj_forward) {
|
|
|
|
proj_direction(true);
|
|
|
|
} else if (val == cmd_proj_backward) {
|
|
|
|
proj_direction(false);
|
2016-04-19 01:25:18 +00:00
|
|
|
} else if (val == cmd_light) {
|
2016-04-22 03:37:37 +00:00
|
|
|
light_set();
|
2016-04-18 16:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void debug () {
|
|
|
|
debug_state = true;
|
|
|
|
Serial.println(cmd_debug);
|
|
|
|
log("debugging enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
void connect () {
|
|
|
|
Serial.println(cmd_connect);
|
|
|
|
log("connect()");
|
|
|
|
}
|
|
|
|
|
2016-04-19 01:25:18 +00:00
|
|
|
void identify () {
|
|
|
|
Serial.println(cmd_proj_identifier);
|
|
|
|
log("identify()");
|
|
|
|
}
|
|
|
|
|
2016-04-22 03:37:37 +00:00
|
|
|
void light_set () {
|
2016-04-19 01:25:18 +00:00
|
|
|
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();
|
2016-04-22 03:37:37 +00:00
|
|
|
|
|
|
|
Serial.println(cmd_light);//confirm light change
|
|
|
|
log(color);
|
2016-04-19 01:25:18 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 03:37:37 +00:00
|
|
|
void proj_start () {
|
|
|
|
if (proj_dir) {
|
|
|
|
digitalWrite(proj_fwd_pin, HIGH);
|
|
|
|
digitalWrite(proj_bwd_pin, LOW);
|
2016-04-18 16:59:42 +00:00
|
|
|
} else {
|
2016-04-22 03:37:37 +00:00
|
|
|
digitalWrite(proj_bwd_pin, HIGH);
|
|
|
|
digitalWrite(proj_fwd_pin, LOW);
|
2016-04-18 16:59:42 +00:00
|
|
|
}
|
2016-04-22 03:37:37 +00:00
|
|
|
proj_running = true;
|
2016-05-09 13:26:10 +00:00
|
|
|
delay(500); // Let bump pass out of microswitch
|
2016-04-22 03:37:37 +00:00
|
|
|
|
|
|
|
//delay(1300); //TEMPORARY DELAY FOR TESTING TIMING
|
|
|
|
}
|
|
|
|
|
2016-04-22 18:34:41 +00:00
|
|
|
void proj_reading () {
|
2016-05-09 13:26:10 +00:00
|
|
|
proj_micro_raw = digitalRead(proj_micro_pin);
|
|
|
|
if (proj_micro_raw == 1) {
|
2016-04-22 18:34:41 +00:00
|
|
|
//do nothing
|
2016-05-09 13:26:10 +00:00
|
|
|
} else if (proj_micro_raw == 0) {
|
2016-04-22 18:34:41 +00:00
|
|
|
proj_stop();
|
2016-04-22 03:37:37 +00:00
|
|
|
}
|
|
|
|
//delay(1); //needed?
|
|
|
|
}
|
|
|
|
|
|
|
|
void proj_stop () {
|
|
|
|
digitalWrite(proj_bwd_pin, LOW);
|
2016-04-22 18:34:41 +00:00
|
|
|
digitalWrite(proj_fwd_pin, LOW);
|
|
|
|
|
|
|
|
proj_running = false;
|
2016-04-22 03:37:37 +00:00
|
|
|
|
2016-04-18 16:59:42 +00:00
|
|
|
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");
|
|
|
|
}
|
2016-04-22 03:37:37 +00:00
|
|
|
//delay(50); //delay after direction change to account for slippage of the belt
|
2016-04-18 16:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void log (String msg) {
|
|
|
|
if (debug_state) {
|
|
|
|
Serial.println(msg);
|
|
|
|
}
|
|
|
|
}
|