Update code to use internal pull-ups + afsk

Adds afsk control via SoftModem
Switches microswitch endstop to use INPUT_PULLUP instead of circutry.
This commit is contained in:
Matt 2015-04-28 08:18:31 -04:00
parent 5cf3001b2a
commit f6dd6bf01f
1 changed files with 54 additions and 25 deletions

View File

@ -1,12 +1,14 @@
//Intval for pro trinket arduinos //Intval for pro trinket arduinos
//Using 12vdc motors in place of a stepper //Using 12vdc motors in place of a stepper
//HBridge for motor control
//Microswitch for control //Microswitch for control
#include <SoftModem.h> #include <SoftModem.h>
#include <ctype.h> #include <ctype.h>
/* /*
---------------------------------------------------- ----------------------------------------------------
Microswitch (use INPUT_PULLUP!!)
GND-----\ | \-----PIN
---------------------------------------------------- ----------------------------------------------------
*/ */
@ -16,31 +18,36 @@ SoftModem modem;
/* ------------------------------------------------ /* ------------------------------------------------
* cmd * cmd
* ------------------------------------------------*/ * ------------------------------------------------*/
volatile int c = 0; volatile int cmd_fsk = 0;
volatile char cmd_fsk_char = 'x';
volatile int cmd_input = 0; volatile int cmd_input = 0;
const int CMD_FORWARD = 102; //f const int CMD_FORWARD = 102; //f
const char CMD_FWD_FSK = 'f';
const int CMD_BACKWARD = 98; //b const int CMD_BACKWARD = 98; //b
const char CMD_BKW_FSK = 'b';
const int CMD_BLACK = 110; //n const int CMD_BLACK = 110; //n
/* ------------------------------------------------ /* ------------------------------------------------
* pins * pins
* ------------------------------------------------*/ * ------------------------------------------------*/
const int PIN_INDICATOR = 13; const int PIN_INDICATOR = 13;
const int PIN_MOTOR_FORWARD = 5;
const int PIN_MOTOR_BACKWARD = 6; const int PIN_MOTOR_FORWARD = 10;
const int PIN_MICRO = 2; const int PIN_MOTOR_BACKWARD = 11;
const int PIN_MICRO = 8;
/* ------------------------------------------------ /* ------------------------------------------------
* loop * loop
* ------------------------------------------------*/ * ------------------------------------------------*/
const int LOOP_DELAY = 100; const int LOOP_DELAY = 10;
/* ------------------------------------------------ /* ------------------------------------------------
* speed * speed
* ------------------------------------------------*/ * ------------------------------------------------*/
const int FWD_SPEED = 240; const int FWD_SPEED = 255;
const int BWD_SPEED = 240; const int BWD_SPEED = 255;
volatile boolean running = false; volatile boolean running = false;
volatile boolean cam_dir = true; volatile boolean cam_dir = true;
@ -49,6 +56,7 @@ volatile int micro_position = 0;
volatile boolean micro_primed = false; volatile boolean micro_primed = false;
unsigned long timer = 0; unsigned long timer = 0;
volatile int timer_int = 0;
volatile int cam_count = 0; volatile int cam_count = 0;
volatile int cam_pos = 0; volatile int cam_pos = 0;
@ -56,57 +64,78 @@ volatile int cam_pos = 0;
void setup () { void setup () {
Serial.begin(9600); Serial.begin(9600);
Serial.flush(); Serial.flush();
modem.begin();
Pins_init(); Pins_init();
Serial.println("Welcome to intval2."); Serial.println("Welcome to intval2.");
} }
void loop () { void loop () {
while (modem.available() > 0 && !running) { while (modem.available() > 0 && !running) {
c = modem.read(); cmd_fsk = modem.read();
Serial.println(c); if (isprint(cmd_fsk)){
cmd_fsk_char = (char)cmd_fsk;
}
//Serial.println(cmd_fsk_char);
} }
if (Serial.available() > 0 && !running){ if (Serial.available() > 0 && !running){
cmd_input = Serial.read(); cmd_input = Serial.read();
//Serial.println(cmd_input); //Serial.println(cmd_input);
} }
if (cmd_input == 102) { if ((cmd_input == CMD_FORWARD
|| cmd_fsk_char == CMD_FWD_FSK)
&& !running) {
Frame(true); Frame(true);
} }
if (running) { if ((cmd_input == CMD_BACKWARD
Frame_check(); || cmd_fsk_char == CMD_BKW_FSK)
} else { && !running) {
delay(LOOP_DELAY); Frame(false);
}
if (cmd_input != 0
|| cmd_fsk_char != 'x') {
cmd_input = 0;
cmd_fsk = 0;
cmd_fsk_char = 'x';
}
if (running) {
Read_micro();
} else {
delay(LOOP_DELAY);
} }
cmd_input = 0;
} }
void Pins_init () { void Pins_init () {
pinMode(PIN_MOTOR_FORWARD, OUTPUT); pinMode(PIN_MOTOR_FORWARD, OUTPUT);
pinMode(PIN_MOTOR_BACKWARD, OUTPUT); pinMode(PIN_MOTOR_BACKWARD, OUTPUT);
pinMode(PIN_MICRO, INPUT); pinMode(PIN_MICRO, INPUT_PULLUP);
pinMode(PIN_INDICATOR, OUTPUT); pinMode(PIN_INDICATOR, OUTPUT);
} }
void Frame (boolean dir) { void Frame (boolean dir) {
//Serial.println("Starting Frame()...");
Time_start(); Time_start();
cam_dir = dir; cam_dir = dir;
if (cam_dir) { if (cam_dir) {
// Serial.println("Forward");
analogWrite(PIN_MOTOR_FORWARD, FWD_SPEED); analogWrite(PIN_MOTOR_FORWARD, FWD_SPEED);
analogWrite(PIN_MOTOR_BACKWARD, 0);
} else { } else {
//Serial.println("Backwards");
analogWrite(PIN_MOTOR_BACKWARD, BWD_SPEED); analogWrite(PIN_MOTOR_BACKWARD, BWD_SPEED);
analogWrite(PIN_MOTOR_FORWARD, 0);
} }
Indicator(true); Indicator(true);
running = true; running = true;
micro_primed = false; micro_primed = false;
cmd_input = 0;
} }
void Frame_check () { void Read_micro () {
micro_position = digitalRead(PIN_MICRO); micro_position = digitalRead(PIN_MICRO);
//Serial.println(micro_position); //Serial.println(micro_position);
if (micro_position == LOW && micro_primed == false) { if (micro_position == LOW && micro_primed == false) {
micro_primed = true; micro_primed = true;
Serial.println("Frame micro_primed"); //Serial.println("Frame micro_primed");
} else if (micro_position == HIGH && micro_primed == true) { } else if (micro_position == HIGH && micro_primed == true) {
Stop(); Stop();
} }
@ -117,7 +146,7 @@ void Stop () {
analogWrite(PIN_MOTOR_FORWARD, 0); analogWrite(PIN_MOTOR_FORWARD, 0);
analogWrite(PIN_MOTOR_BACKWARD, 0); analogWrite(PIN_MOTOR_BACKWARD, 0);
Serial.println("Frame ran"); //Serial.println("Frame ran");
Time_end(); Time_end();
cam_count++; cam_count++;
@ -138,9 +167,9 @@ void Time_start () {
void Time_end () { void Time_end () {
timer = millis() - timer; timer = millis() - timer;
int timer_int = int(timer); timer_int = int(timer);
Serial.print(timer_int); //Serial.print(timer_int);
Serial.println("ms"); //Serial.println("ms");
} }
void Indicator (boolean state) { void Indicator (boolean state) {