//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 zap =9; //fake welder
//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 //slidepot1
int pot2pin = A1; //15 //slidepot2
//int pot3pin = A2; //16
//int pot4pin = A3; //17
//int pot5pin = A4; //18
//int pot6pin = A5; //19
// 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 z=0; //default zero
long dlay1 =0; //tack traverse distance
long dlay2 =20000; //steps per rev
int long dlay3 =0; //micro per hz
//int dlay3 = 250; //valid arc time delay, if no arc after this time(microseconds) then shuts down the program
int long pot1; //star traverse speed
int long pot2; //inner radius continous bead speed
int long pot3; //tack weld time
int long pot4; //transit of welder time
int long pot5; //valid arc kill timer
int long pot6; //out radius bead speed
int long pot7; //tack count
int pot8; //hz
int diam1 = 2;//example inch diameters
int diam2 = 3;
int radi1 = diam1 * 100; //radial progression for respective diameter
int radi2 = diam2 * 100;
int long lastChange; // the time of changing pin state
int long state = HIGH; //clock ID
int long micropsec = 1000000; //micros per second
long i = 0; //rev step counter
long j = 0; //stall counter
int k = 0;// steps per pulselock
//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(zap, OUTPUT);
pinMode(weld, OUTPUT);
pinMode(transit,INPUT);
pinMode(rad2, OUTPUT);
lastChange = micros(); // stepper state changed NOW
digitalWrite(steppin, state);
i=0;
j=0;
digitalWrite(enable, powerState);
pot1 =100; // postion traverse speed
//pot2 = map(analogRead(pot2pin), 0, 1023, 1000, 20); // bead traverse speed
pot3 = 5000; // tack weld time
//pot4 = 3000; // arm and start transition time
//pot5 = 10000; // valid arc kill timer
//pot6 = 200; // outer radious speed
pot7 = 7; //# of tack points
pot8 = 12; //hz
dlay3 = micropsec/pot8;
dlay1=((3)*dlay2)/pot7;
// values above are currently just a place holder
//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
Serial.begin(2000000); // suggest 115200 or 230400 for com speed, 9600 too slow //mute if not on Sim
Serial.println("pulsechasing one radius only"); //mute if not on sim
Serial.println("Press Button to Initiate"); //mute if not on sim
}
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
//disused section
//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;
lastChange = micros();Serial.println(lastChange);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;}
// }
//if(c==10){w=1;//pulsed weld
//if(digitalRead(pulse)== HIGH){i=i+1; if(i == 402){c=0;}
// if (e==0){
// if ( lastChange + 200 <= micros()) { lastChange = micros(); state = !state; digitalWrite(steppin, state);k=k+1;}
// if(k == 200){e=1;k=0;c=0;w=0;}} }
// if(digitalRead(pulse)== LOW){digitalWrite(steppin, LOW);e=0;}
//}
if(c==10){if (w==1){ //pulsed conitnous weld
if(digitalRead(pulse)== HIGH){ if(e > 199){w=0; state = LOW;c=0;j=1;} ; //e=(number of pulses+1)/2
if (b==0){
if ( lastChange + 20 <= micros()) { lastChange = micros(); state = !state; digitalWrite(steppin, state);k=k+1;}
if(k == radi1){e=e+1;b=1;k=0;c=0;w=0;}} } //k=(#steps per pulse)2
if(digitalRead(pulse)== LOW){digitalWrite(steppin, LOW);b=0;}
}}
if(a == 10) { //revolve weld mode
if ( lastChange + (dlay3 / 2) <= micros()) { lastChange = micros(); state = !state; digitalWrite(zap, state);}
}
//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) {i=0; b=0;z=z+1;
} }
if(z==pot7){p=p+1;i=0;Serial.println(p);lastChange = micros();Serial.println(lastChange);z=0;}//p+2 skips pulsed weld
}
if( p == 3){w=1; //pulsed weld
if(j==0){
if (i ==0){ a=10;c=10;} //"a" simulates welder pulse
}
if(j==1){
if (c == 0) {a=0; digitalWrite(zap,LOW);//remove "a" when actual hooked to welder
i=0;p=p+1;Serial.println(p);j=0;e=0;}
}
}
if( p == 4) { p=0; a=0; z=0; digitalWrite(rad2,LOW); powerState = !powerState;lastChange = micros();Serial.println(lastChange);}//rest
}