Stop() Function for AccelStepper library

Thank you for your help, I started learning programming a few months ago and have been pretty successful at solving most problems, but cant get past this one.
I am working on controlling 4 NEMA17 Bipolar Stepper Motors through a webserver via an esp32 board. Each Stepper motor is controlled by their own A4988 drivers. I have 5 simple functions and controls: FORWARD, LEFT, RIGHT, REVERSE, and STOP. From the start, the Stepper motors will spin without any input from the called functions. They do however respond to the functions for FORWARD, REVERSE, LEFT and RIGHT, but they will not respond to STOP. I am looking for a fix for those two issues. Why the Stepper Motors are already running before being called to, and why is my STOP function not stopper the Stepper Motors.

Thank you again for your help and responses!

#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#include <AccelStepper.h>

// REPLACE WITH YOUR NETWORK CREDENTIALS
const char* ssid = "**********";
const char* password = "***********";

// Define pin connections
int Sleep = 15; //GPIO15---D15 of Nodemcu-Control Sleep Mode on A4988
int M1Step = 35; //GPIO35---D35 of Nodemcu--Step of stepper motor driver
int M1Dire  = 34; //GPIO34---D34 of Nodemcu--Direction of stepper motor driver
int M2Step = 5; //GPIO5---D5 of Nodemcu--Step of stepper motor driver
int M2Dire  = 18; //GPIO18---D18 of Nodemcu--Direction of stepper motor driver
int M3Step = 19; //GPIO19---D19 of Nodemcu--Step of stepper motor driver
int M3Dire  = 21; //GPIO21---D21 of Nodemcu--Direction of stepper motor driver
int M4Step = 22; //GPIO22---D22 of Nodemcu--Step of stepper motor driver
int M4Dire  = 23; //GPIO23---D23 of Nodemcu--Direction of stepper motor driver

// Define motor interface type
#define motorInterfaceType 1

// Creates an instance
AccelStepper myStepper1(motorInterfaceType, M1Step, M1Dire);
AccelStepper myStepper2(motorInterfaceType, M2Step, M2Dire);
AccelStepper myStepper3(motorInterfaceType, M3Step, M3Dire);
AccelStepper myStepper4(motorInterfaceType, M4Step, M4Dire);



// HTML web page
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<title>FoxBot Control</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial; text-align: center; margin:0px auto; padding-top: 30px;}
.button {
padding: 10px 20px;
font-size: 24px;
text-align: center;
outline: none;
color: #fff;
background-color: #2f4468;
border: none;
border-radius: 5px;
box-shadow: 0 6px #999;
cursor: pointer;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
.button:hover {background-color: #1f2e45}
.button:active {
background-color: #1f2e45;
box-shadow: 0 4px #666;
transform: translateY(2px);
}
</style>
</head>
<body>
<h1>FoxBot Web Server</h1>
<p>
<button class="button" onmousedown="toggleCheckbox('forward');" ontouchstart="toggleCheckbox('forward');" onmouseup="toggleCheckbox('stop');" ontouchend="toggleCheckbox('stop');">FORWARD</button>
</p>
<p>
<button class="button" onmousedown="toggleCheckbox('left');" ontouchstart="toggleCheckbox('left');" onmouseup="toggleCheckbox('stop');" ontouchend="toggleCheckbox('stop');">LEFT </button>

<button class="button" onmousedown="toggleCheckbox('right');" ontouchstart="toggleCheckbox('right');" onmouseup="toggleCheckbox('stop');" ontouchend="toggleCheckbox('stop');">RIGHT</button>
</p>
<p>
<button class="button" onmousedown="toggleCheckbox('reverse');" ontouchstart="toggleCheckbox('reverse');" onmouseup="toggleCheckbox('stop');" ontouchend="toggleCheckbox('stop');">REVERSE</button>
</p>
<p>
<button class="button" onmousedown="toggleCheckbox('stop');" ontouchstart="toggleCheckbox('stop');" onmouseup="toggleCheckbox('stop');" ontouchend="toggleCheckbox('stop');">STOP</button>
</p>
<script>
function toggleCheckbox(x) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/" + x, true);
xhr.send();
}


</script>
</body>
</html>)rawliteral";

void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}

AsyncWebServer server(80);

void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed!");
return;
}
Serial.println();
Serial.print("ESP IP Address: http://");
Serial.println(WiFi.localIP());


myStepper1.setMaxSpeed(1000);
myStepper2.setMaxSpeed(1000);
myStepper3.setMaxSpeed(1000);
myStepper4.setMaxSpeed(1000);
  



// Send web page to client
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html);
});

// Receive an HTTP GET request for Output 12
server.on("/forward", HTTP_GET, [] (AsyncWebServerRequest *request) {
    myStepper1.setSpeed(200);
    myStepper2.setSpeed(200);
    myStepper3.setSpeed(200);
    myStepper4.setSpeed(200);
    
    myStepper1.runSpeed();
    myStepper2.runSpeed();
    myStepper3.runSpeed();
    myStepper4.runSpeed();
request->send(200, "text/plain", "ok");
});

// Receive an HTTP GET request for Output 14
server.on("/left", HTTP_GET, [] (AsyncWebServerRequest *request) {
    myStepper1.setSpeed(-200);
    myStepper2.setSpeed(200);
    myStepper3.setSpeed(-200);
    myStepper4.setSpeed(200);
    
    myStepper1.runSpeed();
    myStepper2.runSpeed();
    myStepper3.runSpeed();
    myStepper4.runSpeed();
request->send(200, "text/plain", "ok");
});

// Receive an HTTP GET request for Output 27
server.on("/right", HTTP_GET, [] (AsyncWebServerRequest *request) {
    myStepper1.setSpeed(200);
    myStepper2.setSpeed(-200);
    myStepper3.setSpeed(200);
    myStepper4.setSpeed(-200);
    
    myStepper1.runSpeed();
    myStepper2.runSpeed();
    myStepper3.runSpeed();
    myStepper4.runSpeed();
request->send(200, "text/plain", "ok");
});

// Receive an HTTP GET request for All Output's
server.on("/reverse", HTTP_GET, [] (AsyncWebServerRequest *request) {
    myStepper1.setSpeed(-200);
    myStepper2.setSpeed(-200);
    myStepper3.setSpeed(-200);
    myStepper4.setSpeed(-200);
    
    myStepper1.runSpeed();
    myStepper2.runSpeed();
    myStepper3.runSpeed();
    myStepper4.runSpeed();
request->send(200, "text/plain", "ok");
});

// Receive an HTTP GET request for All Output's
server.on("/stop", HTTP_GET, [] (AsyncWebServerRequest *request) {
    myStepper1.stop();
    myStepper2.stop();
    myStepper3.stop();
    myStepper4.stop();
    


request->send(200, "text/plain", "ok");
});
server.onNotFound(notFound);
server.begin();
}

void loop() {
}

Your topic has been moved. Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Thanks for using code tags in your first post :+1:

Do you have a current version of AccelStepper?

http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#a638817b85aed9d5cd15c76a76c00aced

Does this example help?
http://www.airspayce.com/mikem/arduino/AccelStepper/Quickstop_8pde-example.html

Online help...
https://groups.google.com/g/accelstepper?pli=1

There are two basically different methods to create steps with AccelStepper:

stepper.run();
stepper.runSpeed();

stepper.run() uses acceleration/deceleration and is a positioning tool: The stepper will move to a previously set target. if the target is reached, stepper.run() stops creating step pulses.
stepper.runSpeed() is simply to move the stepper at the set speed. Without Acceleration/Deceleration and without checking any target. It simply creates step pulses (according to the set speed) as long as it is called.

Stepper.stop() only makes sense together with stepper.run(), because it only sets a new target as close to the current position that it can be reached with the current deceleration.
Because stepper.runSpeed doesn't care about targets it will not react on stepper.stop(). To stop a stepper when using stepper.runSpeed() simply stop calling this function.

can you please tell exactly which board and libraries you use? When I compile and load it on my ESP32 Dev Module it crashes:

E (273) gpio: GPIO can only be used ⸮.J⸮⸮⸮сmode
E (274) gpio: GPIO can only be used as input mode

( and some more errors )

I ended up correcting the issue by using an arduino nano in serial communication with the ESP32. The arduino was able to handle the stepper motor drivers a lot better. To stop the stepper motors I was able to use "myStepper1.setSpeed(0);" in order to stop the motors, for some reason that worked on the nano but not the esp32 board. Than you for your help!