My first Blynk sketch. Patience ready only :)

Looks like got some time now.
Here what I done so far -

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Stepper.h>

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

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

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

const int stepsPerRevolution = 500;
int state = 5;
Stepper rightStepper(stepsPerRevolution, 8,10,9,11);
Stepper leftStepper(stepsPerRevolution, 4,6,5,7);

void setup()
{
  // Debug console
  Serial.begin(9600);

  delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

   rightStepper.setSpeed(60);
  leftStepper.setSpeed(60);

  //move forward
  if (state == 1) {
    rightStepper.step(1);
    leftStepper.step(-1);    
  }
  //move backward
  if (state == 2) {
    rightStepper.step(-1);
    leftStepper.step(1);    
  }
  //move right
  if (state == 3) {
    rightStepper.step(1);
    leftStepper.step(1);    
  }
  //move left
  if (state == 4) {
    rightStepper.step(-1);
    leftStepper.step(-1);    
  }
  //do nothing
  if (state == 5) {
    rightStepper.step(0);
    leftStepper.step(0);
  }
}
BLYNK_WRITE(V0) {
  if (param.asInt() == 1) {
   state = 1; // forward
  } else {
    state = 5; // stop
  }
}
BLYNK_WRITE(V1) {
  if (param.asInt() == 1) {
   state = 2; // backward
  } else {
    state = 5; // stop
  }
}
BLYNK_WRITE(V3) {
  if (param.asInt() == 1) {
   state = 3; // right
  } else {
    state = 5; // stop
  }
}
BLYNK_WRITE(V2) {
  if (param.asInt() == 1) {
   state = 4; // forward
  } else {
    state = 5; // stop
  }
}

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

Looks like your “controls” are in setup(), that will not work as they are one time functions.

Either put them into each of the relevant BLYNK_WRITE() functions or create one function with all the controls and call the function from the BLYNK_WRITE()'s along with the state variable.

Like that? -
P.S. I added some terminal widget… But not getting Blynk version, as in setup() - why?

#define BLYNK_PRINT Serial

#include <Stepper.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

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

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

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

const int stepsPerRevolution = 2048;
Stepper rightStepper(stepsPerRevolution, 8,10,9,11);
Stepper leftStepper(stepsPerRevolution, 4,6,5,7);
int state = 5;

WidgetTerminal terminal(V5);

BLYNK_WRITE(V5)
{
  if (String("echo") == param.asStr()) {
    terminal.println("echo OK") ;
  } else {
    terminal.println("FAIL") ;}
  }

void setup()
{
  // Debug console
  Serial.begin(9600);

  delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  rightStepper.setSpeed(18);
  leftStepper.setSpeed(18);

  Blynk.begin(auth, wifi, ssid, pass);
  
terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
 
}
BLYNK_WRITE(V0) {
  if (param.asInt() == 1) {
   state = 1; // forward
  } else {
    state = 5; // stop
  }
 Control();
}
BLYNK_WRITE(V1) {
  if (param.asInt() == 1) {
   state = 2; // backward
  } else {
    state = 5; // stop
  }
  Control();
}
BLYNK_WRITE(V3) {
  if (param.asInt() == 1) {
   state = 3; // right
  } else {
    state = 5; // stop
  }
  Control();
}
BLYNK_WRITE(V2) {
  if (param.asInt() == 1) {
   state = 4; // left
  } else {
    state = 5; // stop
  }
  Control();
}

void Control() {
  //move forward
  if (state == 1) {
    rightStepper.step(1);
    leftStepper.step(-1);    
  }
  //move backward
  if (state == 2) {
    rightStepper.step(-1);
    leftStepper.step(1);    
  }
  //move right
  if (state == 3) {
    rightStepper.step(1);
    leftStepper.step(1);    
  }
  //move left
  if (state == 4) {
    rightStepper.step(-1);
    leftStepper.step(-1);    
  }
  //do nothing
  if (state == 5) {
    rightStepper.step(0);
    leftStepper.step(0);
  }
}
void loop()
{
  Blynk.run();
}

Now stepper motor driver changing states, but not rotating wheel… well, it rotating but about 100th part of whole wheel rotation to 180 degrees. Whats wrong there? I have uln2003 drivers and 28byj-48 motors.

