I’m trying to port this code to Blynk where the goal is to run the stepper motor when Button(V1) is pressed:
#include <AccelStepper.h>
#define HALFSTEP 8
// Motor pin definitions
#define motorPin1 D5 // IN1 on the ULN2003 driver 1
#define motorPin2 D6 // IN2 on the ULN2003 driver 1
#define motorPin3 D7 // IN3 on the ULN2003 driver 1
#define motorPin4 D8 // IN4 on the ULN2003 driver 1
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
void setup() {
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(100.0);
stepper1.setSpeed(200);
stepper1.moveTo(20000);
}//--(end setup )---
void loop() {
//Change direction when the stepper reaches the target position
if (stepper1.distanceToGo() == 0) {
stepper1.moveTo(-stepper1.currentPosition());
}
stepper1.run();
}
Here’s my Blynk code:
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <AccelStepper.h>
#define HALFSTEP 8
// Motor pin definitions
#define motorPin1 D5 // IN1 on the ULN2003 driver 1
#define motorPin2 D6 // IN2 on the ULN2003 driver 1
#define motorPin3 D7 // IN3 on the ULN2003 driver 1
#define motorPin4 D8 // IN4 on the ULN2003 driver 1
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
// 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[] = "********";
void setup() {
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(100.0);
stepper1.setSpeed(200);
stepper1.moveTo(-999999);
}
void loop() {
Blynk.run();
}
BLYNK_WRITE(1)
{
stepper1.run();
}
So far, when the button is pressed on or off, the motor is moving one step at a time. So, for 200,000 steps, I’d be pressing to 200,000 times. What’s the right way to do this instead of having to manually press the button 200,000 times?
EDIT:
After some more investigation, BLYNK_WRITE(1) is invoked every time the blynk button is pressed ON or OFF. This calls the “stepper1.runSpeed()” which returns a boolean vale “true”. This causes the motor to go 1 step. Still not sure how I can keep it moving continuously when the blynk button is pressed.