I wanted to control 2 Stepper Motors for running a robot using the joystick of Blynk App and NodeMCU. But when I searched for the codes of real time controlling of Stepper Motors online I didn’t get much code and most of them were not real time.
This is code I am currently working on:-
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define RightMotorSpeed D7
#define RightMotorDir D8
const int enPin = D2;
const int enPin2 = D3;
#define LeftMotorSpeed D6
#define LeftMotorDir D5
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
// Use your own WiFi settings
char auth[] = "LRTCZUnCI06P-pqh5rlPXRbuOUgQ_uGH";
char ssid[] = "Airtel_7599998800";
char pass[] = "air71454";
// neutral zone settings for x and y
// joystick must move outside these boundary numbers to activate the motors
// makes it a little easier to control the wifi car
int minRange = 312;
int maxRange = 712;
// analog speeds from 0 (lowest) - 1023 (highest)
// 3 speeds used -- 0 (noSpeed), 350 (minSpeed), 850 (maxSpeed).
// use whatever speeds you want...too fast made it a pain in the ass to control
int minSpeed = 450;
int maxSpeed = 1023;
int noSpeed = 0;
void moveControl(int x, int y)
{
// movement logic
// move forward
// y je vetsi jak maxrange a současně x je vetsi jak minRange a současne mensi jak max range
while(y >= maxRange && x >= minRange && x <= maxRange) //zataci R
{
digitalWrite(RightMotorDir,HIGH);
digitalWrite(LeftMotorDir,HIGH);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
delayMicroseconds(500);
digitalWrite(RightMotorSpeed,0);
digitalWrite(LeftMotorSpeed,0);
delayMicroseconds(500);
}
// move forward right
while(x >= maxRange && y >= maxRange) //zataci R
{
digitalWrite(RightMotorDir,HIGH);
digitalWrite(LeftMotorDir,HIGH);
analogWrite(RightMotorSpeed,minSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
}
// move forward left
while(x <= minRange && y >= maxRange)
{
digitalWrite(RightMotorDir,HIGH);
digitalWrite(LeftMotorDir,HIGH);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,minSpeed);
}
// neutral zone
while(y < maxRange && y > minRange && x < maxRange && x > minRange)
{
analogWrite(RightMotorSpeed,noSpeed);
analogWrite(LeftMotorSpeed,noSpeed);
}
// move back
while(y <= minRange && x >= minRange && x <= maxRange)
{
digitalWrite(RightMotorDir,LOW);
digitalWrite(LeftMotorDir,LOW);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
}
// move back and right
while(y <= minRange && x <= minRange)
{
digitalWrite(RightMotorDir,LOW);
digitalWrite(LeftMotorDir,LOW);
analogWrite(RightMotorSpeed,minSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
}
// move back and left
while(y <= minRange && x >= maxRange)
{
digitalWrite(RightMotorDir,LOW);
digitalWrite(LeftMotorDir,LOW);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,minSpeed);
}
}
void setup()
{
// initial settings for motors off and direction forward
pinMode(RightMotorSpeed, OUTPUT);
pinMode(LeftMotorSpeed, OUTPUT);
pinMode(RightMotorDir, OUTPUT);
pinMode(LeftMotorDir, OUTPUT);
digitalWrite(RightMotorSpeed, LOW);
digitalWrite(LeftMotorSpeed, LOW);
digitalWrite(RightMotorDir, HIGH);
digitalWrite(LeftMotorDir,HIGH);
Serial.begin(9600);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
pinMode(enPin2,OUTPUT);
digitalWrite(enPin2,LOW);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}
BLYNK_WRITE(V1)
{
int x = param[0].asInt();
int y = param[1].asInt();
moveControl(x,y);
}
Here I have defined by 2 Stepper motors as Right and Left and since I am using TB6600 Motor Driver therefore their Pulse and Direction Pins are also Defined. That is the main reason that I am unable to use the Stepper motor Library for the code.
Running the code I see that Both the motors runs fine once for 3 to 5 seconds and the the Blynk Server Disconnects and Reconnects again causing the motors to stop and not creating a real time communication. Some one Please help me create a code for these 2 stepper motors to run at realtime.
I think that Blink.run() causes the server to reconnect and stop the motor.
I also searched for this cause and found that instead of Stepper motor Library I should use AccelStepper Library but that is also not achieved . Please help me with this. Any correct reference is also appreciable. Thanks in advance.