//dip switch is used to replesent welder feedback pins
//green LED is machine enabled
//blue LED is valid arc met
//currently using a delay(pot3) for weld time until pulse and final slope implemented
//timer code below
//if (lastChange + pot2 <= micros()) { lastChange = micros(); state = !state; digitalWrite(steppin, state); state = !state;digitalWrite(steppin, state);i=i+1; }
int pot1pin = A0; //star traverse speed
int pot2pin = A1; //continous bead speed
int pot3pin = A2; //tack weld time
int pot4pin = A3; //transit of welder time
int pot5pin = A4; //pulse delay addition, welder would do this
int pot6pin = A5; //fs delay addition, welder would do this
int steppin = 4; //stepper motion
int dirpin = 3; //stepper direction
int push = 13; //start button
int enable = 12; //machine enable/kill, Dip#1 light green LED
int arc = 11; //pulse lockout, Dip#2 light blue LED
int pulse = 10; //pulse lockout, Dip#3 yellar LED
int fs = 9; //final slope, Dip#4 pink LED
int weld = 8; //welder ID, Red LED
int transit = 7; // switch for activating second radius, Dip#8 dark greek LED
int rad2 = 6; // lit when working on secound radius, dark blue LED
// the below aare called values tht change
int a = 0;
int b = 0;
int c = 0;
int w = 0;
int p = 0;
int dlay1 =8000;
int dlay2 =20000;
int long pot1; //stored value
int long pot2; //stored value
int long pot3; //stored value
int long pot4; //stored value
int long pot5; //stored value
int long pot6; //stored value
int long lastChange; // the time of changing pin state
int long state = HIGH; //clock ID
long i = 0; //rev step counter
void setup()
{
pinMode(steppin, OUTPUT);
pinMode(dirpin, OUTPUT);
pinMode(push, INPUT);
pinMode(enable, INPUT);
pinMode(arc, INPUT);
pinMode(pulse, INPUT);
pinMode(fs, INPUT);
pinMode(weld, OUTPUT);
pinMode(transit,INPUT);
pinMode(rad2, OUTPUT);
lastChange = micros(); // stepper state changed NOW
digitalWrite(steppin, state);
Serial.begin(2000000); // suggest 115200 or 230400 for com speed, 9600 too slow
Serial.println("Press Button to Initiate");
i=0;
}
void loop(){
//this segment is reoccuring checks
digitalWrite(dirpin, HIGH); //set as high until a reverser is implemented
pot1 = map(analogRead(pot1pin), 0, 1023, 2000, 1); // postion traverse speed
pot2 = map(analogRead(pot2pin), 0, 1023, 2000, 1); // bead traverse speed
pot3 = map(analogRead(pot3pin), 0, 1023, 3000, 500); // tack weld time
pot4 = map(analogRead(pot4pin), 0, 1023, 5000, 1000); // arm transition time
pot5 = map(analogRead(pot5pin), 0, 1023, 3000, 1); // pulse delay length
pot6 = map(analogRead(pot5pin), 0, 1023, 3000, 1); // fs length
//weld circuit
if (w == 1){digitalWrite(weld, HIGH);
if (digitalRead(pulse)==HIGH){ // simulating welder responses, "//" out when not sim
if(i==1){delay(pot5);Serial.println("simulated pulse, exaggerate");}
}
if(digitalRead(fs)==HIGH){ // simulating welder responses, "//" out when not sim
if(c ==1 &i == (pot3-1)) {delay(pot5);Serial.println("simulated finalslope, exaggerated");}
if(c ==3 &i == (dlay2-1)) {delay(pot5);Serial.println("simulated finalslope, exaggerated");}
}
}
if (w == 0){digitalWrite(weld, LOW);}
if (digitalRead(weld)==HIGH & digitalRead(arc)==LOW){Serial.print(" weld "); Serial.print(p); Serial.println("failed"); p=11;c=0; w=0; }
if( c == 1) { w=1; //tackweld mode
if (lastChange + pot6 <= micros()) {lastChange = micros(); state = !state; state = !state; i=i+1;}
if(i >= pot3){c=0;w=0;}
}
if( c == 3) { w=1; //revolve weld mode
if (lastChange + pot2 <= micros()) { lastChange = micros(); state = !state; digitalWrite(steppin, state); state = !state;digitalWrite(steppin, state);i=i+1;}
if(i == dlay2){c=0;w=0;}
}
//traverse circuit
if( c == 2) { //5 point 1/5rev
if (lastChange + pot1 <= micros()) { lastChange = micros(); state = !state; digitalWrite(steppin, state); state = !state;digitalWrite(steppin, state);i=i+1;}
if(i == dlay1){c=0;}
}
if( c == 4) { //time to simulate weld arm transit between rings
digitalWrite(dirpin, LOW);
if (lastChange + pot4 <= micros()) { lastChange = micros(); state = !state; digitalWrite(rad2, state); state = !state;digitalWrite(rad2, state);i=i+1;}
if(i >= pot4){c=0;}
}
//start of automation process
//button checks
if(digitalRead(enable) == LOW){a = 0; b = 0; c=0; w=0; i=0; p=0;} //if enable is off resets int A and B
if (digitalRead(push) == HIGH){
if (a == 1){a = 2; Serial.println("reset simulation");}
a=a+1; Serial.println(a);delay(1000); //reads button and induces start delay
if( a == 1 && digitalRead(enable)== HIGH){Serial.println("Process Start");a = 3; delay(3000); p = 1;Serial.print(" p => "); Serial.println(p);}
}//reads enable state and if A is of propper procedure, sets B up for next procedure
//start of automation
if( p == 1){ //tack weld
if (b == 0){
if (i == 0) {c=1;}
if (c == 0){b=b+1; i=0;}
}
if( b == 1){
if (i == 0) {c=2;}
if ( c == 0){p=p+1; i=0; b=0; Serial.print(" p => "); Serial.println(p);}
}}
if( p == 2){ //tack weld
if (b == 0){
if (i == 0) {c=1;}
if (c == 0){b=b+1; i=0;}
}
if( b == 1){
if (i == 0) {c=2;}
if ( c == 0){p=p+1; i=0; b=0; Serial.print(" p => "); Serial.println(p);}
}}
if( p == 3){ //tack weld
if (b == 0){
if (i == 0) {c=1;}
if (c == 0){b=b+1; i=0;}
}
if( b == 1){
if (i == 0) {c=2;}
if ( c == 0){p=p+1; i=0; b=0; Serial.print(" p => "); Serial.println(p);}
}}
if( p == 4){ //tack weld
if (b == 0){
if (i == 0) {c=1;}
if (c == 0){b=b+1; i=0;}
}
if( b == 1){
if (i == 0) {c=2;}
if ( c == 0){p=p+1; i=0; b=0; Serial.print(" p => "); Serial.println(p);}
}}
if( p == 5){ //tack weld
if (b == 0){
if (i == 0) {c=1;}
if (c == 0){b=b+1; i=0;}
}
if( b == 1){
if (i == 0) {c=2;}
if ( c == 0){p=p+1; i=0; b=0; Serial.print(" p => "); Serial.println(p);}
}}
if( p == 6 || p == 8){ //p6 is revolving weld
if(b == 0){
if (i == 0) {c=3;}
if (c == 0){ b=b+1;i=0;}
}
if( b == 1){
if(digitalRead(transit)==LOW){p=p+3;}
if ( c == 0){p=p+1; i=0; b=0; Serial.print(" p => "); Serial.println(p);}
}}
if( p == 7 || p == 9){ //p7 is arm transit
if (i == 0 && b == 0) {c = 4;}
if (c == 0 && b == 0){ b=b+1;i=0;}
if( b == 1){
if ( c == 0){p=p+1; i=0; b=0; Serial.print(" p => "); Serial.println(p);}
}}
if( p == 10) { p=0; a=0; digitalWrite(rad2,LOW);
Serial.print("pot1=");Serial.println(pot1);
Serial.print("pot2=");Serial.println(pot2);
Serial.print("pot3=");Serial.println(pot3);
Serial.print("pot4=");Serial.println(pot4);
Serial.print("pot5=");Serial.println(pot5);
Serial.print("pot6=");Serial.println(pot6);
}//reset
if(a >= 4){p=11;w=0;}
}