Hi all,
Please bear with me as this is my first post and I am new to forum. I haven been working on a project that uses an ESP8266 NodeMCU, CNC Shield, 2 A4988 drivers with 2 NEMA 17s. The goal is to be able to control X and Y motor directions with the joystick on the mobile dashboard. I was able to find an old set of codes for the Blynk Legacy app, but due to issues with the SimpleTimer library I was not able to compile on Arduino IDE. After spending some time with the forum I understand that Blynk does not like the “delay” commands as it stops the MCU from communication with the cloud server? I’ve looked at some FAQs, but none of them seem to easily integrate to my project. What would be the best way to go at fixing this sketch so it can control the 2 NEMA 17s? Thank you for your time.
Jeff
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "*****"
#define BLYNK_DEVICE_NAME "****"
#define BLYNK_AUTH_TOKEN "*****"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// 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[] = "*****";
// X Stepper Motor
int XstepPin = D1;
int XdirPin = D2;
int limitx = 200; // X stepper motor limit
int speedx = 500;
// Y Stepper Motor
int YstepPin = D3;
int YdirPin = D4;
int speedy = 500;
int Movey = 512; // value from blynk joystick "default value"
// Manual control X stepper motor
int vresistor = A0;
int vrdata = 0;
int pinValue1;
int pinValue2;
int pinValue3;
int pinValue4;
int firstVal, secondVal,thirdVal; // sensors
// This function sends Arduino's up time every second to Virtual Pin (1).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V1, millis() / 1000);
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}
void loop()
{
Blynk.run();
}
// From Blynk application to Nodemcu ESP8266
// in Blynk app writes values to the Virtual Pin V2
BLYNK_WRITE(V2) // X stepper Motor limits
{
pinValue1 = param.asInt(); // assigning incoming value from pin V2 to a variable
limitx = pinValue1;
}
BLYNK_WRITE(V3) // X stepper Motor Speed
{
pinValue2 = param.asInt();
speedx = pinValue2;
}
BLYNK_WRITE(V5) // Y Stepper motor control
{
pinValue3 = param.asInt();
Movey = pinValue3;
}
void StepperMotor()
{
if( digitalRead(D6) == 1 )
{
digitalWrite(XdirPin,HIGH); // Enables the motor to move in a particular direction
for(int x = 0; x < limitx; x++) {
digitalWrite(XstepPin,HIGH);
delayMicroseconds(speedx);
digitalWrite(XstepPin,LOW);
delayMicroseconds(speedx);
}
delay(500); // half second delay
digitalWrite(XdirPin,LOW); //Changes the rotations direction
for(int x = 0; x < limitx; x++) {
digitalWrite(XstepPin,HIGH);
delayMicroseconds(speedx);
digitalWrite(XstepPin,LOW);
delayMicroseconds(speedx);
}
delay(500);
}
}
void StepperMotorY()
{
if( digitalRead(D6) == 0 )
{
vrdata = analogRead(vresistor);
if ( vrdata < 100 )
{
digitalWrite(XdirPin,LOW);
digitalWrite(XstepPin,HIGH);
delayMicroseconds(speedx);
digitalWrite(XstepPin,LOW);
delayMicroseconds(speedx);
}
if ( vrdata > 900 )
{
digitalWrite(XdirPin,HIGH);
digitalWrite(XstepPin,HIGH);
delayMicroseconds(speedx);
digitalWrite(XstepPin,LOW);
delayMicroseconds(speedx);
}
if ( Movey > 512 )
{
digitalWrite(YdirPin,HIGH); // Enables the motor to move in a particular direction
digitalWrite(YstepPin,HIGH);
delayMicroseconds(speedy);
digitalWrite(YstepPin,LOW);
delayMicroseconds(speedy);
}
if ( Movey < 512 )
{
digitalWrite(YdirPin,LOW); // Enables the motor to move in a particular direction
digitalWrite(YstepPin,HIGH);
delayMicroseconds(speedy);
digitalWrite(YstepPin,LOW);
delayMicroseconds(speedy);
}
}
}