Cookins
October 14, 2017, 11:21am
22
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();
}
Costas
October 14, 2017, 11:39am
23
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.
Cookins
October 14, 2017, 12:15pm
24
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();
}
Cookins
October 14, 2017, 12:41pm
25
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.
Costas
October 14, 2017, 5:12pm
26
You could put the commands in a for loop to step more than the current 100th.
Cookins
October 14, 2017, 7:26pm
27
Not really understand what you mean. Can you please post example?
You mean loop some string? How I can do that?
Much thanks.
Costas
October 14, 2017, 7:38pm
28
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.
Cookins
October 14, 2017, 8:03pm
29
There is no loop⌠Heard to many times
Will try tomorrow. Thanks.
Cookins
October 15, 2017, 9:49am
30
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.
Costas
October 15, 2017, 9:57am
31
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.
Cookins
October 15, 2017, 12:26pm
32
Itâs 1000 in code.
But maybe driver will not allow to break current ordered steps what it doing.
Cookins
October 15, 2017, 12:29pm
33
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.
Costas
October 15, 2017, 2:41pm
34
This should not compile as you are missing closing brace, }, from the BLYNK_WRITE(V5) function.
Cookins
October 15, 2017, 7:04pm
35
It is compiling. I took terminal code from blynk examples. It not saying about brace anything.
Costas
October 15, 2017, 7:13pm
36
You are missing the flush.
Cookins
October 16, 2017, 8:59am
37
You rightâŚ
Thought it will erase everything in second afterâŚ
Why I thought so?
Jamin
October 16, 2017, 9:08am
38
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
Cookins
October 16, 2017, 3:41pm
40
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
But wait a second! You just changed old one. Pardon me. Iâm was a bit inattentive.