Advertisement
rileydylan581

Automation-Project-Unstable

Aug 13th, 2022 (edited)
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int stopped = 1;
  2. int flag = 0;
  3. unsigned long refresh_rate = 1000;
  4. unsigned long now = 0;
  5.  
  6. // Main Lathe Power Relay
  7. int lathe_relay_pin = 11;
  8. unsigned long relay_start = 0;
  9.  
  10. unsigned long start_delay_seconds = 2;//    <---    CHANGE THIS FOR DELAY BEFORE FIRST CUT
  11. bool first_cut = true;
  12.  
  13.  
  14. // Buttons
  15. int start_button_pin = 7;
  16. int stop_button_pin = 6;
  17.  
  18. int start_button_val = 0;
  19. int stop_button_val = 0;
  20.  
  21. // Switches
  22. int stop_switch_pin = 4;
  23. int pneumatic_switch_pin = 5;
  24.  
  25. int stop_switch_val = 0;
  26. int pneumatic_switch_val = 0;
  27.  
  28.  
  29. // Pneumatic Arm
  30. int pneumatic_pin = 13;
  31.  
  32. unsigned long pneumatic_start = 0;
  33. unsigned long pneumatic_seconds = 5;//      <---    CHANGE THIS FOR PNEUMATIC ARM DURATION
  34.  
  35.  
  36. // Stepper motor
  37. int stepper_pin = 8;
  38. int stepper_dir_pin = 9;
  39. int stepper_ena_pin = 10;
  40.  
  41. unsigned long stepper_start = 0;
  42. unsigned long stepper_reverse_start = 0;
  43.  
  44. int stepper_ticks = 0;
  45. int stepper_ticks_full = 0;
  46.  
  47. float stepper_steps = 0.5;//          <---          CHANGE THIS FOR STEP SIZE
  48. int revolution_size = 200;//          <---          CHANGE THIS FOR REVOLUTION SIZE
  49. int stepper_ticks_max = revolution_size/stepper_steps;
  50.  
  51. unsigned long seconds_per_rev = 1;//  <---          CHANGE THIS FOR REVOLUTION DURATION
  52. unsigned long step_every = (float)seconds_per_rev/(float)stepper_ticks_max*1000000;
  53.  
  54.  
  55. unsigned long relay_length = 0;
  56. unsigned long pneumatic_length = 0;
  57. unsigned long stepper_length = 0;
  58.  
  59.  
  60.  
  61.  
  62.  
  63. void checkValues() {
  64.     start_button_val = digitalRead(start_button_pin);
  65.     stop_button_val = digitalRead(stop_button_pin);
  66.     pneumatic_switch_val = digitalRead(pneumatic_switch_pin);
  67.     stop_switch_val = digitalRead(stop_switch_pin);
  68.     //start_button_val = 0;
  69.     //stop_button_val = 0;
  70.     //pneumatic_switch_val = 0;
  71.     //stop_switch_val = 0;
  72. }
  73. void setup() {
  74.     pinMode(start_button_pin, INPUT);
  75.     pinMode(stop_button_pin, INPUT);
  76.     pinMode(pneumatic_switch_pin, INPUT);
  77.  
  78.     pinMode(pneumatic_pin, OUTPUT);
  79.     pinMode(stepper_pin, OUTPUT);
  80.     pinMode(stepper_dir_pin, OUTPUT);
  81.     pinMode(stepper_ena_pin, OUTPUT);
  82.     pinMode(lathe_relay_pin, OUTPUT);
  83.     Serial.begin(9600);
  84. }
  85.  
  86. void reset_vals() {
  87.     flag = 0;
  88.     pneumatic_start = 0;
  89.     stepper_start = 0;
  90.     stepper_ticks = 0;
  91.     stepper_ticks_full = 0;
  92.     first_cut = true;
  93.     digitalWrite(pneumatic_pin, LOW);
  94.     digitalWrite(stepper_dir_pin, LOW);
  95. }
  96.  
  97. void loop() {
  98.     now = micros(); // get current time
  99.     checkValues(); // check pin values
  100.  
  101.     // check before just in case start button is being held
  102.  
  103.     if (stopped) reset_vals(); // if the program stopped reset all global values
  104.  
  105.     if (start_button_val == 0) { // check if the start button is pressed
  106.         if (stopped == 1) relay_start = now; // log the delay start
  107.         stopped = 0; // stopped = false
  108.         digitalWrite(lathe_relay_pin, HIGH); // start lathe
  109.     }
  110.  
  111.     if (stop_button_val == 1) { // check if the stop button is pressed
  112.         if (stopped == 0) Serial.println("Stopped");
  113.         stopped = 1; // stopped = true
  114.     }
  115.  
  116.     if (stop_switch_val == 1 && flag != 4) { // we stopped now it is time to reverse the stepper motor
  117.         digitalWrite(pneumatic_pin, LOW); // stop the pneumatic arm if it is extended
  118.         digitalWrite(stepper_dir_pin, HIGH); // change the stepper motor direction
  119.         digitalWrite(lathe_relay_pin, LOW); // turn off the lathe
  120.         stepper_start = now; // log the start time
  121.         flag = 4; // change the flag
  122.         stepper_reverse_start = now; // log the reverse start time
  123.     }
  124.  
  125.     if (stopped) { // if we stopped
  126.         reset_vals(); // reset the global values
  127.         digitalWrite(stepper_ena_pin, HIGH); // turn off the stepper motor
  128.         digitalWrite(lathe_relay_pin, LOW); // turn off the lathe
  129.         return;
  130.     } else digitalWrite(stepper_ena_pin, LOW); // if we didn't stop, turn on the stepper motor
  131.  
  132.     if (flag == 0 && pneumatic_switch_val == 0) { // if the flag is 0 and the arm is back
  133.         if (now < relay_start) relay_length = (4294967295-relay_start)+now; // get the current start delay duration
  134.         else relay_length = now-relay_start;
  135.  
  136.         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
  137.             Serial.print("Starting Arm, Time since start ");
  138.             Serial.print(float(relay_length/1000000));
  139.             Serial.println(" Seconds");
  140.             pneumatic_start = now; // change flag to 1 and set the pneumatic start value
  141.             flag = 1;
  142.             first_cut = false;
  143.         }
  144.     }
  145.  
  146.     if (flag == 1) {
  147.         if (now < pneumatic_start) pneumatic_length = (4294967295-pneumatic_start)+now;
  148.         else pneumatic_length = now - pneumatic_start;
  149.  
  150.         if (pneumatic_length >= (float)pneumatic_seconds*1000000) {
  151.             Serial.print("Stopping Arm, Took ");
  152.             Serial.print((float)pneumatic_length/1000000);
  153.             Serial.println(" Seconds");
  154.             digitalWrite(pneumatic_pin, LOW);
  155.             flag = 2;
  156.         } else digitalWrite(pneumatic_pin, HIGH);
  157.     }
  158.  
  159.     if (flag == 2 && pneumatic_switch_val == 0) {
  160.         stepper_start = now;
  161.         stepper_ticks = 0;
  162.         flag = 3;
  163.     }
  164.  
  165.     if (flag == 3) {
  166.         if (now < stepper_start) stepper_length = (4294967295-stepper_start)+now;
  167.         else stepper_length = now-stepper_start;
  168.  
  169.         if (stepper_length >= step_every) {
  170.             digitalWrite(stepper_pin, HIGH);
  171.             stepper_ticks++;
  172.             stepper_ticks_full++;
  173.             stepper_start = now;
  174.         } else if (stepper_length > refresh_rate) digitalWrite(stepper_pin, LOW);
  175.  
  176.         if (stepper_ticks >= stepper_ticks_max) {
  177.             flag = 0;
  178.             Serial.print("Rev Done, Took ");
  179.             Serial.print((float)step_every*(float)stepper_ticks/1000000);
  180.             Serial.println(" Seconds");
  181.         }
  182.     }
  183.     if (flag == 4 && pneumatic_switch_val == 0) {
  184.         if (now < stepper_start) stepper_start = now;
  185.         unsigned long stepper_length = now-stepper_start;
  186.         if ((stepper_length >= step_every) && stepper_ticks_full > 0) {
  187.             digitalWrite(stepper_pin, HIGH);
  188.             stepper_ticks_full--;
  189.             stepper_start = now;
  190.         } else if (stepper_ticks_full >= 0 && stepper_length > refresh_rate) {
  191.             digitalWrite(stepper_pin, LOW);
  192.         }
  193.         if (stepper_ticks_full <= 0) {
  194.             stopped = 1;
  195.             digitalWrite(stepper_dir_pin, LOW);
  196.             digitalWrite(stepper_pin, LOW);
  197.             if (now < stepper_reverse_start) stepper_reverse_start = now;
  198.             unsigned long reverse_length = now - stepper_reverse_start;
  199.             Serial.print("Reversed stepper motor to start position, Took ");
  200.             Serial.print((float)reverse_length/1000000);
  201.             Serial.println(" Seconds");
  202.         }
  203.  
  204.     }
  205. }
  206.  
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement