Stepper motors cannot switch direction when prompted to do so

Title covered main issue pretty much. Basically I have a separate esp-32 cam that detects events and sends a signal to the arduino to switch the stepper motor direction. (Ignore the distance sensor part, that is the 2nd part of the project I'm working on but I can't proceed because of this issue.) Any tips?

#include <ContinuousStepper.h>


ContinuousStepper<FourWireStepper> stepper;
ContinuousStepper<FourWireStepper> stepper1;

const int trigPin = 7;
const int echoPin = 8;

float duration, distance;

char c;

void setup() {
    Serial.begin(115200);
    Serial.println("Arduino UART receiver ready");
    stepper.begin(10, 12, 11, 13);
    stepper1.begin(3, 5, 4, 6);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
  


}


void loop() {
    
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);
    distance = (duration*.0343)/2;
    Serial.print("Distance: ");
    Serial.println(distance);
    
    if (distance < 15) {
        
    }

    if (Serial.available()) {
        c = Serial.read();

        if (c == 'L') {
             stepper1.spin(500);
             
             stepper.spin(500);
             
             
        }
        else if (c == 'R') {
             
        }
        else if (c == 'F') {
            stepper1.spin(-500);
            
            stepper.spin(500);
            
            
            
        }
        else if (c == 'S') {
             stepper1.spin(500);
             
             stepper.spin(500);
             
             
        }
    }
    stepper.loop();
    stepper1.loop();
}

Show your wiring. And a schematic.

1 Like

Though I don't have a schematic since Im pretty new at this, I think my wiring is fine. I've tested it by having the motors spin on their own with no other code; the motors can operate fine when you define spin and loop once, the problem comes whenever i try to change spin()

Show your wiring, and a schematic.

Okay then. Since the requested information is not forthcoming, I'm done here.

1 Like

100% of failures are human-caused. Let's deduce and eliminate the cause to find the solution.

Can you describe how you got your stepper motor to move without code? Or, is the motor a DC motor?

Please, post a wiring diagram and a photo of your wiring.

The library README and this forum topic state the library is for accurate speed.

With a 28byj48 4 wire stepper and a uln2003 driver I can switch directions using your code for serial input and the continuous stepper library. I only have one stepper and can not test with two.

There are major issues of interactions between the stepper library and the blocking nature of pulseIn( ). You will do much better to use the TimerOne examples in the library as the template.

I would suggest that you write some code without the HC-SRT04 and see if you can switch direction with serial input. I certainly was able to do it with both a stripped down version of your code or with a version using the TimerOne driven steps.

That is not a definite yes it is properly wired properly. it is a maybe at best. Do the best you can on a schematic, even a paper and pencil is better then what you have given us. Be sure to post links to technical information on each of the hardware devices. Note: Schematics is the language of electronics, guessing is not.

TimerOne Examples help fix the issue, thank you!