There are some case that is skipping

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);
}

so looking briefly at your logic, how could it? It might enter 5 and immediately go to 6, same for 6 to 7, but your logic progression is so simple, it's not possible to do what you're describing, unless I missed some conditional change of state_fam.
Look carefully at the conditions for those progressions, can they be immediately satisfied by some input conditions?

For example, are all your inputs pulled up? Debounced? Are some floating when contact isn't closed? Because mechanical switches and buttons often do many transitions in state when opening/closing.

well I checked it also, it seems that it shouldn't have that problem. I already did it in a simulator where limit switch can be used too. But I honestly think the problem is the quality of limit switch

Physical limit switches can bounce several times on closing. Try changing to checking a limit switch opening, instead.

Yes. But here in the loop() is a delay of 50 milliseconds, which should provide a crude debouncing effect.

I don't think it will fix anything, but instead of scattering

digitalRead(button4Pin) == LOW

about in the code, @shigs555 might rather read all the switches just once at the top of the loop, as in

    bool button4Pressed = digitalRead(button4Pin) == LOW;

which in some other cases will very much matter, and here at least it means only one edit to appy @Paul_KD7HB's suggestion.

I wonder aloud if these local variables are OK in case code:

[quote="shigs555, post:1, topic:1243000"]
`        for(int i = 0; i < stepsPerRevolution; i++) {`
[/quote]

I've been bitten by other local variables in switch/case statements. The solution here, if it is a problem would be to { embrace } the code of each case, or go old school and just declare a local variable meant only to serve locally in for loops. Which BTW, would be better named anything other than a single character especially maybe not 'i'.

I am guessing that the for loop index is already embraced, but would rather know that for sure.

a7

I e just noticed the use of break, surely at the end of each case as normal.

@shigs555 do you know the break statements in your for loops only break out of the loop, the execution continues with the statement directly after the loop.

If you were thinking thise broke out of the case, it could account for behaviour different to what you wanted or expected.

a7

It does not skip these cases, it zooms through them.

state: 4
state: 5
state: 5
state: 5
state: 5
state: 5
state: 6
state: 7
state: 2

Files for WOKWI.COM

sketch.ino
#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);
}

diagram.json

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 14.4, "left": 37.9, "attrs": {} },
    {
      "type": "wokwi-stepper-motor",
      "id": "stepper1",
      "top": -211.21,
      "left": 22.62,
      "attrs": { "size": "8" }
    },
    { "type": "wokwi-a4988", "id": "drv1", "top": -129.6, "left": -33.6, "attrs": {} },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -181.64, "left": 0, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": -172.8, "left": -29.4, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd2", "top": 96, "left": 18.6, "attrs": {} },
    { "type": "wokwi-vcc", "id": "vcc2", "top": 19.96, "left": -9.6, "attrs": {} },
    {
      "type": "wokwi-dip-switch-8",
      "id": "sw1",
      "top": -99.7,
      "left": 87.9,
      "rotate": 90,
      "attrs": {}
    },
    {
      "type": "wokwi-stepper-motor",
      "id": "stepper2",
      "top": -211.21,
      "left": -111.78,
      "attrs": { "size": "8" }
    },
    { "type": "wokwi-a4988", "id": "drv2", "top": -129.6, "left": -168, "attrs": {} },
    { "type": "wokwi-vcc", "id": "vcc3", "top": -181.64, "left": -134.4, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd3", "top": -172.8, "left": -163.8, "attrs": {} }
  ],
  "connections": [
    [ "drv1:2B", "stepper1:A+", "green", [ "h0" ] ],
    [ "drv1:2A", "stepper1:A-", "green", [ "h0" ] ],
    [ "drv1:1A", "stepper1:B-", "green", [ "h0" ] ],
    [ "drv1:1B", "stepper1:B+", "green", [ "h0" ] ],
    [ "gnd1:GND", "drv1:GND.2", "black", [ "h9.6", "v57.52" ] ],
    [ "vcc1:VCC", "drv1:VMOT", "red", [ "v0" ] ],
    [ "gnd2:GND", "nano:GND.1", "black", [ "h153.6", "v-19.2" ] ],
    [ "gnd2:GND", "drv1:GND.1", "black", [ "v-9.6", "h-153.6", "v-86.48" ] ],
    [ "vcc2:VCC", "drv1:VDD", "red", [ "v19.2", "h19.2", "v-96" ] ],
    [ "nano:GND.2", "sw1:8b", "black", [ "v0" ] ],
    [ "nano:GND.2", "sw1:7b", "black", [ "v0" ] ],
    [ "nano:GND.2", "sw1:6b", "black", [ "v0" ] ],
    [ "nano:GND.2", "sw1:5b", "black", [ "v0" ] ],
    [ "nano:10", "sw1:8a", "green", [ "v0" ] ],
    [ "nano:11", "sw1:7a", "green", [ "v0" ] ],
    [ "nano:12", "sw1:6a", "green", [ "v0" ] ],
    [ "nano:13", "sw1:5a", "green", [ "v9.6", "h-19.2", "v-115.2" ] ],
    [ "drv2:2B", "stepper2:A+", "green", [ "h0" ] ],
    [ "drv2:2A", "stepper2:A-", "green", [ "h0" ] ],
    [ "drv2:1A", "stepper2:B-", "green", [ "h0" ] ],
    [ "drv2:1B", "stepper2:B+", "green", [ "h0" ] ],
    [ "gnd3:GND", "drv2:GND.2", "black", [ "h9.6", "v57.52" ] ],
    [ "vcc3:VCC", "drv2:VMOT", "red", [ "v0" ] ],
    [ "nano:2", "drv1:STEP", "orange", [ "v-48", "h-192", "v-38.4" ] ],
    [ "nano:4", "drv1:DIR", "blue", [ "v-38.4", "h-182.4", "v-38.4" ] ],
    [ "nano:6", "drv2:STEP", "orange", [ "v-28.8", "h-288", "v-57.6" ] ],
    [ "nano:8", "drv2:DIR", "blue", [ "v-19.2", "h-278.4", "v-57.6" ] ],
    [ "vcc2:VCC", "drv2:VDD", "red", [ "v19.2", "h-105.6", "v-134.4" ] ],
    [ "gnd2:GND", "drv2:GND.1", "black", [ "v0", "h-124.8", "v-153.6" ] ]
  ],
  "dependencies": {}
}

As identified in post #2. The code is entering those cases, but exit conditions are satisfied immediately.
@shigs555 ??

Yes, I agree. I wanted to show something visual and declarative to bridge language or tl;dr... so I will show the visual with timestamps (1 second for 1, 2, 3, 4 but milliseconds for 5, 6, 7) not able to timestamp on wokwi

1 Like

Well, in post 3 he suspected the limit switches, then wandered off. Problem solved, but couldn't find his way back here to tell us? Who knows.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.