//p13 LED is powerstate light
//blue LED dark is transit system ID, not controled
//BAR LED is welder pigtail, light blue wires are sim functional only, LEDs are sim only
//b1 LED is machine trigger signal,needs b4
//b2 LED lite ia gas, pre flow means gas and trigger can be same time,needs b4
//b3 LED is welder enable, needs b4
//b4 LED is welder grnd
//b5 LED final slope collector, poitive in
//b6 LED final slope emmitter //would use relay to b5
//b7 LED pulse lockout collector, poitive in
//b8 LED pulse lockout emmitter //would use relay to b7
//b9 LED valid arc collector, poitive in, needs specific resistance for miller limits, for sim default on, IRL would power up, thus in sim need manually disable with pushbutton
//b10 LED valid arc collector //would use relay to b9
//timer code below
//if (lastChange + pot2 <= micros()) { lastChange = micros(); state = !state; digitalWrite(steppin, state); state = !state;digitalWrite(steppin, state);i=i+1; }
//int RX =0;
//int TX =1;
int push = 2; //start button
int dirpin = 3; //stepper direction
int steppin = 4; //stepper motion
int transit = 5; // switch for activating second radius, Dip#8 dark greek LED
int weld = 6; //LED BAR b1,b2
int arc =7; //valid arc input
//int pulse =8; //pulse lock
//int unused =9;
//int unused =10;
//int unused =11;
int rad2 =12; // dark blue LED lit when working on secound radius
int enable = 13; //board LED, intial acknowledgement light
int pot1pin = A0; //14 //star traverse speed
int pot2pin = A1; //15 //inner radius continous bead speed
int pot3pin = A2; //16 //tack weld time
int pot4pin = A3; //17 //transit of welder time
int pot5pin = A4; //18 //valid arc kill timer
int pot6pin = A5; //19 //out radius bead speed
// the below aare called values that change
int a = 0; //obsolete
int b = 0; // arm int
int c = 0; // weld map
int d = 0; //arc int
//int e = 0; // pulse int
int w = 0; //weld int
int p = 0; //stage map
int dlay1 =8000;
long dlay2 =20000;
//int dlay3 = 250; //valid arc time delay, if no arc after this time(microseconds) then shuts down the program
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
long j = 0; //stall counter
//button specific
int powerState = LOW;
int buttonState = HIGH;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup()
{
pinMode(steppin, OUTPUT);
pinMode(dirpin, OUTPUT);
pinMode(push, INPUT_PULLUP);
pinMode(enable, OUTPUT);
//pinMode(pulse, INPUT_PULLUP);
pinMode(arc, INPUT_PULLUP);
//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 //mute if not on Sim
Serial.println("Press Button to Initiate"); //mute if not on sim
i=0;
j=0;
digitalWrite(enable, powerState);
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 and start transition time
pot5 = map(analogRead(pot5pin), 0, 1023, 10000, 1000); // valid arc kill timer
pot6 = map(analogRead(pot6pin), 0, 1023, 10000, 1); // outer radius speed
// if using the single run values, must reset every adjustment
}
void loop(){
//button debounce
int reading = digitalRead(push);
if (reading != lastButtonState) {lastDebounceTime = millis();}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {buttonState = reading;
if (buttonState == HIGH) {powerState = !powerState;
if (powerState == HIGH){p=p+1;
Serial.println(p); //mute if not sim
}} }}
digitalWrite(enable, powerState);
lastButtonState = reading;
//this segment is reoccuring 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
digitalWrite(dirpin, HIGH); //set as high until a reverser is implemented
//pot1 = map(analogRead(pot1pin), 0, 1023, 2000, 1); // postion traverse speed
//pot1 = 1;
//pot2 = map(analogRead(pot2pin), 0, 1023, 2000, 1); // bead traverse speed
//pot2 = 1;
//pot3 = map(analogRead(pot3pin), 0, 1023, 3000, 500); // tack weld time
//pot3 = 3000;
//pot4 = map(analogRead(pot4pin), 0, 1023, 5000, 1000); // arm and start transition time
//pot4 = 1000;
//pot5 = map(analogRead(pot5pin), 0, 1023, 10000, 1000); // valid arc kill timer
//pot5 = 1000;
//pot6 = map(analogRead(pot6pin), 0, 1023, 10000, 1); // outer radius speed
//pot6 = 1;
//using analog read eats 9600 clock cycles every run through
//initial delay period
if(a ==1){if (lastChange <= micros()) {lastChange = micros(); state = !state; state = !state; i=i+1;}
if(i >= pot4){w=0;a=0;b=0;i=0;p=p+1;
Serial.println(p); //mute if not sim
} }
//weld circuit
if (w == 1){digitalWrite(weld, HIGH);} //welder pin on
if (digitalRead(arc)==HIGH){d=1;} // valid arc no found
if (digitalRead(arc)==LOW){d=0;} //valid arc found
if (w == 0){digitalWrite(weld, LOW);} // welder pin off
if (w==1 && d==1){ //kill timer for program if no valid arc found
if (lastChange <= micros()) {lastChange = micros();j=j+1;}
if (j==pot5){Serial.println("weld fail, reset machine"); p=11;c=0;j=0; w=0;}
} //may want to add a kill for welder as only kills the program
//e=digitalRead(pulse); //not sure why this works but below didnt
//Serial.println(e); serial plot confirmation
//doh, its syntax, (digitalRead(pulse == HIGH)) vs (digitalRead(arc)==HIGH)
//if (digitalRead(pulse) == HIGH){e=1;} //pulse lock not found
//if (digitalRead(pulse) == LOW){e=0;} //pulse found
if( c == 1) { w=1; //tackweld mode
if (d==0 && lastChange <= 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 ( d==0 && (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;}
}
if(c == 5) { w=1; //revolve weld mode
if ( d==0 && (lastChange + pot6 <= 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
digitalRead(p);
//start of automation
if(p==1){a=1;}// defeats stuck button
if( p == 2){ //tack weld
if (b == 0){ //weld phase
if (i == 0) {c=1;}
if (c == 0){b=b+1; i=0;}
}
if( b == 1){ //move phase
if (i == 0) {c=2;}
if ( c == 0){p=p+1; i=0; b=0;
Serial.println(p); //mute if not sim
} }}
if( p == 3){ //tack weld
if (b == 0){ //weld phase
if (i == 0) {c=1;}
if (c == 0){b=b+1; i=0;}
}
if( b == 1){ //move phase
if (i == 0) {c=2;}
if ( c == 0){p=p+1; i=0; b=0;
Serial.println(p); //mute if not sim
} }}
if( p == 4){ //tack weld
if (b == 0){ //weld phase
if (i == 0) {c=1;}
if (c == 0){b=b+1; i=0;}
}
if( b == 1){ //move phase
if (i == 0) {c=2;}
if ( c == 0){p=p+1; i=0; b=0;
Serial.println(p); //mute if not sim
} }}
if( p == 5){ //tack weld
if (b == 0){ //weld phase
if (i == 0) {c=1;}
if (c == 0){b=b+1; i=0;}
}
if( b == 1){ //move phase
if (i == 0) {c=2;}
if ( c == 0){p=p+1; i=0; b=0;
Serial.println(p); //mute if not sim
} }}
if( p == 6){ //tack weld
if (b == 0){ //weld phase
if (i == 0) {c=1;}
if (c == 0){b=b+1; i=0;}
}
if( b == 1){ //move phase
if (i == 0) {c=2;}
if ( c == 0){p=p+1; i=0; b=0;
Serial.println(p); //mute if not sim
} }}
if( p == 7 || p == 9){ //p6 is revolving weld
if(b == 0){ //weld phase
if (p==7 && i == 0) {c=3;}
if (p==9 && i == 0) {c=5;}
if (c == 0){ b=b+1;i=0;}
}
if( b == 1){ //decides if needs to do second radius or end
if(digitalRead(transit)==LOW){p=p+3;b=0;}
if ( c == 0){p=p+1; i=0; b=0;
Serial.println(p); //mute if not sim
} }}
if( p == 8 || p == 10){ //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.println(p); //mute if not sim
} }}
if( p == 11) { p=0; a=0; digitalWrite(rad2,LOW); powerState = !powerState;}//rest
}