Stepper motor only rotates 1 step, or keeps rotating.

So I'm trying to write a program that allows me to turn a stepper motor on and off wirelessly through an app.
For the wireless connection, I use a NodeMcu ESP8266 and for the motor driver, I use an EasyDriver v4.5.
I programmed the app with MIT App Inventor and the app works well.

The problem is that either my motor turns 1 step (after I turn it on through the app) (first program), or it keeps rotating when I turn the motor on through the app but I can't turn it off (second program). I've tried so many things for days but none of them seem to work.

So what I want is that when I press the "LedAan" button in the app, the motor keeps rotating until I press the "LedUit" button in the app.

I think the problem in the first program is that the wifi client request is "blocking" the other code unless it receives an input (through my app).

I'm sure the driver and motor are working correctly since those work flawlessly in a standalone program I wrote.

I've only been programming for a year so I'm not that good and my way of working / programming might be inefficient.

First program:

#include <ESP8266WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const char WiFiPassword[] = "StarTracker";
const char AP_NameChar[] = "StarTracker" ;

#define SCREEN_WIDTH 128 //breedte OLED scherm (in pixels)
#define SCREEN_HEIGHT 64 //hoogte OLED scherm (in pixels)

#define OLED_RESET     LED_BUILTIN
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //configureert het display met de juiste settings

WiFiServer server(80);

String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
String html_1 = "<!DOCTYPE html><html><head><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>";
String html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='LED ON' ></form>
";
String html_3 = "<form id='F2' action='LEDOFF'><input class='button' type='submit' value='LED OFF' ></form>
";
String html_4 = "</div></body></html>";

String request = "";

static const uint8_t LED = 16;
int BuiltinLedHIGH = 0;
int BuiltinLedLOW = 1;

int StarTrackerMain = 0;

int MotorStep = 12;
int MotorDir = 14;

int OLEDPRINTED = 0;

void setup()
{
  Serial.begin(115200);

  pinMode(LED, OUTPUT);
  digitalWrite(LED, BuiltinLedLOW);

  pinMode (14, OUTPUT);  //MotorDir output
  digitalWrite (14, LOW);  //MotorDir LOW
  pinMode (12, OUTPUT);  //MotorStep output
  digitalWrite (12, LOW);  //MotorStep LOW

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("LED = Uit");
  display.display();

  boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
  server.begin();

} // void setup()


void loop()
{

  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client)  {
    return;
  }

  // Read the first line of the request
  request = client.readStringUntil('\r');

  if       ( request.indexOf("LedAan") > 0 )  {
    StarTrackerMain = 1;
    OLEDPRINTED = 0;
  }
  else if  ( request.indexOf("LedUit") > 0 ) {
    StarTrackerMain = 0;
    OLEDPRINTED = 0;
  }

  if (StarTrackerMain == 1 & OLEDPRINTED == 0) {
    digitalWrite(LED, BuiltinLedHIGH);
    display.clearDisplay();
    display.display();
    display.setCursor(0, 0);
    display.println("LED = Aan");
    display.display();
    OLEDPRINTED = 1;
  }

  if (StarTrackerMain == 1) {
    digitalWrite (12, HIGH);
    delay(1);
    digitalWrite (12, LOW);
    delay(1);
  }

  if (OLEDPRINTED == 0) {
    digitalWrite(LED, BuiltinLedLOW);
    display.clearDisplay();
    display.display();
    display.setCursor(0, 0);
    display.println("LED = Uit");
    display.display();
    OLEDPRINTED = 1;
  }

  client.flush();

  client.print( header );
  client.print( html_1 );
  client.print( html_2 );
  client.print( html_3 );
  client.print( html_4);

  delay(5);
  // The client will actually be disconnected when the function returns and 'client' object is detroyed

}

Second program:

#include <ESP8266WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const char WiFiPassword[] = "StarTracker";
const char AP_NameChar[] = "StarTracker" ;

#define SCREEN_WIDTH 128 //breedte OLED scherm (in pixels)
#define SCREEN_HEIGHT 64 //hoogte OLED scherm (in pixels)

#define OLED_RESET     LED_BUILTIN
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //configureert het display met de juiste settings

WiFiServer server(80);

String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
String html_1 = "<!DOCTYPE html><html><head><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>";
String html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='LED ON' ></form>
";
String html_3 = "<form id='F2' action='LEDOFF'><input class='button' type='submit' value='LED OFF' ></form>
";
String html_4 = "</div></body></html>";

String request = "";

static const uint8_t LED = 16;
int BuiltinLedHIGH = 0;
int BuiltinLedLOW = 1;

int StarTrackerMain = 0;

int MotorStep = 12;
int MotorDir = 14;

char user_input;

int OLED = 0;

void setup()
{
  Serial.begin(9600);

  pinMode(LED, OUTPUT);
  digitalWrite(LED, BuiltinLedLOW);

  pinMode (14, OUTPUT);  //MotorDir output
  digitalWrite (14, LOW);  //MotorDir LOW
  pinMode (12, OUTPUT);  //MotorStep output
  digitalWrite (12, LOW);  //MotorStep LOW

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("LED = Uit");
  display.display();

  boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
  server.begin();

}
void loop() {
  WiFiclient();
  MotorOn();
  MotorOff();
}

void WiFiclient() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client)  {
    return;
  }

  // Read the first line of the request
  request = client.readStringUntil('\r');

  if       ( request.indexOf("LedAan") > 0 )  {
    OLED = 1;
    StarTrackerMain = 1;
  }
  else if  ( request.indexOf("LedUit") > 0 ) {
    OLED = 1;
    StarTrackerMain = 0;
  }

  client.flush();

  client.print( header );
  client.print( html_1 );
  client.print( html_2 );
  client.print( html_3 );
  client.print( html_4);


  delay(5);
}

void MotorOn() {
  if (StarTrackerMain == 1 & OLED == 1) {
    digitalWrite(LED, BuiltinLedHIGH);
    display.clearDisplay();
    display.display();
    display.setCursor(0, 0);
    display.println("LED = Aan");
    display.display();
    OLED = 0;
  }

  while (StarTrackerMain == 1) {
    digitalWrite (12, HIGH);
    delay(1);
    digitalWrite (12, LOW);
    delay(1);
  }
}

void MotorOff() {
  if (StarTrackerMain == 0 & OLED == 1) {
    digitalWrite(LED, BuiltinLedLOW);
    display.clearDisplay();
    display.display();
    display.setCursor(0, 0);
    display.println("LED = Uit");
    display.display();
    OLED = 0;
  }
}

Referring to your second program and your motorOn() function ...

I don't understand why you have this

if (StarTrackerMain == 1 & OLED == 1) {

rather than the simpler

if (StarTrackerMain == 1) {

Likewise in the motorOff() function.

I think the major problem in your motorOn() function is this

  while (StarTrackerMain == 1) {

because there is nothing inside that WHILE loop to change StarTrackerMain back to 0

I don't think you need the WHILE loop at all - try deleting it.

...R

Hi, you were completely right, that while loop caused my problems and that OLED variable wasn't needed. The OLED variable was used to make sure the for() loop would only run once but that's also the case without that variable.

I just found a way to control the motor without the while() function. I put the motor control code (which is the code below) in another "loop" and enabled / disabled the EasyDriver board through a digital signal from the NodeMcu. So instead of enabling / disabling the motor control loop, I just enabled / disabled the driver itself.
This is the motor control code that needs to be looped for the motor to rotate:

  digitalWrite (12, HIGH);
  delay(1);
  digitalWrite (12, LOW);
  delay(1);

My new program now looks like this (the OLED variable is still there but it's already gone in my latest version):

#include <ESP8266WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const char WiFiPassword[] = "StarTracker";
const char AP_NameChar[] = "StarTracker" ;

#define SCREEN_WIDTH 128 //breedte OLED scherm (in pixels)
#define SCREEN_HEIGHT 64 //hoogte OLED scherm (in pixels)

#define OLED_RESET     LED_BUILTIN
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //configureert het display met de juiste settings

WiFiServer server(80);

String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
String html_1 = "<!DOCTYPE html><html><head><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>";
String html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='LED ON' ></form>
";
String html_3 = "<form id='F2' action='LEDOFF'><input class='button' type='submit' value='LED OFF' ></form>
";
String html_4 = "</div></body></html>";

String request = "";

static const uint8_t LED = 16;
int BuiltinLedHIGH = 0;
int BuiltinLedLOW = 1;

int StarTrackerMain = 0;

int MotorStep = 12;
int MotorDir = 14;
int MotorEnablePin = 0;

int OLEDPRINTED = 0;

void setup()
{
  Serial.begin(115200);

  pinMode(LED, OUTPUT);
  digitalWrite(LED, BuiltinLedLOW);

  pinMode(MotorDir, OUTPUT);  //MotorDir output
  digitalWrite(MotorDir, LOW);  //MotorDir LOW
  pinMode(MotorStep, OUTPUT);  //MotorStep output
  digitalWrite(MotorStep, LOW);  //MotorStep LOW
  pinMode(MotorEnablePin, OUTPUT);
  digitalWrite(MotorEnablePin, HIGH);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("LED = Uit");
  display.display();

  boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
  server.begin();

} // void setup()


void loop()
{

  Motor();
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client)  {
    return;
  }

  // Read the first line of the request
  request = client.readStringUntil('\r');

  if       ( request.indexOf("LedAan") > 0 )  {
    StarTrackerMain = 1;
    OLEDPRINTED = 0;
    digitalWrite(MotorEnablePin, LOW);
  }
  else if  ( request.indexOf("LedUit") > 0 ) {
    StarTrackerMain = 0;
    OLEDPRINTED = 0;
    digitalWrite(MotorEnablePin, HIGH);
  }

  if (StarTrackerMain == 1 & OLEDPRINTED == 0) {
    digitalWrite(LED, BuiltinLedHIGH);
    display.clearDisplay();
    display.display();
    display.setCursor(0, 0);
    display.println("LED = Aan");
    display.display();
    OLEDPRINTED = 1;
  }

  /*  if (StarTrackerMain == 1) {
      digitalWrite (MotorStep, HIGH);
      delay(1);
      digitalWrite (MotorStep, LOW);
      delay(1);
    } */

  if (OLEDPRINTED == 0) {
    digitalWrite(LED, BuiltinLedLOW);
    display.clearDisplay();
    display.display();
    display.setCursor(0, 0);
    display.println("LED = Uit");
    display.display();
    OLEDPRINTED = 1;
  }

  client.flush();

  client.print( header );
  client.print( html_1 );
  client.print( html_2 );
  client.print( html_3 );
  client.print( html_4);

  delay(5);
  // The client will actually be disconnected when the function returns and 'client' object is detroyed

}

void Motor() {
  digitalWrite (12, HIGH);
  delay(1);
  digitalWrite (12, LOW);
  delay(1);
}

Also sorry for my bad English, I'm Dutch :wink:

Mr_BananaPants:
Also sorry for my bad English, I'm Dutch :wink:

I had not noticed that there was anything wrong with it :slight_smile:

...R

Here's my final version (for now) which can also control the speed of the motor in case you wanted to know how I've fixed it. The code that controlled the OLED screen is also a lot more efficient. In case you have any suggestions or things that could've been done better, I'm happy to know!

#include <ESP8266WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


/////LOCAL ACCESS POINT SETTINGS/////
const char WiFiPassword[] = "StarTracker";
const char AP_NameChar[] = "StarTracker" ;
WiFiServer server(80);
/////LOCAL ACCESS POINT SETTINGS/////


/////OLED SETTINGS/////
#define SCREEN_WIDTH 128 //breedte OLED scherm (in pixels)
#define SCREEN_HEIGHT 64 //hoogte OLED scherm (in pixels)
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //configureert het display met de juiste settings
/////OLED SETTINGS/////


/////HTML STRINGS/////
String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
String html_1 = "<!DOCTYPE html><html><head><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>";
String html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='LED ON' ></form>
";
String html_3 = "<form id='F2' action='LEDOFF'><input class='button' type='submit' value='LED OFF' ></form>
";
String html_4 = "</div></body></html>";
String request = "";
/////HTML STRINGS/////


/////BUILTINLED PINS + VARIABLES/////
static const uint8_t LED = 16;
int BuiltinLedHIGH = 0;
int BuiltinLedLOW = 1;
String MotorSnelheid = "traag";
String MotorStatus = "uit";
/////BUILTINLED PINS + VARIABLES/////


/////VARIABLES/////
int StarTrackerMain = 0;
int MotorSnel = 0;
/////VARIABLES/////


/////MOTOR CONNECTIONS/////
int MotorStep = 12;
int MotorDir = 14;
int MotorEnablePin = 0;
int MotorMS1 = 9;
int MotorMS2 = 10;
/////MOTOR CONNECTIONS/////


void setup() {

  Serial.begin(115200);

  /////BUILTINLED SETTINGS/////
  pinMode(LED, OUTPUT);
  digitalWrite(LED, BuiltinLedLOW);
  /////BUILTINLED SETTINGS/////


  /////MOTOR SETTINGS/////
  pinMode(MotorDir, OUTPUT);  //MotorDir output
  digitalWrite(MotorDir, LOW);  //MotorDir LOW
  pinMode(MotorStep, OUTPUT);  //MotorStep output
  digitalWrite(MotorStep, LOW);  //MotorStep LOW

  pinMode(MotorEnablePin, OUTPUT);  //MotorEnablePin output
  digitalWrite(MotorEnablePin, HIGH);

  pinMode(MotorMS1, OUTPUT);  //MotorMS1 output
  digitalWrite(MotorMS1, HIGH);  //MotorMS1 LOW
  pinMode(MotorMS2, OUTPUT);  //MotorMS2 output
  digitalWrite(MotorMS2, HIGH);  //MotorMS2 LOW
  /////MOTOR SETTINGS/////


  /////OLED SETTINGS/////
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
  /////OLED SETTINGS/////


  /////START LOCAL ACCESS POINT + SERVER/////
  boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
  server.begin();
  /////START LOCAL ACCESS POINT + SERVER/////
}


void loop() {


  /////START MOTOR + OLED VOID/////
  Motor();
  /////START MOTOR + OLED VOID/////


  /////WIFI CLIENT/////
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client)  {
    return;
  }
  /////WIFI CLIENT/////


  /////SERVER REQUESTS/////
  request = client.readStringUntil('\r');  //Leest de eerste regel van het request

  if       ( request.indexOf("StarTrackerMainAan") > 0 )  {
    digitalWrite(MotorEnablePin, LOW);
    MotorStatus = "aan";
  }
  else if  ( request.indexOf("StarTrackerMainUit") > 0 ) {
    digitalWrite(MotorEnablePin, HIGH);
    MotorStatus = "uit";
  }
  else if  ( request.indexOf("SpeedSnel") > 0 ) {
    digitalWrite(MotorMS2, LOW);
    digitalWrite(MotorMS1, LOW);
    MotorSnelheid = "snel";
  }
  else if  ( request.indexOf("SpeedTraag") > 0 ) {
    digitalWrite(MotorMS2, HIGH);
    digitalWrite(MotorMS1, HIGH);
    MotorSnelheid = "traag";
  }
  /////SERVER REQUESTS/////


  /////OLED SCREEN STATUS + MOTOR SPEED/////
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print("Status: ");
  display.println(MotorStatus);
  display.print("Snelheid: ");
  display.println(MotorSnelheid);
  display.display();
  /////OLED SCREEN STATUS + MOTOR SPEED/////


  /////HTML RESPONSE/////
  client.flush();

  client.print( header );
  client.print( html_1 );
  client.print( html_2 );
  client.print( html_3 );
  client.print( html_4);

  // The client will actually be disconnected when the function returns and 'client' object is detroyed
  delay(5);
  /////HTML RESPONSE/////
}



/////MOTOR DRIVER/////
void Motor() {
  digitalWrite (12, HIGH);
  delay(1);
  digitalWrite (12, LOW);
  delay(1);
}
/////MOTOR DRIVER/////