After much research and searching both here at BC and abroad, I have been able to accomplish my goal of running a stepper motor using Blynk - yay!
My next objective (and issue) is rather simple - to be able to click a Blynk button (assigned to V1 in this case) to enable the stepper motor to rotate CW 90 degrees and stop. Then, through the use of another button (assigned V2), enable the motor to rotate CCW 90 degrees. I have been able to code in these operations and they are functional, but only allow this action to occur once per direction (aka per button).
With the current code below, when I push the (V1) Button in Blynk to “ON”, the stepper motor rotates CW 90 degrees and stops, and the Blynk app Button is switched back to “OFF” (via “Blynk.virtualWrite”). However, the Blynk button is not able to switch to “ON” after this first rotation has been executed, almost like I am locked out of using the button to repeat the motor action. Likewise, the identical situation occurs with the (V2) Button that is for CCW (makes sense, as the code for CCW is the same structure).
I have a feeling it is something wrong with the FlagState, but not sure as I am not too savvy yet with coding. Any help or guidance would be greatly appreciated! Let me know if there are any questions or additional info that would help or clarify. I did put comments to help explain what I believe is going on.
EDIT: rearranged the functions so they are more organized…
Hardware: NodeMCU + WiFi (via built in ESP)
Below is my code in its entirety:
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
#include <AccelStepper.h>
#define HALFSTEP 8
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxx";
char pass[] = "xxxxxxxx";
// Motor pin definitions
#define motorPin1 14 // IN1 on the ULN2003 driver 1
#define motorPin2 12 // IN2 on the ULN2003 driver 1
#define motorPin3 13 // IN3 on the ULN2003 driver 1
#define motorPin4 15 // IN4 on the ULN2003 driver 1
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepperCW(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepperCCW(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
// Global variables
int CWFlagState = 0; // used to lock out subsequent button presses
int CCWFlagState = 0; // used to lock out subsequent button presses
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
// *** ClockWise motor parameters used by AccelStepper ***
stepperCW.setMaxSpeed(5000); // Maximum speed setting
stepperCW.setAcceleration(100); // Acceleration setting
stepperCW.setSpeed(5000); // Normal speed setting
stepperCW.move(1024); // value in steps (+/-); 4096 steps is 360 degrees
// *** ClockWise motor parameters used by AccelStepper ***
stepperCCW.setMaxSpeed(5000); // Maximum speed setting
stepperCCW.setAcceleration(100); // Acceleration setting
stepperCCW.setSpeed(5000); // Normal speed setting
stepperCCW.move(-1024); // value in steps (+/-); 4096 steps is 360 degrees
}
BLYNK_WRITE(V1) {
int pinValue = param.asInt();
if(pinValue == 1 && CWFlagState == 0) { // if V1 button is pushed to "ON" & CWFlagState=0...
CWFlagState = 1; // sets CWFlagState=1 - lock out subsequent button pushes...
timer.setTimer(1L, runStepperCW, 20000); // ...and then rotate Stepper motor CW
}
}
void runStepperCW() { // This function runs when V1=1 and CWFlagState=0
stepperCW.run(); // Runs the AccelStepper for Clockwise rotation
if(stepperCW.distanceToGo() == 0) { // If Stepper reaches its target...
stepperCW.stop(); // Stepper is set to stop
CWFlagState = 0; // Set the CWFlagState to 0
Blynk.virtualWrite(V1, 0); // Sets V1 to 0 (resets the button to "OFF")
}
}
BLYNK_WRITE(V2) {
int pinValue = param.asInt();
if(pinValue == 1 && CWFlagState == 0) { // if V2 button is pushed to "ON" & CCWFlagState=0...
CCWFlagState = 1; // sets CCWFlagState=1 - lock out subsequent button pushes...
timer.setTimer(1L, runStepperCCW, 20000); // ...and then rotate Stepper motor CCW
}
}
void runStepperCCW() { // This function runs when V2=1 and CCWFlagState=0
stepperCCW.run(); // Runs the AccelStepper for CounterClockwise rotation
if(stepperCCW.distanceToGo() == 0) { // If Stepper reaches its target...
stepperCCW.stop(); // Stepper is set to stop
CCWFlagState = 0; // Set the CCWFlagState to 0
Blynk.virtualWrite(V2, 0); // Sets V2 to 0 (resets the button to "OFF")
}
}
void loop() {
Blynk.run();
timer.run();
}