Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int stopped = 1;
- int flag = 0;
- unsigned long refresh_rate = 1000;
- unsigned long now = 0;
- // Main Lathe Power Relay
- int lathe_relay_pin = 11;
- unsigned long relay_start = 0;
- unsigned long start_delay_seconds = 2;// <--- CHANGE THIS FOR DELAY BEFORE FIRST CUT
- bool first_cut = true;
- // Buttons
- int start_button_pin = 7;
- int stop_button_pin = 6;
- int start_button_val = 0;
- int stop_button_val = 0;
- // Switches
- int stop_switch_pin = 4;
- int pneumatic_switch_pin = 5;
- int stop_switch_val = 0;
- int pneumatic_switch_val = 0;
- // Pneumatic Arm
- int pneumatic_pin = 13;
- unsigned long pneumatic_start = 0;
- unsigned long pneumatic_seconds = 5;// <--- CHANGE THIS FOR PNEUMATIC ARM DURATION
- // Stepper motor
- int stepper_pin = 8;
- int stepper_dir_pin = 9;
- int stepper_ena_pin = 10;
- unsigned long stepper_start = 0;
- unsigned long stepper_reverse_start = 0;
- int stepper_ticks = 0;
- int stepper_ticks_full = 0;
- float stepper_steps = 0.5;// <--- CHANGE THIS FOR STEP SIZE
- int revolution_size = 200;// <--- CHANGE THIS FOR REVOLUTION SIZE
- int stepper_ticks_max = revolution_size/stepper_steps;
- unsigned long seconds_per_rev = 1;// <--- CHANGE THIS FOR REVOLUTION DURATION
- unsigned long step_every = (float)seconds_per_rev/(float)stepper_ticks_max*1000000;
- unsigned long relay_length = 0;
- unsigned long pneumatic_length = 0;
- unsigned long stepper_length = 0;
- void checkValues() {
- start_button_val = digitalRead(start_button_pin);
- stop_button_val = digitalRead(stop_button_pin);
- pneumatic_switch_val = digitalRead(pneumatic_switch_pin);
- stop_switch_val = digitalRead(stop_switch_pin);
- //start_button_val = 0;
- //stop_button_val = 0;
- //pneumatic_switch_val = 0;
- //stop_switch_val = 0;
- }
- void setup() {
- pinMode(start_button_pin, INPUT);
- pinMode(stop_button_pin, INPUT);
- pinMode(pneumatic_switch_pin, INPUT);
- pinMode(pneumatic_pin, OUTPUT);
- pinMode(stepper_pin, OUTPUT);
- pinMode(stepper_dir_pin, OUTPUT);
- pinMode(stepper_ena_pin, OUTPUT);
- pinMode(lathe_relay_pin, OUTPUT);
- Serial.begin(9600);
- }
- void reset_vals() {
- flag = 0;
- pneumatic_start = 0;
- stepper_start = 0;
- stepper_ticks = 0;
- stepper_ticks_full = 0;
- first_cut = true;
- digitalWrite(pneumatic_pin, LOW);
- digitalWrite(stepper_dir_pin, LOW);
- }
- void loop() {
- now = micros(); // get current time
- checkValues(); // check pin values
- // check before just in case start button is being held
- if (stopped) reset_vals(); // if the program stopped reset all global values
- if (start_button_val == 0) { // check if the start button is pressed
- if (stopped == 1) relay_start = now; // log the delay start
- stopped = 0; // stopped = false
- digitalWrite(lathe_relay_pin, HIGH); // start lathe
- }
- if (stop_button_val == 1) { // check if the stop button is pressed
- if (stopped == 0) Serial.println("Stopped");
- stopped = 1; // stopped = true
- }
- if (stop_switch_val == 1 && flag != 4) { // we stopped now it is time to reverse the stepper motor
- digitalWrite(pneumatic_pin, LOW); // stop the pneumatic arm if it is extended
- digitalWrite(stepper_dir_pin, HIGH); // change the stepper motor direction
- digitalWrite(lathe_relay_pin, LOW); // turn off the lathe
- stepper_start = now; // log the start time
- flag = 4; // change the flag
- stepper_reverse_start = now; // log the reverse start time
- }
- if (stopped) { // if we stopped
- reset_vals(); // reset the global values
- digitalWrite(stepper_ena_pin, HIGH); // turn off the stepper motor
- digitalWrite(lathe_relay_pin, LOW); // turn off the lathe
- return;
- } else digitalWrite(stepper_ena_pin, LOW); // if we didn't stop, turn on the stepper motor
- if (flag == 0 && pneumatic_switch_val == 0) { // if the flag is 0 and the arm is back
- if (now < relay_start) relay_length = (4294967295-relay_start)+now; // get the current start delay duration
- else relay_length = now-relay_start;
- if (relay_length >= (float)start_delay_seconds*1000000 || !first_cut) { // if it has been longer than start_delay_seconds or it is not the first cut
- Serial.print("Starting Arm, Time since start ");
- Serial.print(float(relay_length/1000000));
- Serial.println(" Seconds");
- pneumatic_start = now; // change flag to 1 and set the pneumatic start value
- flag = 1;
- first_cut = false;
- }
- }
- if (flag == 1) {
- if (now < pneumatic_start) pneumatic_length = (4294967295-pneumatic_start)+now;
- else pneumatic_length = now - pneumatic_start;
- if (pneumatic_length >= (float)pneumatic_seconds*1000000) {
- Serial.print("Stopping Arm, Took ");
- Serial.print((float)pneumatic_length/1000000);
- Serial.println(" Seconds");
- digitalWrite(pneumatic_pin, LOW);
- flag = 2;
- } else digitalWrite(pneumatic_pin, HIGH);
- }
- if (flag == 2 && pneumatic_switch_val == 0) {
- stepper_start = now;
- stepper_ticks = 0;
- flag = 3;
- }
- if (flag == 3) {
- if (now < stepper_start) stepper_length = (4294967295-stepper_start)+now;
- else stepper_length = now-stepper_start;
- if (stepper_length >= step_every) {
- digitalWrite(stepper_pin, HIGH);
- stepper_ticks++;
- stepper_ticks_full++;
- stepper_start = now;
- } else if (stepper_length > refresh_rate) digitalWrite(stepper_pin, LOW);
- if (stepper_ticks >= stepper_ticks_max) {
- flag = 0;
- Serial.print("Rev Done, Took ");
- Serial.print((float)step_every*(float)stepper_ticks/1000000);
- Serial.println(" Seconds");
- }
- }
- if (flag == 4 && pneumatic_switch_val == 0) {
- if (now < stepper_start) stepper_start = now;
- unsigned long stepper_length = now-stepper_start;
- if ((stepper_length >= step_every) && stepper_ticks_full > 0) {
- digitalWrite(stepper_pin, HIGH);
- stepper_ticks_full--;
- stepper_start = now;
- } else if (stepper_ticks_full >= 0 && stepper_length > refresh_rate) {
- digitalWrite(stepper_pin, LOW);
- }
- if (stepper_ticks_full <= 0) {
- stopped = 1;
- digitalWrite(stepper_dir_pin, LOW);
- digitalWrite(stepper_pin, LOW);
- if (now < stepper_reverse_start) stepper_reverse_start = now;
- unsigned long reverse_length = now - stepper_reverse_start;
- Serial.print("Reversed stepper motor to start position, Took ");
- Serial.print((float)reverse_length/1000000);
- Serial.println(" Seconds");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement