//Written by Brant H. 14 Oct 2023 for Milwaukee Makerspace //Auto Slide Gate //constants const int cmd_in = 12; // current switch from laser cutter const int mtr1_out = 5; // output pin to close motor start relay const int mtr2_out = 6; // output pin to close motor start relay const int lsw1_in = 7; // input pin from limit switch at open position const int lsw2_in = 8; // input pin from limit switch at closed position const int led1_out = 9; // output pin for open position valid const int led2_out = 10; // output pin for closed position valid const int cmd_led_out = 13; int read_cmd_in; int read_lsw1_in; int read_lsw2_in; int last_cmd_state; int last_lsw1_state; int last_lsw2_state; int cmd_state; int lsw1_state; int lsw2_state; int mtr1_state; int mtr2_state; int last_mtr1_state; int last_mtr2_state; int n; void setup() { pinMode(cmd_in, INPUT_PULLUP); pinMode(mtr1_out, OUTPUT); pinMode(mtr2_out, OUTPUT); pinMode(lsw1_in, INPUT_PULLUP); pinMode(lsw2_in, INPUT_PULLUP); pinMode(led1_out, OUTPUT); pinMode(led2_out, OUTPUT); //Serial.begin(9600); } void loop() { read_lsw1_in = digitalRead(lsw1_in); read_lsw2_in = digitalRead(lsw2_in); read_cmd_in = digitalRead(cmd_in); if (read_lsw1_in == 1) { //check left switch open digitalWrite(led1_out, 0);} //no light if (read_lsw1_in == 0) { //check left switch closed digitalWrite(led1_out, 1);} //yes light if (read_lsw2_in == 1) { //check right switch open digitalWrite(led2_out, 0);} //no light if (read_lsw2_in == 0) { //check right switch closed digitalWrite(led2_out, 1);} //yes light if (read_cmd_in == 1) { //check cmd digitalWrite(cmd_led_out, 0);} //no light if (read_cmd_in == 0) { //check cmd digitalWrite(cmd_led_out, 1);} //yes light cmd_state = read_cmd_in; lsw1_state = read_lsw1_in; lsw2_state = read_lsw2_in; if( (cmd_state == 1) && (lsw1_state == 0) && (lsw2_state == 1) && (last_mtr1_state == 1) && (last_mtr2_state == 1) ) { n=1; //laser cutter is off, starts open, has not run motor } else if((cmd_state == 1) && (lsw1_state == 1) && (lsw2_state == 0) && (last_mtr1_state == 1) && (last_mtr2_state == 1) ) { n=2; //laser cutter is off, starts closed, has not run motor } else if((cmd_state == 0) && (lsw1_state == 1) && (lsw2_state == 0)) { n=5; //laser cutter is on, run motor to left, open gate } else if((cmd_state == 0) && (lsw1_state == 1) && (lsw2_state == 1)) { n=6; //laser cutter is on, keep running left once LSW2 is open } else if((cmd_state == 0) && (lsw1_state == 0) && (lsw2_state == 1)) { n=7; //laser cutter is on, gate is open, arrives at LSW1 } else if((cmd_state == 1) && (lsw2_state == 1) && (last_mtr1_state == 0) && (last_mtr2_state == 0) ) { n=8; //laser cutter is off, run motor to right, close gate } else if((cmd_state == 1) && (lsw1_state == 1) && (lsw2_state == 1) && (last_cmd_state == 0) && (last_lsw2_state == 1) ) { n=9; //laser cutter is off, keep running right once LSW1 is open } else if((cmd_state == 1) && (lsw1_state == 1) && (lsw2_state == 0) && (last_cmd_state == 1) && (last_lsw2_state == 0) ) { n=10; //laser cutter is off, gate is closed, arrives at LSW2 } else if((lsw1_state == 0) && (lsw2_state == 0)) { n=11; //if ever both switches are pressed, stop motor } switch (n) { case 1: mtr1_state = 1; mtr2_state = 1; digitalWrite(mtr1_out,1); digitalWrite(mtr2_out,1); break; case 2: mtr1_state = 1; mtr2_state = 1; digitalWrite(mtr1_out,1); digitalWrite(mtr2_out,1); break; case 5: mtr1_state = 1; mtr2_state = 0; digitalWrite(mtr1_out,0); digitalWrite(mtr2_out,1); break; case 6: mtr1_state = 1; mtr2_state = 0; digitalWrite(mtr1_out,0); digitalWrite(mtr2_out,1); break; case 7: mtr1_state = 1; mtr2_state = 1; digitalWrite(mtr1_out,1); digitalWrite(mtr2_out,1); break; case 8: mtr1_state = 0; mtr2_state = 1; digitalWrite(mtr1_out,1); digitalWrite(mtr2_out,0); break; case 9: mtr1_state = 0; mtr2_state = 1; digitalWrite(mtr1_out,1); digitalWrite(mtr2_out,0); break; case 10: mtr1_state = 1; mtr2_state = 1; digitalWrite(mtr1_out,1); digitalWrite(mtr2_out,1); break; case 11: mtr1_state = 1; mtr2_state = 1; digitalWrite(mtr1_out,1); digitalWrite(mtr2_out,1); break; default: mtr1_state = 1; mtr2_state = 1; digitalWrite(mtr1_out,1); digitalWrite(mtr2_out,1); break; } delay(100); last_cmd_state = cmd_state; last_lsw1_state = lsw1_state; last_lsw2_state = lsw2_state; last_mtr1_state = mtr1_state; last_mtr2_state = mtr2_state; //Serial.println(cmd_state); //Serial.println(lsw1_state); //Serial.println(lsw2_state); //Serial.println(mtr1_state); //Serial.println(mtr2_state); //Serial.println(last_cmd_state); //Serial.println(last_lsw1_state); //Serial.println(last_lsw2_state); //Serial.println(last_mtr1_state); //Serial.println(last_mtr2_state); delay(100); }