My stepper rotates to the left but doesn't turn back

//#include <IOXhop_MCUio.h>
#include <FS.h>                   //this needs to be first, or it all crashes and burns...
#include <SPI.h>

#include <Stepper.h>
#define BLYNK_PRINT Serial        // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <BlynkSimpleEsp8266.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson
#include <SimpleTimer.h>
#include <SoftwareSerial.h>
//#include <StepperMotor.h>
//#include "HX711.h"

//HX711 scale;
BlynkTimer timer;

int step_number = 0;

int blynkIsDownCount=0;

char blynk_token[34] = "***";
float weight;
bool shouldSaveConfig = false;

//IOXhop_MCUio mcu(8,D3,D4);

int state = 0;

const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, D5,D6,D7,D8);
//StepperMotor myStepper(D5,D6,D7,D8);

int motorSpeed;
int motorDirection;

const int Relay1 = D1;
const int Relay2 = D2;
const int Relay3 = D3;
const int Relay4 = D4;


void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}

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

  Serial.println("mounting FS...");

  if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {

      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened config file");
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed json");
          strcpy(blynk_token, json["blynk_token"]);

        } else {
          Serial.println("failed to load json config");
        }
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }
 
    WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 34);

    WiFiManager wifiManager;
    wifiManager.setSaveConfigCallback(saveConfigCallback);
    wifiManager.addParameter(&custom_blynk_token);

  if (!wifiManager.autoConnect("Wifi_Manager", "password")) {
    Serial.println("failed to connect and hit timeout");
    delay(3000);
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(5000);
  }

  //if you get here you have connected to the WiFi
  Serial.println("connected........:)");
  strcpy(blynk_token, custom_blynk_token.getValue());

  //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();

    json["blynk_token"] = blynk_token;
    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }
    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();

  }
  Serial.println("local ip");
  Serial.println(WiFi.localIP());
  Blynk.config(blynk_token);
 
  pinMode(Relay1,OUTPUT);
  pinMode(Relay2,OUTPUT);
  pinMode(Relay3,OUTPUT);
  pinMode(Relay4,OUTPUT);
  digitalWrite(Relay1,HIGH);
  digitalWrite(Relay2,HIGH);
  digitalWrite(Relay3,HIGH);
  digitalWrite(Relay4,HIGH);

  myStepper.setSpeed(200);
//  myStepper.setStepDuration(1);
//  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
//  scale.set_scale(2280.f); 
//  scale.tare(); 


}

BLYNK_WRITE(V0)
{
    if (param.asInt() == 0)
    {
      Blynk.virtualWrite(V1, "1.เปิดฝาหม้อ"); 
      timer.setTimer(10, Servo1,1);  // Run Servo1 loop every 800ms 

    }

}
void Servo1() {
    //myStepper.step(360);
    int j = 0;
    while(j < 1000)
      {
        Blynk.run();
        myStepper.step(200); // again avoid to many steps
        j = j+50;
      }
    Serial.println(j);
    state = 2;
}
void Servo2() {
    int j = 0;
    Serial.println("Reword...Motor");
    myStepper.setSpeed(100);
    while(j < 1000)
      {
        Blynk.run();
        myStepper.step(-200); // again avoid to many steps
        j = j+50;
      }
    Blynk.virtualWrite(V0, 1); 
    state = 0;
}

void loop()
{
  Blynk.run();
  timer.run();

  if(state == 2){
    Blynk.virtualWrite(V1, "2.ชั่งน้ำหนัก"); 
    timer.setTimer(10, put_r,1);
//    put_r();
//    Blynk.virtualWrite(V0, 1);
//    while(weight < 20.0){
//      put_r();
//      Blynk.run();
//      weight = scale.get_units()*0.453592;
//      Serial.println(weight);
//    }
    state = 0;
  }
  if(state == 3){
    digitalWrite(Relay3,LOW);
    Serial.println("st");
    Blynk_Delay(2000);
    digitalWrite(Relay3,HIGH);
    Serial.println("sp");
    Blynk_Delay(1000);
    timer.setTimer(10, Servo2,1);
    
    state = 4;
  }

}
void put_r(){
    Serial.println("เปิดที่ใส่ข้าว");
    digitalWrite(Relay1, LOW);
    digitalWrite(Relay2, HIGH);
    Blynk_Delay(3000);
    close_r();
}
void close_r(){
    Serial.println("ปิดที่ใส่ข้าว");
    digitalWrite(Relay1, HIGH);
    digitalWrite(Relay2, LOW);
    Blynk_Delay(3200);
    st_r();
}
void st_r(){
    Serial.println("หยุดที่ปิดเปิด");
    digitalWrite(Relay1, HIGH);
    digitalWrite(Relay2, HIGH);
    state = 3;
}
BLYNK_CONNECTED()
{
  Blynk.syncAll();
  Serial.println(".");//per debug
}

void reconnecting()
{
  if (!Blynk.connected())
  {
    blynkIsDownCount++;
    BLYNK_LOG("blynk server is down! %d  times", blynkIsDownCount);
    Blynk.connect(5000);
  }
}
void Blynk_Delay(int milli){
   int end_time = millis() + milli;
   while(millis() < end_time){
       Blynk.run();
       yield();
   }
}

You need to edit your code so that it displays correctly, like this:

Blynk%20-%20FTFC

You also need to explain exactly what your question is, and what problems you’re experiencing.

Pete.

I have a problem --> Servo2();
Thank you. Please help me solve the problem. :joy:

beyond my coding skills… this code seems to branch off in multidirections and my small brain cant keep up on the correct trail. the void loop looks complex, and i’ve always heard to keep that one area clean, even if you do nothing else. sorry, im not much help.

Just to confirm … this section of code is intended to rotate the motor counterclockwise 20 revolutions, correct? myStepper.step is a blocking function. Blynk.run only executes in between revolutions.

    while(j < 1000)
      {
        Blynk.run();
        myStepper.step(-200); // again avoid to many steps
        j = j+50;
      }

My code works as follows
When V0 is pressed -> Servo1(); -> put_r(); -> close_r(); -> st_r(); -> Servo2();
Everything works normally except Servo2();

Blynk.run Should I remove this?
It will not work when removing this thing.

I don’t think Blynk.run is the problem. Is my understanding of your function correct? 20 counterclockwise rotations? Is that your intent? Are you able to rotate the motor clockwise and counterclockwise without any Blynk-related code?

I’m not sure I brought from this example.

https://www.arduino.cc/en/Tutorial/StepperOneRevolution

Will you have this solution?
But what I want is to rotate clockwise and counterclockwise.

Can you try this at the very beginning of your setup() routine, prior to any SPIFFS-, wi-fi- or Blynk-related stuff …

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

  myStepper.setSpeed(60);

  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);

  // ...
};

When placed in the setup, it works

Can you try this at the very bottom of your setup() routine, after all the SPIFFS-, wi-fi- and Blynk-related stuff …

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

  // ...

  int j;

  myStepper.setSpeed(60);

  Serial.println("clockwise");
  for (j = 0; j < 200; j++) {
     myStepper.step(1);
     Blynk.run();
  }

  Serial.println("counterclockwise");
  for (j = 0; j < 200; j++) {
     myStepper.step(-1);
     Blynk.run();
  }
};

Both clockwise and counterclockwise It rotates in the same direction.

it work i changed the new library thank you :hugs: