2016-04-12 04:07:36 +00:00
|
|
|
boolean debug_state = false;
|
|
|
|
|
2022-06-18 03:39:19 +00:00
|
|
|
const int proj_fwd_pin = 9;
|
|
|
|
const int proj_bwd_pin = 10;
|
|
|
|
const int proj_micro_pin = 4;
|
2019-02-28 22:50:39 +00:00
|
|
|
const int proj_time = 1200;
|
|
|
|
const int proj_delay = 42;
|
2016-04-12 04:07:36 +00:00
|
|
|
|
|
|
|
boolean proj_dir = true;
|
2019-02-28 22:50:39 +00:00
|
|
|
boolean proj_running = false;
|
2022-06-18 03:39:19 +00:00
|
|
|
volatile int proj_micro_raw;
|
2016-04-12 04:07:36 +00:00
|
|
|
|
|
|
|
const char cmd_projector = 'p';
|
|
|
|
const char cmd_proj_forward = 'g';
|
|
|
|
const char cmd_proj_backward = 'h';
|
|
|
|
|
2016-04-18 16:39:45 +00:00
|
|
|
const char cmd_debug = 'd';
|
|
|
|
const char cmd_connect = 'i';
|
2016-04-12 04:07:36 +00:00
|
|
|
volatile char cmd_char = 'z';
|
2017-12-28 23:32:03 +00:00
|
|
|
const char cmd_mcopy_identifier = 'm';
|
|
|
|
const char cmd_proj_identifier = 'j';
|
|
|
|
|
2016-04-12 04:07:36 +00:00
|
|
|
const int serialDelay = 5;
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(57600);
|
|
|
|
Serial.flush();
|
|
|
|
Serial.setTimeout(serialDelay);
|
2022-06-18 03:39:19 +00:00
|
|
|
pins_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void pins_init () {
|
|
|
|
pinMode(proj_fwd_pin, OUTPUT);
|
|
|
|
pinMode(proj_bwd_pin, OUTPUT);
|
|
|
|
pinMode(proj_micro_pin, INPUT_PULLUP);
|
|
|
|
|
|
|
|
analogWrite(proj_fwd_pin, 0);
|
|
|
|
analogWrite(proj_bwd_pin, 0);
|
2016-04-12 04:07:36 +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';
|
|
|
|
}
|
2022-06-18 03:39:19 +00:00
|
|
|
if (proj_running) {
|
|
|
|
proj_reading();
|
|
|
|
}
|
2016-04-12 04:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmd (char val) {
|
|
|
|
if (val == cmd_debug) {
|
|
|
|
debug();
|
|
|
|
} else if (val == cmd_connect) {
|
|
|
|
connect();
|
2018-01-05 20:48:50 +00:00
|
|
|
} else if (val == cmd_mcopy_identifier) {
|
|
|
|
identify();
|
2016-04-12 04:07:36 +00:00
|
|
|
} else if (val == cmd_projector) {
|
2017-12-29 01:54:54 +00:00
|
|
|
proj_start();
|
2016-04-12 04:07:36 +00:00
|
|
|
} else if (val == cmd_proj_forward) {
|
|
|
|
proj_direction(true);
|
|
|
|
} else if (val == cmd_proj_backward) {
|
|
|
|
proj_direction(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void debug () {
|
|
|
|
debug_state = true;
|
2016-04-18 16:39:45 +00:00
|
|
|
Serial.println(cmd_debug);
|
2016-04-12 04:07:36 +00:00
|
|
|
log("debugging enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
void connect () {
|
2016-04-18 16:39:45 +00:00
|
|
|
Serial.println(cmd_connect);
|
2016-04-12 04:07:36 +00:00
|
|
|
log("connect()");
|
|
|
|
}
|
|
|
|
|
2018-01-05 20:48:50 +00:00
|
|
|
void identify () {
|
|
|
|
Serial.println(cmd_proj_identifier);
|
|
|
|
log("identify()");
|
|
|
|
}
|
|
|
|
|
2017-12-29 01:54:54 +00:00
|
|
|
void proj_start () {
|
|
|
|
if (proj_dir) {
|
2022-06-18 03:39:19 +00:00
|
|
|
analogWrite(proj_fwd_pin, 255);
|
|
|
|
analogWrite(proj_bwd_pin, 0);
|
2016-04-12 05:31:07 +00:00
|
|
|
} else {
|
2022-06-18 03:39:19 +00:00
|
|
|
analogWrite(proj_bwd_pin, 255);
|
|
|
|
analogWrite(proj_fwd_pin, 0);
|
2016-04-12 05:31:07 +00:00
|
|
|
}
|
2017-12-29 01:54:54 +00:00
|
|
|
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 () {
|
2022-06-18 03:39:19 +00:00
|
|
|
analogWrite(proj_bwd_pin, 0);
|
|
|
|
analogWrite(proj_fwd_pin, 0);
|
2017-12-29 01:54:54 +00:00
|
|
|
|
|
|
|
proj_running = false;
|
|
|
|
|
2016-04-18 16:39:45 +00:00
|
|
|
Serial.println(cmd_projector);
|
2016-04-12 04:07:36 +00:00
|
|
|
log("projector()");
|
|
|
|
}
|
|
|
|
|
|
|
|
void proj_direction (boolean state) {
|
|
|
|
proj_dir = state;
|
2016-04-18 16:39:45 +00:00
|
|
|
if (state) {
|
|
|
|
Serial.println(cmd_proj_forward);
|
|
|
|
log("proj_direction -> true");
|
|
|
|
} else {
|
|
|
|
Serial.println(cmd_proj_backward);
|
|
|
|
log("proj_direction -> false");
|
|
|
|
}
|
2017-12-29 01:54:54 +00:00
|
|
|
//delay(50); //delay after direction change to account for slippage of the belt
|
2016-04-12 04:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void log (String msg) {
|
|
|
|
if (debug_state) {
|
|
|
|
Serial.println(msg);
|
|
|
|
}
|
|
|
|
}
|