Hello, I need help in my code because there's instances that case 5 to 7 is skipping.
do you think its a code problem or wiring? I tried securing the wires the issue still persist
#include <Stepper.h>
int stepsPerRevolution = 200;//match it with the microstep on driver
int motSpeed = 50;
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);
Stepper myStepper2(stepsPerRevolution, 6, 8);
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);
// 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);
delayMicroseconds(10);
}
}
byte runStepper1 = false;
byte runStepper2 = false;
void loop() {
Serial.print("state: ");
Serial.println(state_fam);
switch (state_fam) {
case 0:
digitalWrite(A0, !LOW);
digitalWrite(A1, !HIGH);
digitalWrite(A2, !LOW);
if (digitalRead(button4Pin) == LOW){
state_fam = 1;
}
break;
case 1: // Spins tube holder clockwise, turns on green light
runStepper1 = true;
for(int i = 0; i < stepsPerRevolution; i++) {
myStepper1.step(motDir);
delayMicroseconds(500);
digitalWrite(A0, !LOW);
digitalWrite(A1, !HIGH);
digitalWrite(A2, !LOW);
if(digitalRead(limit1Pin) == LOW)
break;
}
// Dictates the state of the relay for the LEDs
// Green light on
// 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 = 2;
}
break;
case 2: // The stepper spins clockwise for the presser
runStepper2 = true;
for(int i = 0; i < stepsPerRevolution; i++) {
myStepper2.step(motDir);
delayMicroseconds(500);
if(digitalRead(limit2Pin) == LOW)
break;
}
// 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 = 3;
}
break;
case 3: // 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 = 4;
break;
case 4: // The stepper spins counter-clockwise for the presser to retract
runStepper2 = true;
for(int i = 0; i < stepsPerRevolution; i++) {
myStepper2.step(-motDir);
delayMicroseconds(500);
if(digitalRead(limit3Pin) == LOW)
break;
}
// 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 = 5;
}
break;
case 5: // Spins tube holder clockwise, turns on green light
runStepper1 = true;
for(int i = 0; i < stepsPerRevolution; i++) {
myStepper1.step(motDir);
delayMicroseconds(500);
digitalWrite(A0, !LOW);
digitalWrite(A1, !HIGH);
digitalWrite(A2, !LOW);
if(digitalRead(limit1Pin) == LOW)
break;
}
// Dictates the state of the relay for the LEDs
// Green light on
// Changes the case in the code to proceed to restart the process
if (digitalRead(limit1Pin) == HIGH) {
runStepper1 = true;
state_fam = 6;
}
break;
case 6:
runStepper1 = true;
for(int i = 0; i < stepsPerRevolution; i++) {
myStepper1.step(motDir);
delayMicroseconds(500);
if(digitalRead(limit1Pin) == LOW)
break;
}
// Dictates the state of the relay for the LEDs
// Green light on
digitalWrite(A0, !HIGH);
digitalWrite(A1, !LOW);
digitalWrite(A2, !LOW);
if (digitalRead(limit1Pin) == LOW) {
runStepper1 = false;
state_fam = 7;
}
break;
case 7:
digitalWrite(A0, !LOW);
digitalWrite(A1, !HIGH);
digitalWrite(A2, !LOW);
if (digitalRead(button4Pin) == LOW){
state_fam = 2;
}
break;
}
delay(50);
}