You could put the commands in a for loop to step more than the current 100th.

Not really understand what you mean. Can you please post example?
You mean loop some string? How I can do that?
Much thanks.

if (state == 1) {
    for( int i = 0; i < 10; i++){
      rightStepper.step(1);
      leftStepper.step(-1);   
    } 
  }

Same process for each Control. I don’t know how long the current step takes but it will be trial and error with the value 10. Start low and then if you need a bigger step try 20, 40 etc.
If the step starts to take too long you might need a Blynk.run() right at the start of the function but try it without first.

There is no loop… Heard to many times :slight_smile:
Will try tomorrow. Thanks.

It’s working. But motors sometime acting strange. Think it’s because they cheap maybe. Anyway I have another question. When steps in progress how I can immediately interrupt it? It’s not listening any commands till it not finish current steps.

How long are the steps taking and what value are you using for i? The for loop is not ideal because has you have found it’s a blocking routine. Not a big problem if the step time is short but a major problem if each step is several seconds.

It’s 1000 in code.
But maybe driver will not allow to break current ordered steps what it doing.

Anyway thanks. Think project is done and I will need better steppers or DC motors for future use.

Last question - above I asked about terminal. Terminal in app suppose to show me version of Blynk and await “echo” input text. But it’s totally not works. It’s already in code above.

This should not compile as you are missing closing brace, }, from the BLYNK_WRITE(V5) function.

It is compiling. I took terminal code from blynk examples. It not saying about brace anything.

You are missing the flush.

You right…
Thought it will erase everything in second after…
Why I thought so? :smiley:

BLYNK_WRITE(V0) {
  if (param.asInt() == 1) {
   state = 1; // forward
  } else {
    state = 5; // stop
  }
 Control();
}
BLYNK_WRITE(V1) {
  if (param.asInt() == 1) {
   state = 2; // backward
  } else {
    state = 5; // stop
  }
  Control();
}
BLYNK_WRITE(V3) {
  if (param.asInt() == 1) {
   state = 3; // right
  } else {
    state = 5; // stop
  }
  Control();
}
BLYNK_WRITE(V2) {
  if (param.asInt() == 1) {
   state = 4; // left
  } else {
    state = 5; // stop
  }
  Control();
}

void Control() {
  //move forward
  if (state == 1) {
    rightStepper.step(1);
    leftStepper.step(-1);    
  }
  //move backward
  if (state == 2) {
    rightStepper.step(-1);
    leftStepper.step(1);    
  }
  //move right
  if (state == 3) {
    rightStepper.step(1);
    leftStepper.step(1);    
  }
  //move left
  if (state == 4) {
    rightStepper.step(-1);
    leftStepper.step(-1);    
  }
  //do nothing
  if (state == 5) {
    rightStepper.step(0);
    leftStepper.step(0);
  }
}

You code ‘works’ but its messy.

Try this:

BLYNK_WRITE(V0) {
  param.asInt() ? Control(1) : Control(5); 
}
BLYNK_WRITE(V1) {
  param.asInt() ? Control(2) : Control(5); 
}
BLYNK_WRITE(V3) {
  param.asInt() ? Control(3) : Control(5); 
}
BLYNK_WRITE(V2) {
  param.asInt() ? Control(4) : Control(5); 
}

void Control(int state) {
  switch(state){
    case 1:
      //move forward
      rightStepper.step(1);
      leftStepper.step(-1);   
    break;
    case 2:
      //move backward
      rightStepper.step(-1);
      leftStepper.step(1);     
    break;
    case 3:
      //move right
      rightStepper.step(1);
      leftStepper.step(1);    
    break;
    case 4:
      //move left
      rightStepper.step(-1);
      leftStepper.step(-1);     
    break;
    case 5:
      //stop
      rightStepper.step(0);
      leftStepper.step(0); 
    break;
  }
}
2 Likes

Will do.

It is working, but it’s again doing 1 step in one button press. If I will fast press button then it a bit super slow doing some move, like before :slightly_frowning_face:

But wait a second! You just changed old one. Pardon me. I’m was a bit inattentive.