Hello everyone, I'm working on a project where I'm trying to control a stepper motor with an Easy Driver and an ESP8266 EP-01. I have a feeling I have everything wired correctly and the problem is a caveat. I have the following programming on the ESP
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "MySpectrumWiFiEB-2G"
#define STAPSK "pw"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
ESP8266WebServer server(80);
const int led = 13;
int dirPin = 2;
int stepPin = 5;
int enPin = 3;
String str;
String str1;
String forwardX;
int iforwardX;
String backwardX;
int ibackwardX;
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/plain", "hello from esp8266!\r\n");
digitalWrite(led, 0);
}
void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
//Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, HIGH);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
if (MDNS.begin("esp8266")) {
//Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/godoor", []() {
str1 = server.arg("todo");
str= str1.substring(0,str1.length());
if(str.indexOf("move")>=0) {
forwardX =getValue(str,',',1);
iforwardX = forwardX.toInt();
backwardX =getValue(str,',',2);
ibackwardX = backwardX.toInt();
digitalWrite(enPin, LOW);
for (int i = 0; i < iforwardX; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
digitalWrite(dirPin,LOW);
for (int i = 0; i < ibackwardX; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
digitalWrite(dirPin,HIGH);
digitalWrite(enPin, HIGH);
//Serial.println("loop"); // read it and send it out Serial (USB)
}
server.send(200, "text/plain", "Motor should have moved "+forwardX);
});
server.addHook([](const String & method, const String & url, WiFiClient * client, ESP8266WebServer::ContentTypeFunction contentType) {
(void)method; // GET, PUT, ...
(void)url; // example: /root/myfile.html
(void)client; // the webserver tcp client connection
(void)contentType; // contentType(".html") => "text/html"
/*Serial.printf("A useless web hook has passed\n");
Serial.printf("(this hook is in 0x%08x area (401x=IRAM 402x=FLASH))\n", esp_get_program_counter());*/
return ESP8266WebServer::CLIENT_REQUEST_CAN_CONTINUE;
});
server.begin();
//Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
MDNS.update();
}
When I navigate to 192.168.2.5/godoor?todo=move,900,8 the motor jumps and barely moves. It makes me think that it's not honoring the delayMicroseconds function. When I wire it to a Leonardo, it works perfectly. Does anybody have any ideas why this won't work?
Thanks in advanced for any insight!