Corrected Schematic
I made adjustment to the circuit using couple of ceramic capacitors to stablize, but still the same issue, Its working like oracle's fortune ball...lol.
I bring my finger closer to it starts moving all the motors, irrespective of the underlying conditions.
HEre is my code that I uploaded it to ESP01.
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_MCP23017.h>
#include <AccelStepper.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>
#include "wifiparams.h"
#define MOVETO 2000 // position to which to move
#define SPEED 200 // initial constant speed
#define MAXSPEED 300 // constant speed
#define ACCEL 50
#define MCP23017_ADDRESS 0x20
#define enablePin 1 //TX Pin
#define CtrlPin 3 //Rx Pin
#define motorInterfaceType 1
#define NUM_MOTORS 2 // # of motors to control
char help_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8" >
<title>ESP-01 WiFi stepper controller</title>
</head>
<body>
<h2>ESP-01 WiFi stepper controller</h2>
<p>Control stepper:
<a href="/CCW" target="_self" >CCW</a>
<a href="/CW" target="_self">CW</a>
<a href="/OFF" target="_self">OFF</a></p>
<p>current status : MYSTATUS </p>
</body>
</html>
)rawliteral";
const int STEPS_PER_REV = 2048;
Adafruit_MCP23017 mcp;
long int lowPosition ;
long int highPosition;
// int step_pin[2] = {4, 22};
// int dir_pin[2] = {5,21};
int step_pin[2] = {11, 1};
int dir_pin[2] = {12,0};
// int motor3_PIN[2] = {7, 22};
// DEFINE ARRAY OF STEPPER MOTORS
AccelStepper motor1 = AccelStepper(motorInterfaceType, step_pin[0], dir_pin[0]);
AccelStepper motor2 = AccelStepper(motorInterfaceType, step_pin[1], dir_pin[1]);
AccelStepper steppers[NUM_MOTORS] = {motor1, motor2};
AsyncWebServer server(80);
AccelStepper *ActiveMotor;
int ActiveNum;
class ClientObj{
public:
AsyncClient client;
String status="";
void setStatus(String newstatus){
status = newstatus;
}
void print(String linetobeprinted){
Serial.println( linetobeprinted);
}
};
ClientObj myclient;
String status = "";
void printHelp(AsyncWebServerRequest *Request){
AsyncClient *aclient = Request->client();
String htmlstr = String(help_html);
String ctrlpin_input = String(analogRead(CtrlPin));
String ctrlpin_char = String(ctrlpin_input);
String pos;
String distancetoGo;
for(int i=0; i<NUM_MOTORS; i++){
pos = pos + "Blind " + String(i) + ": " + String(steppers[i].currentPosition()) + "\n";
distancetoGo = distancetoGo + String(i) + ": " + String(steppers[i].distanceToGo()) + "\n";
}
htmlstr.replace("MYSTATUS ", myclient.status + '@' + aclient->remoteIP().toString().c_str() +
" Pin:" + ctrlpin_char + " LowPoint:" + lowPosition + " HighPoint:" + highPosition +
"\n DISTANCE TO GO: \n" + distancetoGo + "\nPOSITION :\n" + pos );
Request->send(200, "text/html", (const String) htmlstr);
}
// Initialize WiFi
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); //--> Connect to your WiFi router
//----------------------------------------Wait for connection
delay(5000);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
Serial.println("");
Serial.println("WiFi connected");
}
void rotate(AccelStepper stepper){
stepper.enableOutputs();
while ( (myclient.status.indexOf("OFF") !=0) && (stepper.distanceToGo() != 0))
{
stepper.run();
delayMicroseconds(2000);
//delay(500);
}
if (stepper.distanceToGo() == 0){
stepper.disableOutputs();
delayMicroseconds(500);
}
stepper.disableOutputs();
}
void CCW( AsyncWebServerRequest *Request){
// AsyncClient *aclient = Request->client();
// Serial.printf("client ip : %s\n", aclient->remoteIP().toString().c_str());
status = myclient.status;
// Serial.println("CCW()");
// Match the request
if (status.indexOf("CCW") !=0 )
{
myclient.status="CCW";
for(int i=0;i<NUM_MOTORS;i++){
steppers[i].enableOutputs();
if (steppers[i].isRunning()) {
steppers[i].stop();
delayMicroseconds(500);
}
steppers[i].setSpeed(-SPEED);
steppers[i].moveTo(lowPosition);
delayMicroseconds(500);
}
}
printHelp(Request);
}
void CW(AsyncWebServerRequest *Request){
// AsyncClient *aclient = Request->client();
// Serial.printf("client ip : %s\n", aclient->remoteIP().toString().c_str());
status = myclient.status;
// Serial.println("CW()");
if (status.indexOf("CW") !=0 ){
myclient.status="CW";
for ( int i =0; i < NUM_MOTORS;i++){
steppers[i].enableOutputs();
if (steppers[i].isRunning()) {
steppers[i].stop();
delayMicroseconds(500);
}
steppers[i].setSpeed(SPEED);
steppers[i].moveTo(highPosition);
delayMicroseconds(500);
}
}
printHelp(Request);
}
void setLow(AsyncWebServerRequest *Request){
// AsyncClient *aclient = Request->client();
// Serial.printf("client ip : %s\n", aclient->remoteIP().toString().c_str());
status = myclient.status;
// Serial.println("CW()");
if (status.indexOf("OFF") == 0 ){
ActiveMotor->enableOutputs();
long int currpos = ActiveMotor->currentPosition();
for (int i =0; i< NUM_MOTORS; i++){
steppers[i].enableOutputs();
lowPosition = ( 0 < currpos ) ? currpos : 0;
steppers[i].disableOutputs();
}
}
printHelp(Request);
}
void setHigh(AsyncWebServerRequest *Request){
// AsyncClient *aclient = Request->client();
// Serial.printf("client ip : %s\n", aclient->remoteIP().toString().c_str());
status = myclient.status;
// Serial.println("CW()");
if (status.indexOf("OFF") == 0 ){
ActiveMotor->enableOutputs();
long int currpos = ActiveMotor->currentPosition();
for(int i=0; i<NUM_MOTORS; i++){
steppers[i].enableOutputs();
highPosition = (currpos < MOVETO )? currpos: MOVETO;
steppers[i].disableOutputs();
}
}
printHelp(Request);
}
void setOFF(){
status = myclient.status;
// Serial.println("LED is OFF()");
if (status.indexOf("OFF") <0 ){
myclient.status="OFF";
for ( int i = 0; i<NUM_MOTORS; i++){
steppers[i].stop();
delayMicroseconds(500);
steppers[i].disableOutputs();
}
digitalWrite(enablePin,HIGH);
}
}
void OFF(AsyncWebServerRequest *Request){
// AsyncClient * aclient = Request->client();
// Serial.printf("client ip : %s\n", aclient->remoteIP().toString().c_str());
setOFF();
printHelp(Request);
}
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
printHelp(request);
}
void setuprequest(){
server.on("/OFF", HTTP_GET, OFF);
server.on("/CCW", HTTP_GET, CCW);
server.on("/CW" , HTTP_GET, CW);
server.on("/setLow", HTTP_GET, setLow);
server.on("/setHigh", HTTP_GET, setHigh);
server.onNotFound(notFound);
// Web Server Root URL
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
printHelp(request);
});
}
void setup() {
Serial.begin(115200);
delay(1000);
myclient.status=String("OFF");
highPosition = MOVETO;
lowPosition = 0.0;
initWiFi();
// define handlers for the request;
setuprequest();
Serial.flush();
Serial.end();
// start the server
server.begin();
Wire.begin(0,2);
// mcp.begin(MCP23017_ADDRESS);
mcp.begin();
pinMode(enablePin,FUNCTION_3);
pinMode(enablePin, OUTPUT);
// set motor pins as output
for (int i = 0; i < NUM_MOTORS; i++) {
mcp.pinMode(step_pin[i], OUTPUT);
// mcp.digitalWrite(step_pin[i], LOW);
mcp.pinMode(dir_pin[i], OUTPUT);
// mcp.digitalWrite(dir_pin[i], LOW);
steppers[i].enableOutputs();
steppers[i].setAcceleration(ACCEL);
steppers[i].setSpeed(SPEED);
steppers[i].setMaxSpeed(MAXSPEED);
}
setOFF();
// for now disengage everything
ActiveNum=0;
}
void loop(){
ActiveMotor = &(steppers[ActiveNum]);
if ((myclient.status.indexOf("OFF") != 0) && (ActiveNum < NUM_MOTORS))
{
if ( (ActiveMotor->distanceToGo() != 0))
{
ActiveMotor->run();
delayMicroseconds(1000);
// delay(500);
}
else if (ActiveMotor->distanceToGo() == 0)
{
ActiveMotor->disableOutputs();
delayMicroseconds(500);
ActiveNum = ActiveNum+1;
}
} else if (ActiveNum == NUM_MOTORS){
setOFF();
// reset the ActiveMotor index
ActiveNum = 0;
}
}