Advertisement
rileydylan581

Automation-Project-Legacy

Aug 13th, 2022
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int stopped = 1;
  2.  
  3. int start_button_pin = 7;
  4. int stop_button_pin = 6;
  5.  
  6. int start_button_val = 0;
  7. int stop_button_val = 0;
  8.  
  9.  
  10. int stepper_pin = 8;
  11. int stepper_count = 0;
  12. int stepper_ticks = 0;
  13. int stepper_ticks_full = 0;
  14.  
  15. int stepper_dir_pin = 9;
  16.  
  17. int stepper_ena_pin = 10;
  18.  
  19.  
  20. int stop_switch_pin = 4;
  21. int stop_switch_val = 0;
  22.  
  23. int pneumatic_switch_val = 0;
  24. int pneumatic_switch_pin = 5;
  25.  
  26.  
  27. int pneumatic_pin = 13;
  28. int pneumatic_count = 0;
  29.  
  30.  
  31. int flag = 0;
  32.  
  33. float pneumatic_seconds = 5;
  34.  
  35.  
  36. float stepper_steps = 0.5;
  37. int revolution_size = 200;
  38. int stepper_ticks_max = revolution_size/stepper_steps;
  39. int step_every = 2;
  40.  
  41.  
  42. void checkValues() {
  43.     start_button_val = digitalRead(start_button_pin);
  44.     stop_button_val = digitalRead(stop_button_pin);
  45.     pneumatic_switch_val = digitalRead(pneumatic_switch_pin);
  46.     stop_switch_val = digitalRead(stop_switch_pin);
  47. }
  48. void setup() {
  49.     pinMode(start_button_pin, INPUT);
  50.     pinMode(stop_button_pin, INPUT);
  51.     pinMode(pneumatic_switch_pin, INPUT);
  52.  
  53.     pinMode(pneumatic_pin, OUTPUT);
  54.     pinMode(stepper_pin, OUTPUT);
  55.     pinMode(stepper_dir_pin, OUTPUT);
  56.     pinMode(stepper_ena_pin, OUTPUT);
  57.     Serial.begin(9600);
  58. }
  59.  
  60. void reset_vals() {
  61.     flag = 0;
  62.     pneumatic_count = 0;
  63.     stepper_count = 0;
  64.     stepper_ticks = 0;
  65.     stepper_ticks_full = 0;
  66.     digitalWrite(pneumatic_pin, LOW);
  67.     digitalWrite(stepper_dir_pin, LOW);
  68. }
  69.  
  70. void loop() {
  71.     checkValues();
  72.  
  73.    
  74.  
  75.     // check before just in case start button is being held
  76.     if (start_button_val == 0) {
  77.         stopped = 0;
  78.  
  79.     }
  80.     if (stop_button_val == 1) {
  81.         stopped = 1;
  82.     }
  83.     if (stop_switch_val == 1 && flag != 4) {
  84.         digitalWrite(pneumatic_pin, LOW);
  85.         digitalWrite(stepper_dir_pin, HIGH);
  86.         stepper_count = 0;
  87.         flag = 4;
  88.     }
  89.     if (stopped) {
  90.         //Serial.println("Stopped");
  91.         reset_vals();
  92.         digitalWrite(stepper_ena_pin, HIGH);
  93.         return;
  94.     } else {
  95.         digitalWrite(stepper_ena_pin, LOW);
  96.     }
  97.  
  98.  
  99.     if (pneumatic_switch_val == 0) {
  100.         Serial.println("Starting arm");
  101.         digitalWrite(pneumatic_pin, HIGH);
  102.         delay(pneumatic_seconds*1000);
  103.         Serial.println("Stopping arm");
  104.         digitalWrite(pneumatic_pin, LOW);
  105.     }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.     if (flag == 0 && pneumatic_switch_val == 0) {
  112.         Serial.println("Starting Arm");
  113.         pneumatic_count = 0;
  114.         flag = 1;
  115.     }
  116.     if (flag == 1) {
  117.         pneumatic_count += 1;
  118.         if (pneumatic_count >= pneumatic_seconds*1000) {
  119.             //Serial.println(pneumatic_count);
  120.             Serial.println("Stopping Arm");
  121.             digitalWrite(pneumatic_pin, LOW);
  122.             flag = 2;
  123.         } else {
  124.             digitalWrite(pneumatic_pin, HIGH);
  125.             if (pneumatic_count % 1000 == 0) {
  126.                 //Serial.println("Pneumatic count is ");
  127.                 //Serial.println(pneumatic_count);
  128.             }
  129.         }
  130.            
  131.     }
  132.     if (flag == 2 && pneumatic_switch_val == 0) {
  133.         stepper_count = 0;
  134.         stepper_ticks = 0;
  135.         flag = 3;
  136.     }
  137.     if (flag == 3) {
  138.         if (stepper_count == 0 || stepper_count % step_every == 0) {
  139.             digitalWrite(stepper_pin, HIGH);
  140.             stepper_ticks++;
  141.             stepper_ticks_full++;
  142.         } else {
  143.             digitalWrite(stepper_pin, LOW);
  144.         }
  145.         stepper_count++;
  146.         if (stepper_ticks >= stepper_ticks_max) {
  147.             flag = 0;
  148.             Serial.println("Rev Done");
  149.         }
  150.     }
  151.     if (flag == 4 && pneumatic_switch_val == 0) {
  152.         if ((stepper_count == 0 || stepper_count % step_every == 0) && stepper_ticks_full > 0) {
  153.             digitalWrite(stepper_pin, HIGH);
  154.             stepper_ticks_full--;
  155.             //Serial.println(stepper_ticks);
  156.         } else if (stepper_ticks_full >= 0) {
  157.             digitalWrite(stepper_pin, LOW);
  158.         }
  159.         stepper_count++;
  160.         if (stepper_ticks_full <= 0) {
  161.             stopped = 1;
  162.             digitalWrite(stepper_dir_pin, LOW);
  163.             Serial.println("Reversed stepper motor to start position");
  164.         }
  165.  
  166.     }
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement