Controling stepper with blynk use a last value step

I am making a program to drive stepper by using input from the terminal, everything moves normally and well, but I have problems when the hardware is dead, or restarted in the middle of the road. And finally the motor isn’t in the starting position anymore. for example: the motor moves to box 1 (for example it has moved 255 steps) and suddenly the engine stops then the last stop position will be the starting point again. then how do I read the value of the last step? can I read the last value from the web ?? Thank you and I attach my code:

 * nama : Indrawan arifianto
 * tgl  : 30/7/2019
 */

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <Stepper.h>

//
#define STEPS 2038 // the number of steps in one revolution of your motor (28BYJ-48)
Stepper stepper(STEPS, 33, 4, 32, 1);
int PreviousStep=0;
int steps=0;


//Define LED
int pinArray[] = {15, 2, 23, 22, 21, 19,5};
int count = 0;
int prev_Led=0;
int Led=0;

// You should get Auth Token in the Blynk App.
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***********************************************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***********************";
char pass[] = "**************************";

// Attach virtual serial terminal to Virtual Pin V1
WidgetTerminal terminal(V16);

// You can send commands from Terminal to your hardware. Just use
// the same Virtual Pin as your Terminal Widget
BLYNK_WRITE(V16)
{

  // if you type "Marco" into Terminal Widget - it will respond: "Polo:"
  if (String("1") == param.asStr()) {
    terminal.println("Buka kotak 1") ;

    //Program Lampu
    Led=15;
    //matikan lampu led sebelumnya
    digitalWrite(prev_Led, LOW);
    //nyalakan lampu Led Sekarang
    digitalWrite(Led, HIGH);
    prev_Led=Led;
    
    // Fungsi penggerak Stepper
    steps=255;
    Serial.print("steps: ");Serial.print(steps);Serial.print("  |  ");Serial.print("PreviousStep: ");Serial.println(PreviousStep);
    Serial.print("Buka kotak 1  ");Serial.println(steps-PreviousStep); Serial.println("");
    stepper.setSpeed(4); // 1 rpm
    stepper.step(steps-PreviousStep); // gerakan ke posisi, jumlah step-nilai step sebelumnya
    PreviousStep=steps;
    delay(1000);
    
  } else if (String("2") == param.asStr()) {
    terminal.println("Buka kotak 2") ;

    //Program Lampu
    Led=2;
    //matikan lampu led sebelumnya
    digitalWrite(prev_Led, LOW);
    //nyalakan lampu Led Sekarang
    digitalWrite(Led, HIGH);
    prev_Led=Led;
    
    // Fungsi penggerak Stepper
    steps=510;
    Serial.print("steps: ");Serial.print(steps);Serial.print("  |  ");Serial.print("PreviousStep: ");Serial.println(PreviousStep);
    Serial.print("Buka kotak 2  ");Serial.println(steps-PreviousStep); Serial.println("");
    stepper.setSpeed(4); // 1 rpm
    stepper.step(steps-PreviousStep); // do 2038 steps -- corresponds to one revolution in one minute
    PreviousStep=steps;
    delay(1000);
    
  } else if (String("3") == param.asStr()) {
    terminal.println("Buka kotak 3") ;

    //Program Lampu
    Led=23;
    //matikan lampu led sebelumnya
    digitalWrite(prev_Led, LOW);
    //nyalakan lampu Led Sekarang
    digitalWrite(Led, HIGH);
    prev_Led=Led;
    
    // Fungsi penggerak Stepper
    steps=765;
    Serial.print("steps: ");Serial.print(steps);Serial.print("  |  ");Serial.print("PreviousStep: ");Serial.println(PreviousStep);
    Serial.print("Buka kotak 3  ");Serial.println(steps-PreviousStep); Serial.println("");
    stepper.setSpeed(4); // 1 rpm
    stepper.step(steps-PreviousStep); // gerakan ke posisi, jumlah step-nilai step sebelumnya
    PreviousStep=steps; //nilai previousStep  nilai stepsnya= 765
    delay(1000);
        
  } else if (String("4") == param.asStr()) {
    terminal.println("Buka kotak 4") ;

    //Program Lampu
    Led=22;
    //matikan lampu led sebelumnya
    digitalWrite(prev_Led, LOW);
    //nyalakan lampu Led Sekarang
    digitalWrite(Led, HIGH);
    prev_Led=Led;
    
    // Fungsi penggerak Stepper
    steps=1019;
    Serial.print("steps: ");Serial.print(steps);Serial.print("  |  ");Serial.print("PreviousStep: ");Serial.println(PreviousStep);
    Serial.print("Buka kotak 4  ");Serial.println(steps-PreviousStep); Serial.println("");
    stepper.setSpeed(4); // 1 rpm
    stepper.step(steps-PreviousStep); // do 2038 steps -- corresponds to one revolution in one minute
    PreviousStep=steps;
    delay(1000);
      
  } else if (String("5") == param.asStr()) {
    terminal.println("Buka kotak 5") ;

    //Program Lampu
    Led=21;
    //matikan lampu led sebelumnya
    digitalWrite(prev_Led, LOW);
    //nyalakan lampu Led Sekarang
    digitalWrite(Led, HIGH);
    prev_Led=Led;
    
    // Fungsi penggerak Stepper
    steps=1274;
    Serial.print("steps: ");Serial.print(steps);Serial.print("  |  ");Serial.print("PreviousStep: ");Serial.println(PreviousStep);
    Serial.print("Buka kotak 5  ");Serial.println(steps-PreviousStep); Serial.println("");
    stepper.setSpeed(4); // 1 rpm
    stepper.step(steps-PreviousStep); // do 2038 steps -- corresponds to one revolution in one minute
    PreviousStep=steps;
    delay(1000);
      
  } else if (String("6") == param.asStr()) {
    terminal.println("Buka kotak 6") ;

    //Program Lampu
    Led=19;
    //matikan lampu led sebelumnya
    digitalWrite(prev_Led, LOW);
    //nyalakan lampu Led Sekarang
    digitalWrite(Led, HIGH);
    prev_Led=Led;
    
    // Fungsi penggerak Stepper
    steps=1529;
    Serial.print("steps: ");Serial.print(steps);Serial.print("  |  ");Serial.print("PreviousStep: ");Serial.println(PreviousStep);
    Serial.print("Buka kotak 6  ");Serial.println(steps-PreviousStep); Serial.println("");
    stepper.setSpeed(4); // 1 rpm
    stepper.step(steps-PreviousStep); // do 2038 steps -- corresponds to one revolution in one minute
    PreviousStep=steps;
    delay(1000);
    
  } else if (String("close") == param.asStr()|| String("0") == param.asStr()){
    terminal.println("Close");

    //Program Lampu
    Led=prev_Led;
    //matikan lampu led sebelumnya
    digitalWrite(prev_Led, LOW);
    //nyalakan lampu Led Sekarang
    digitalWrite(Led, HIGH);
    prev_Led=Led;
    
    if(PreviousStep<=765 && PreviousStep>=0 ){
    // Fungsi penggerak Stepper
    steps=0;
    Serial.print("steps: ");Serial.print(steps);Serial.print("  |  ");Serial.print("PreviousStep: ");Serial.println(PreviousStep);
    Serial.print(" tutup counter  ");Serial.println(steps-PreviousStep);Serial.println("");
    stepper.setSpeed(4); // 1 rpm
    stepper.step(steps-PreviousStep); // do 2038 steps -- corresponds to one revolution in one minute
    PreviousStep=steps;
    delay(1000);
    
    }else {
    // Fungsi penggerak Stepper
    steps=2038;
    Serial.print("steps: ");Serial.print(steps);Serial.print("  |  ");Serial.print("PreviousStep: ");Serial.println(PreviousStep);
    Serial.print(" tutup clockwise");Serial.println(steps-PreviousStep);
    stepper.setSpeed(4); // 1 rpm
    stepper.step(steps-PreviousStep); // do 2038 steps -- corresponds to one revolution in one minute
    PreviousStep=steps;
    delay(1000);
    }
  }else {
    terminal.println("wrong input(stay)") ;  

    //Program Lampu
    Led=prev_Led;
    //matikan lampu led sebelumnya
    digitalWrite(prev_Led, LOW);
    //nyalakan lampu Led Sekarang
    digitalWrite(Led, HIGH);
    prev_Led=Led;
    
    // Fungsi penggerak Stepper
    steps=PreviousStep;
    Serial.print("steps: ");Serial.print(steps);Serial.print("  |  ");Serial.print("PreviousStep: ");Serial.println(PreviousStep);
    Serial.print(" fix position (tidak bergerak)"); Serial.println(steps-PreviousStep);Serial.println("");
    stepper.step(steps-PreviousStep); //steps = 0, not move
    PreviousStep=steps;
    delay(1000);
  }
  
  
  // Ensure everything is sent
  terminal.flush();      
} 

BLYNK_CONNECTED() {
    Blynk.syncAll();
}


void setup()
{
  // Debug console
  //Serial.begin(9600);
  for (count=0;count<7;count++) {
    pinMode(pinArray[count], OUTPUT);
  }

  for (count=0;count<7;count++) {
    pinMode(pinArray[count], LOW);
  }
  
  Blynk.begin(auth, ssid, pass);

  // Clear the terminal content
  terminal.clear();

  //
  steps=0;
  // This will print Blynk Software version to the Terminal Widget when
  // your hardware gets connected to Blynk Server
  terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
  terminal.println(F("-------------"));
  terminal.println(F("Type 'Marco' and get a reply, or type"));
  terminal.println(F("anything else and get it printed back."));
  terminal.flush();
}

void loop()
{
  Blynk.run();
}```

This is a blocking function, meaning that Blynk won’t be able to run and thus your program will most likely crash or become unresponsive. These should really be removed, or substituted for Blynk compatible code.

You can see some of the alternatives to using delay(); in other posts on the forum.

As to the main issue highlighted in your post, I’m not sure what your issue is. Could you possibly try putting it into different words?

This IS possible but it won’t work 100% accurately because if the power is removed mid way through stepping, the stepper motor still won’t know where it is exactly.

You could add a Blynk.virtualWrite(V99, PreviousSteps); after each stepper.step function:

    stepper.step(steps-PreviousStep); //steps = 0, not move
    PreviousStep=steps;
    Blynk.virtualWrite(V99, PreviousSteps);

Also add this function to update the PreviousSteps when Blynk.syncAll() is called:

BLYNK_WRITE(V99)
{
  PreviousSteps  = param.asInt();
}
2 Likes

oh yeah, because the stepper is not complete moved but its ok, oh ya so i just add Blynk.virtualWrite(V99,PreviousStep); after each stepper.step function