Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int stopped = 1;
- int start_button_pin = 7;
- int stop_button_pin = 6;
- int start_button_val = 0;
- int stop_button_val = 0;
- int stepper_pin = 8;
- int stepper_count = 0;
- int stepper_ticks = 0;
- int stepper_ticks_full = 0;
- int stepper_dir_pin = 9;
- int stepper_ena_pin = 10;
- int stop_switch_pin = 4;
- int stop_switch_val = 0;
- int pneumatic_switch_val = 0;
- int pneumatic_switch_pin = 5;
- int pneumatic_pin = 13;
- int pneumatic_count = 0;
- int flag = 0;
- float pneumatic_seconds = 5;
- float stepper_steps = 0.5;
- int revolution_size = 200;
- int stepper_ticks_max = revolution_size/stepper_steps;
- int step_every = 2;
- 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);
- }
- 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);
- Serial.begin(9600);
- }
- void reset_vals() {
- flag = 0;
- pneumatic_count = 0;
- stepper_count = 0;
- stepper_ticks = 0;
- stepper_ticks_full = 0;
- digitalWrite(pneumatic_pin, LOW);
- digitalWrite(stepper_dir_pin, LOW);
- }
- void loop() {
- checkValues();
- // check before just in case start button is being held
- if (start_button_val == 0) {
- stopped = 0;
- }
- if (stop_button_val == 1) {
- stopped = 1;
- }
- if (stop_switch_val == 1 && flag != 4) {
- digitalWrite(pneumatic_pin, LOW);
- digitalWrite(stepper_dir_pin, HIGH);
- stepper_count = 0;
- flag = 4;
- }
- if (stopped) {
- //Serial.println("Stopped");
- reset_vals();
- digitalWrite(stepper_ena_pin, HIGH);
- return;
- } else {
- digitalWrite(stepper_ena_pin, LOW);
- }
- if (pneumatic_switch_val == 0) {
- Serial.println("Starting arm");
- digitalWrite(pneumatic_pin, HIGH);
- delay(pneumatic_seconds*1000);
- Serial.println("Stopping arm");
- digitalWrite(pneumatic_pin, LOW);
- }
- if (flag == 0 && pneumatic_switch_val == 0) {
- Serial.println("Starting Arm");
- pneumatic_count = 0;
- flag = 1;
- }
- if (flag == 1) {
- pneumatic_count += 1;
- if (pneumatic_count >= pneumatic_seconds*1000) {
- //Serial.println(pneumatic_count);
- Serial.println("Stopping Arm");
- digitalWrite(pneumatic_pin, LOW);
- flag = 2;
- } else {
- digitalWrite(pneumatic_pin, HIGH);
- if (pneumatic_count % 1000 == 0) {
- //Serial.println("Pneumatic count is ");
- //Serial.println(pneumatic_count);
- }
- }
- }
- if (flag == 2 && pneumatic_switch_val == 0) {
- stepper_count = 0;
- stepper_ticks = 0;
- flag = 3;
- }
- if (flag == 3) {
- if (stepper_count == 0 || stepper_count % step_every == 0) {
- digitalWrite(stepper_pin, HIGH);
- stepper_ticks++;
- stepper_ticks_full++;
- } else {
- digitalWrite(stepper_pin, LOW);
- }
- stepper_count++;
- if (stepper_ticks >= stepper_ticks_max) {
- flag = 0;
- Serial.println("Rev Done");
- }
- }
- if (flag == 4 && pneumatic_switch_val == 0) {
- if ((stepper_count == 0 || stepper_count % step_every == 0) && stepper_ticks_full > 0) {
- digitalWrite(stepper_pin, HIGH);
- stepper_ticks_full--;
- //Serial.println(stepper_ticks);
- } else if (stepper_ticks_full >= 0) {
- digitalWrite(stepper_pin, LOW);
- }
- stepper_count++;
- if (stepper_ticks_full <= 0) {
- stopped = 1;
- digitalWrite(stepper_dir_pin, LOW);
- Serial.println("Reversed stepper motor to start position");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement