2015-05-01 13:07:30 +00:00
|
|
|
//Intval for pro trinket arduinos
|
|
|
|
//Using 12vdc motors in place of a stepper
|
2016-03-18 22:05:56 +00:00
|
|
|
//L289N HBridge for motor control
|
2015-05-01 13:07:30 +00:00
|
|
|
//Microswitch for control
|
|
|
|
|
|
|
|
/*
|
|
|
|
----------------------------------------------------
|
|
|
|
Microswitch (use INPUT_PULLUP!!)
|
|
|
|
GND-----\ | \-----PIN
|
|
|
|
----------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2016-05-13 02:04:39 +00:00
|
|
|
const int FAST_PWM = 255;
|
2016-05-13 02:05:25 +00:00
|
|
|
const int SLOW_PWM = 127;
|
2015-05-01 13:07:30 +00:00
|
|
|
|
|
|
|
/* ------------------------------------------------
|
|
|
|
* pins
|
|
|
|
* ------------------------------------------------*/
|
2018-10-18 03:24:38 +00:00
|
|
|
//5V Trinket Pro
|
2016-01-05 16:07:12 +00:00
|
|
|
const int PIN_INDICATOR = 13;
|
|
|
|
const int PIN_MOTOR_FORWARD = 9;
|
|
|
|
const int PIN_MOTOR_BACKWARD = 10;
|
|
|
|
const int PIN_MICRO = 19; //laser cut version
|
|
|
|
const int BUTTON[4] = {3, 6, 5, 4}; //trigger, direction, speed, delay
|
2015-05-01 13:07:30 +00:00
|
|
|
|
|
|
|
/* ------------------------------------------------
|
|
|
|
* loop
|
|
|
|
* ------------------------------------------------*/
|
|
|
|
const int LOOP_DELAY = 10;
|
|
|
|
|
|
|
|
/* ------------------------------------------------
|
|
|
|
* state
|
|
|
|
* ------------------------------------------------*/
|
2016-03-18 22:05:56 +00:00
|
|
|
volatile int button_state[4] = {1, 1, 1, 1};
|
|
|
|
volatile long button_time[4] = {0, 0, 0, 0};
|
|
|
|
volatile long buttontime = 0;
|
|
|
|
|
2016-05-13 02:04:39 +00:00
|
|
|
volatile int fwd_speed = FAST_PWM;
|
|
|
|
volatile int bwd_speed = FAST_PWM;
|
2015-05-01 13:07:30 +00:00
|
|
|
|
2016-01-05 16:07:12 +00:00
|
|
|
volatile boolean sequence = false;
|
2015-05-01 13:07:30 +00:00
|
|
|
volatile boolean running = false;
|
|
|
|
volatile boolean cam_dir = true;
|
2016-05-12 20:10:19 +00:00
|
|
|
volatile boolean delaying = false;
|
2015-05-01 13:07:30 +00:00
|
|
|
|
|
|
|
volatile int micro_position = 0;
|
|
|
|
volatile boolean micro_primed = false;
|
|
|
|
|
|
|
|
unsigned long timer = 0;
|
2016-05-12 20:10:19 +00:00
|
|
|
unsigned long frame_start = 0;
|
|
|
|
unsigned long delay_start = 0;
|
2015-05-01 13:07:30 +00:00
|
|
|
|
|
|
|
volatile int cam_count = 0;
|
|
|
|
volatile int cam_pos = 0;
|
|
|
|
|
2016-01-05 16:07:12 +00:00
|
|
|
volatile long seq_delay = 42;
|
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Runs once at startup, defines pin modes for
|
|
|
|
* buttons and other inputs.
|
|
|
|
* ------------------------------------------------*/
|
2015-05-01 13:07:30 +00:00
|
|
|
void setup () {
|
|
|
|
Pins_init();
|
2016-01-05 16:07:12 +00:00
|
|
|
Buttons_init();
|
2015-05-01 13:07:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Runs constantly, sets a timer at beginning of loop,
|
|
|
|
* checks states of every button and then either compares
|
|
|
|
* the timer to the delay end, watches for the microswitch
|
|
|
|
* to stop the frame or alternately delays for LOOP_DELAY.
|
|
|
|
* ------------------------------------------------*/
|
2015-05-01 13:07:30 +00:00
|
|
|
void loop () {
|
2016-05-13 02:04:39 +00:00
|
|
|
timer = millis();
|
2016-01-05 16:07:12 +00:00
|
|
|
Btn(0);
|
|
|
|
Btn(1);
|
|
|
|
Btn(2);
|
|
|
|
Btn(3);
|
2016-05-13 02:04:39 +00:00
|
|
|
if (sequence && delaying) {
|
|
|
|
Watch_delay();
|
|
|
|
}
|
2015-05-01 13:07:30 +00:00
|
|
|
if (running) {
|
2016-05-13 02:04:39 +00:00
|
|
|
Read_micro();
|
2016-05-13 02:49:09 +00:00
|
|
|
}
|
|
|
|
if (!running && !sequence && !delaying){
|
2015-05-01 13:07:30 +00:00
|
|
|
delay(LOOP_DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Defines pin modes for motor control pins, microswitch
|
|
|
|
* and the indicator (LED) pin.
|
|
|
|
* ------------------------------------------------*/
|
2015-05-01 13:07:30 +00:00
|
|
|
void Pins_init () {
|
|
|
|
pinMode(PIN_MOTOR_FORWARD, OUTPUT);
|
|
|
|
pinMode(PIN_MOTOR_BACKWARD, OUTPUT);
|
|
|
|
pinMode(PIN_MICRO, INPUT_PULLUP);
|
|
|
|
pinMode(PIN_INDICATOR, OUTPUT);
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Starts a frame by updating the frame_start timer,
|
|
|
|
* starts motor in set direction, sets running flag
|
|
|
|
* to true and primes the micoswitch flag.
|
|
|
|
* ------------------------------------------------*/
|
2016-05-12 20:10:19 +00:00
|
|
|
void Frame () {
|
|
|
|
frame_start = millis();
|
2015-05-01 13:07:30 +00:00
|
|
|
if (cam_dir) {
|
2016-03-18 22:05:56 +00:00
|
|
|
analogWrite(PIN_MOTOR_FORWARD, fwd_speed);
|
2015-05-01 13:07:30 +00:00
|
|
|
analogWrite(PIN_MOTOR_BACKWARD, 0);
|
|
|
|
} else {
|
2016-03-18 22:05:56 +00:00
|
|
|
analogWrite(PIN_MOTOR_BACKWARD, bwd_speed);
|
2015-05-01 13:07:30 +00:00
|
|
|
analogWrite(PIN_MOTOR_FORWARD, 0);
|
|
|
|
}
|
|
|
|
running = true;
|
2016-05-12 20:10:19 +00:00
|
|
|
micro_primed = false;
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Determines whether or not the motor has turned
|
|
|
|
* far enough to start reading the micoswitch state.
|
|
|
|
* This prevents the switch from checking too early,
|
|
|
|
* while it is not yet pressed.
|
|
|
|
* ------------------------------------------------*/
|
2016-05-12 20:10:19 +00:00
|
|
|
boolean Read_delay () {
|
2016-05-13 02:04:39 +00:00
|
|
|
if (fwd_speed == FAST_PWM) {
|
2016-05-12 20:10:19 +00:00
|
|
|
if (timer - frame_start >= 300) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-05 16:07:12 +00:00
|
|
|
} else {
|
2016-05-12 20:10:19 +00:00
|
|
|
if (timer - frame_start >= 600) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-05 16:07:12 +00:00
|
|
|
}
|
2016-05-12 20:10:19 +00:00
|
|
|
return false;
|
2015-05-01 13:07:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Reads the state of the microswitch after the
|
|
|
|
* Read_delay() period. If the micoswitch is pressed,
|
|
|
|
* set the micro_primed flag to true. If primed and
|
|
|
|
* microswitch gets opened, Stop() the frame.
|
|
|
|
* ------------------------------------------------*/
|
2015-05-01 13:07:30 +00:00
|
|
|
void Read_micro () {
|
2016-05-12 20:10:19 +00:00
|
|
|
if (Read_delay()) {
|
|
|
|
micro_position = digitalRead(PIN_MICRO);
|
|
|
|
if (micro_position == LOW
|
|
|
|
&& micro_primed == false) {
|
|
|
|
micro_primed = true;
|
|
|
|
} else if (micro_position == HIGH
|
|
|
|
&& micro_primed == true) {
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
delay(2);//smooths out signal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Determines when the delay period between frames
|
|
|
|
* in a sequence is over.
|
|
|
|
* ------------------------------------------------*/
|
2016-05-12 20:10:19 +00:00
|
|
|
void Watch_delay () {
|
|
|
|
if (timer - delay_start >= seq_delay) {
|
|
|
|
delaying = false;
|
|
|
|
Frame();
|
2015-05-01 13:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Stops the motor after a 10ms delay. Increments
|
|
|
|
* a cam counter when complete. Resets running and
|
|
|
|
* micro_primed flags to false, starts delaying if
|
|
|
|
* sequence flag is set to true.
|
|
|
|
* ------------------------------------------------*/
|
2015-05-01 13:07:30 +00:00
|
|
|
void Stop () {
|
2016-01-05 16:07:12 +00:00
|
|
|
delay(10);
|
2015-05-01 13:07:30 +00:00
|
|
|
analogWrite(PIN_MOTOR_FORWARD, 0);
|
|
|
|
analogWrite(PIN_MOTOR_BACKWARD, 0);
|
|
|
|
|
|
|
|
cam_count++;
|
|
|
|
if (cam_dir) {
|
|
|
|
cam_pos++;
|
|
|
|
} else {
|
|
|
|
cam_pos--;
|
|
|
|
}
|
|
|
|
|
|
|
|
running = false;
|
|
|
|
micro_primed = false;
|
2016-01-05 16:07:12 +00:00
|
|
|
|
|
|
|
if (sequence) {
|
2016-05-12 20:10:19 +00:00
|
|
|
delaying = true;
|
|
|
|
delay_start = millis();
|
2016-01-05 16:07:12 +00:00
|
|
|
}
|
2015-05-01 13:07:30 +00:00
|
|
|
}
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Turns on or off the indicator LED.
|
|
|
|
* ------------------------------------------------*/
|
2015-05-01 13:07:30 +00:00
|
|
|
void Indicator (boolean state) {
|
|
|
|
if (state) {
|
|
|
|
digitalWrite(PIN_INDICATOR, HIGH);
|
|
|
|
} else {
|
|
|
|
digitalWrite(PIN_INDICATOR, LOW);
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 16:07:12 +00:00
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Declare the pin mode of all buttons as, input pullup
|
|
|
|
* which enables the internal pullup resistor.
|
|
|
|
* ------------------------------------------------*/
|
2016-01-05 16:07:12 +00:00
|
|
|
void Buttons_init () {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
pinMode(BUTTON[i], INPUT_PULLUP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Reads the state of a specific button and compares
|
|
|
|
* it to a stored value in the button_state array.
|
|
|
|
* If the value is different than what is stored,
|
|
|
|
* check if button is pressed and store new timer value,
|
|
|
|
* if button is released, compare current time to the stored
|
|
|
|
* time value and pass that to the button_end function.
|
|
|
|
* ------------------------------------------------*/
|
2016-01-05 16:07:12 +00:00
|
|
|
void Btn (int index) {
|
|
|
|
int val = digitalRead(BUTTON[index]);
|
|
|
|
if (val != button_state[index]) {
|
|
|
|
if (val == LOW) { // pressed
|
|
|
|
button_time[index] = millis();
|
2016-05-13 02:04:39 +00:00
|
|
|
//button_start(index);
|
2016-01-05 16:07:12 +00:00
|
|
|
} else if (val == HIGH) { // not pressed
|
|
|
|
buttontime = millis() - button_time[index];
|
|
|
|
button_end(index, buttontime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
button_state[index] = val;
|
|
|
|
}
|
|
|
|
|
2016-05-13 02:04:39 +00:00
|
|
|
/*
|
|
|
|
* dormant for now
|
|
|
|
* void button_start (int index) {
|
2016-01-05 16:07:12 +00:00
|
|
|
if (index == 0) {
|
|
|
|
}
|
2016-05-13 02:04:39 +00:00
|
|
|
}*/
|
2016-01-05 16:07:12 +00:00
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* Determines a specific action for each press length
|
|
|
|
* of each button. In most cases a press over 1 second is
|
|
|
|
* distinct from one less than 1 second.
|
|
|
|
* ------------------------------------------------*/
|
2016-01-05 16:07:12 +00:00
|
|
|
void button_end (int index, long buttontime) {
|
|
|
|
if (index == 0) {
|
|
|
|
if (buttontime > 1000) {
|
|
|
|
if (!sequence) {
|
|
|
|
sequence = true;
|
2016-05-13 02:04:39 +00:00
|
|
|
Output(2, 75);
|
|
|
|
Frame();
|
2016-01-05 16:07:12 +00:00
|
|
|
}
|
2016-05-12 01:05:03 +00:00
|
|
|
} else {
|
2016-05-13 02:04:39 +00:00
|
|
|
if (sequence) {
|
|
|
|
sequence = false;
|
|
|
|
//Output(2, 75);
|
|
|
|
} else {
|
|
|
|
Frame();
|
|
|
|
}
|
2016-01-05 16:07:12 +00:00
|
|
|
}
|
|
|
|
} else if (index == 1) { //set direction
|
|
|
|
if (buttontime < 1000) {
|
|
|
|
cam_dir = true;
|
|
|
|
Output(1, 500);
|
|
|
|
} else if (buttontime > 1000) {
|
|
|
|
cam_dir = false;
|
|
|
|
Output(2, 250);
|
|
|
|
}
|
|
|
|
} else if (index == 2) { // set speed
|
|
|
|
if (buttontime <= 1000) {
|
2016-05-13 02:04:39 +00:00
|
|
|
fwd_speed = FAST_PWM;
|
|
|
|
bwd_speed = FAST_PWM;
|
2016-01-05 16:07:12 +00:00
|
|
|
Output(1, 500);
|
|
|
|
} else if (buttontime > 1000) {
|
2016-05-13 02:04:39 +00:00
|
|
|
fwd_speed = SLOW_PWM;
|
|
|
|
bwd_speed = SLOW_PWM;
|
2016-01-05 16:07:12 +00:00
|
|
|
Output(2, 250);
|
|
|
|
}
|
|
|
|
} else if (index == 3) { //set delay
|
|
|
|
if (buttontime < 42) {
|
|
|
|
seq_delay = 42;
|
|
|
|
Output(1, 500);
|
|
|
|
} else {
|
|
|
|
seq_delay = buttontime;
|
|
|
|
Output(2, 250);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buttontime = 0;
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:24:38 +00:00
|
|
|
|
|
|
|
/* ------------------------------------------------
|
|
|
|
* Display a specified number of flashes on the
|
|
|
|
* indicator LED for a specified amount of time.
|
|
|
|
* ------------------------------------------------*/
|
2016-01-05 16:07:12 +00:00
|
|
|
void Output (int number, int len) {
|
|
|
|
for (int i = 0; i < number; i++) {
|
|
|
|
Indicator(true);
|
|
|
|
delay(len);
|
|
|
|
Indicator(false);
|
|
|
|
delay(42);
|
|
|
|
}
|
2016-05-13 02:04:39 +00:00
|
|
|
}
|