#include <Stepper.h>
int stepsPerRevolution = 2048;
int motSpeed = 50;
int dt = 500;
int state_fam = 0;
int state_fam_old;
const byte limit1Pin = 10;
const byte limit2Pin = 11;
const byte limit3Pin = 12;
const byte button4Pin = 13;
byte but4State;
int motDir = 1;
Stepper myStepper1(stepsPerRevolution, 2, 4, 3, 5);
Stepper myStepper2(stepsPerRevolution, 6, 8, 7, 9);
void setup() {
Serial.begin(115200);
myStepper1.setSpeed(motSpeed);
myStepper2.setSpeed(motSpeed);
pinMode(limit1Pin, INPUT_PULLUP);
pinMode(limit2Pin, INPUT_PULLUP);
pinMode(limit3Pin, INPUT_PULLUP);
pinMode(button4Pin, INPUT_PULLUP);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
but4State = digitalRead(button4Pin);
myStepper1.setSpeed(motSpeed);
myStepper2.setSpeed(motSpeed);
// HOMING OF THE STEPPER MOTOR FOR THE PRESSER
while (digitalRead(limit3Pin) == HIGH) {
myStepper2.step(motDir * -1);
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
delay(50);
}
}
byte runStepper1 = false;
byte runStepper2 = false;
const int Stop = 5;
void loop() {
Serial.print("state: ");
Serial.println(state_fam);
// Switch case was used in the code because the machine parameters in different functions
switch (state_fam) {
case 0: // Spins tube holder clockwise, turns on green light
runStepper1 = true;
myStepper1.step(motDir * 1);
// Dictates the state of the relay for the LEDs
// Green light on
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
// Changes the case in the code to proceed to the next process
if (digitalRead(limit1Pin) == LOW) {
// Stops the tube holder
runStepper1 = false;
// Changes the case
state_fam = 1;
}
break;
case 1: // The stepper spins clockwise for the presser
runStepper2 = true;
myStepper2.step(motDir * 1);
// Dictates the state of the relay for the LEDs
// Yellow light on
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
// Changes the case in the code to proceed to the next process
if (digitalRead(limit2Pin) == LOW) {
// Stops the presser
runStepper2 = false;
// Changes the case
state_fam = 2;
}
break;
case 2: // The stepper stops for a while to lessen wear aand tear
delay(1000);
// Dictates the state of the relay for the LEDs
// Yellow light on
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
// Changes the case in the code to proceed to the next process
runStepper2 = false;
delay(1000);
state_fam = 3;
break;
case 3: // The stepper spins counter-clockwise for the presser to retract
runStepper2 = true;
myStepper2.step(motDir * -1);
// Dictates the state of the relay for the LEDs
// Yellow light on
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
// Changes the case in the code to proceed to the next process
if (digitalRead(limit3Pin) == LOW) {
runStepper2 = false;
state_fam = 4;
}
break;
case 4: // Spins tube holder clockwise, turns on green light
runStepper1 = true;
myStepper1.step(motDir * 1);
// Dictates the state of the relay for the LEDs
// Green light on
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
// Changes the case in the code to proceed to restart the process
if (digitalRead(limit1Pin) == HIGH) {
runStepper1 = true;
state_fam = 0;
}
break;
case Stop:
break;
}
byte but = digitalRead(button4Pin);
// Pauses the entire process and regardless the limit swtiches is actuated during pause, it ignores it.
// Resumes when pressed again
if (but4State != but) {
but4State = but;
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
delay(20);
if (LOW == but) {
if (Stop == state_fam) {
state_fam = state_fam_old;
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
} else {
state_fam_old = state_fam;
state_fam = Stop;
}
}
}
delay(50);
}