mcopy/ino/components/mcopy_projector/mcopy_projector.ino

138 lines
2.8 KiB
Arduino
Raw Permalink Normal View History

2016-04-12 04:07:36 +00:00
boolean debug_state = false;
const int proj_fwd_pin = 9;
const int proj_bwd_pin = 10;
const int proj_micro_pin = 4;
const int proj_time = 1200;
const int proj_delay = 42;
2016-04-12 04:07:36 +00:00
boolean proj_dir = true;
boolean proj_running = false;
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';
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);
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';
}
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();
} else if (val == cmd_mcopy_identifier) {
identify();
2016-04-12 04:07:36 +00:00
} else if (val == cmd_projector) {
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()");
}
void identify () {
Serial.println(cmd_proj_identifier);
log("identify()");
}
void proj_start () {
if (proj_dir) {
analogWrite(proj_fwd_pin, 255);
analogWrite(proj_bwd_pin, 0);
2016-04-12 05:31:07 +00:00
} else {
analogWrite(proj_bwd_pin, 255);
analogWrite(proj_fwd_pin, 0);
2016-04-12 05:31:07 +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 () {
analogWrite(proj_bwd_pin, 0);
analogWrite(proj_fwd_pin, 0);
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");
}
//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);
}
}