It always looks so simple once it's sorted out.
const int LED = 13;
const int MainAuto = 51;
const int MainManual =50;
const int stepsPerRevolution = 200;
const int IRSensor = 53;
int j = 0;
int i = 0;
#include <Stepper.h>
Stepper FeedStepper(stepsPerRevolution, 2, 3, 4, 5);
Stepper CutStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
pinMode(LED, OUTPUT);
pinMode(MainAuto, INPUT_PULLUP);
pinMode(MainManual, INPUT_PULLUP);
pinMode(IRSensor,INPUT);
digitalWrite(LED, LOW);
FeedStepper.setSpeed(120);
CutStepper.setSpeed(150);
Serial.begin(9600);
}
//Main Program.
void loop() {
if (digitalRead(MainAuto) == LOW){
Auto();
}
else if (digitalRead(MainManual) == LOW){
Manual();
}
else {
digitalWrite(LED,LOW);
j = 0;
i = 0;
}
}
//End Main Program
//Manual Control
void Manual() {
if (j==0) HomeBlade();
const int FeedButton = 49;
const int CutButton = 48;
pinMode(FeedButton, INPUT_PULLUP);
pinMode(CutButton, INPUT_PULLUP);
if (digitalRead(MainManual) == HIGH){
return;
}
if (digitalRead(FeedButton) == LOW){
digitalWrite(LED, HIGH);
FeedStepper.step(10);
}
else {
digitalWrite(LED,LOW);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
}
if (digitalRead(CutButton) == LOW){
digitalWrite(LED, HIGH);
CutStepper.step(200);
delay(500);
digitalWrite(LED,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
}
//End ManualControl
//Run/Auto
void Auto() {
if (j==0) HomeBlade();
int NumberOfParts(int Q);
int PartSize(int L);
const int StockOut = 52;
int L = 4;
int Q = 3;
pinMode(StockOut, INPUT_PULLUP);
L = PartSize(L);
Q = NumberOfParts(Q);
while (i < Q) {
if (digitalRead(StockOut) == HIGH)
i = Q;
if (digitalRead(MainAuto) == HIGH)
i = Q;
FeedStepper.step(L);
delay (50);
CutStepper.step(200);
delay (50);
i++;}
digitalWrite(LED,LOW);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
if (digitalRead(MainAuto) == HIGH){
return;
}
}
//End Auto
//Home Cutter Blade.
void HomeBlade(){
while(digitalRead(IRSensor) == LOW) CutStepper.step(1);
while(digitalRead(IRSensor) == HIGH) CutStepper.step(1);
CutStepper.step(50);
j=1;
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
//End Home Cutter Blade
//Read rotary switch one to get length of part.
int PartSize(int L){
int rs1p1 = 22;
int rs1p2 = 23;
int rs1p3 = 24;
int rs1p4 = 25;
int rs1p5 = 26;
int rs1p6 = 27;
int rs1p7 = 28;
int rs1p8 = 29;
int rs1p9 = 30;
int rs1p10 = 31;
int rs1p11 = 32;
int rs1p12 = 33;
pinMode(rs1p1, INPUT_PULLUP);
pinMode(rs1p2, INPUT_PULLUP);
pinMode(rs1p3, INPUT_PULLUP);
pinMode(rs1p4, INPUT_PULLUP);
pinMode(rs1p5, INPUT_PULLUP);
pinMode(rs1p6, INPUT_PULLUP);
pinMode(rs1p7, INPUT_PULLUP);
pinMode(rs1p8, INPUT_PULLUP);
pinMode(rs1p9, INPUT_PULLUP);
pinMode(rs1p10, INPUT_PULLUP);
pinMode(rs1p11, INPUT_PULLUP);
pinMode(rs1p12, INPUT_PULLUP);
if (digitalRead(rs1p1) == LOW){
return 9;
}
else if (digitalRead(rs1p2) == LOW){
return 11;
}
else if (digitalRead(rs1p3) == LOW){
return 13;
}
else if (digitalRead(rs1p4) == LOW){
return 15;
}
else if (digitalRead(rs1p5) == LOW){
return 17;
}
else if (digitalRead(rs1p6) == LOW){
return 20;
}
else if (digitalRead(rs1p7) == LOW){
return 22;
}
else if (digitalRead(rs1p8) == LOW){
return 24;
}
else if (digitalRead(rs1p9) == LOW){
return 26;
}
else if (digitalRead(rs1p10) == LOW){
return 28;
}
else if (digitalRead(rs1p11) == LOW){
return 30;
}
else if (digitalRead(rs1p12) == LOW){
return 32;
}
//End of read rotory switch one.
}
//Read rotary switch two to get number of parts.
int NumberOfParts(int Q){
int rs2p1 = 34;
int rs2p2 = 35;
int rs2p3 = 36;
int rs2p4 = 37;
int rs2p5 = 38;
int rs2p6 = 39;
int rs2p7 = 40;
int rs2p8 = 41;
int rs2p9 = 42;
int rs2p10 = 43;
int rs2p11 = 44;
int rs2p12 = 45;
pinMode(rs2p1, INPUT_PULLUP);
pinMode(rs2p2, INPUT_PULLUP);
pinMode(rs2p3, INPUT_PULLUP);
pinMode(rs2p4, INPUT_PULLUP);
pinMode(rs2p5, INPUT_PULLUP);
pinMode(rs2p6, INPUT_PULLUP);
pinMode(rs2p7, INPUT_PULLUP);
pinMode(rs2p8, INPUT_PULLUP);
pinMode(rs2p9, INPUT_PULLUP);
pinMode(rs2p10, INPUT_PULLUP);
pinMode(rs2p11, INPUT_PULLUP);
pinMode(rs2p12, INPUT_PULLUP);
if (digitalRead(rs2p1) == LOW){
return 10;
}
else if (digitalRead(rs2p2) == LOW){
return 50;
}
else if (digitalRead(rs2p3) == LOW){
return 100;
}
else if (digitalRead(rs2p4) == LOW){
return 200;
}
else if (digitalRead(rs2p5) == LOW){
return 300;
}
else if (digitalRead(rs2p6) == LOW){
return 400;
}
else if (digitalRead(rs2p7) == LOW){
return 500;
}
else if (digitalRead(rs2p8) == LOW){
return 1000;
}
else if (digitalRead(rs2p9) == LOW){
return 2000;
}
else if (digitalRead(rs2p10) == LOW){
return 3000;
}
else if (digitalRead(rs2p11) == LOW){
return 5000;
}
else if (digitalRead(rs2p12) == LOW){
return 10000;
}
//End of read rotory switch two.
}
A big thank you to wildbill for the help!