Blynk app with ESP8266 with Stepper not looping

I have a combination of code via different examples. I was getting the smoothest operation of my stepper using an ir receiver and wanted to translate into a esp8266. I have been able to get it to run, but once I click up or down a single time in the Blynk app the ESP8266 "shuts off" I think I am missing something in the loop command.

Below is the code:

#include <Stepper.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// const int stepsPerRevolution = 32;  // change this to fit the number of steps per revolution

#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID "TMPLt-83w9m6"
#define BLYNK_TEMPLATE_NAME "RollerBlindControl2"
#define BLYNK_AUTH_TOKEN "************************"

#define STEPS  32   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution

//Stepper myStepper(stepsPerRevolution, D8, D4, D6, D5);
Stepper small_stepper(STEPS, D8, D4, D6, D5);


bool Left;// = false;
bool Right;// = false;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*******";
char pass[] = "**************************";


//++ Start Setup +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void setup(){

  Serial.begin(9600);                                 // baudrate for serial comunication
  Blynk.begin(auth, ssid, pass);                      // network information 
  // myStepper.setSpeed(70);                            // Speed for stepper motor
  
}
//-- Setup End ---------------------------------------------------------------------------------------------------------------------


BLYNK_WRITE(V2){                                      // read input from virtual pin V1
  Left = param.asInt();                              // assigning incoming value from pin V§1 to a variable
  }
  
BLYNK_WRITE(V3){                                      // read input from virtual pin V0
  Right = param.asInt();                               // assigning incoming value from pin V0 to a variable
  }
  
//void Stepper1 (int Direction, int Rotation){          // function for stepper motor control with 2 parameters
 // for (int i = 0; i < Rotation; i++){                 // for loop 
  //my_Stepper.step(Direction * 200);                   // 200 is 360 degree => change value if smaller then 360 degree is needing
  //Blynk.run();
  //}
//}

//++ Start Loop +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void loop() {   
  
  Blynk.run();
  
  if (Left){                                          // if condition 
 // Stepper1(1, 10);                                     // steppermotor rotates 10 times 360 degree right
  //Serial.println("Right Turn");
  Serial.println("Right Turn");
  // myStepper.step(stepsPerRevolution);
  small_stepper.setSpeed(1000); //Max seems to be 500
                      Steps2Take  =  -2048;  // Rotate CW
                      small_stepper.step(Steps2Take);
                      //Left();
  }
  delay(20);                                           // delay 20ms
  
  if (Right){                                           // if condition 
  //Stepper1(-1, 10);                                    // steppermotor rotates 10 times 360 degree left
  //Serial.println("Left Turn");
  Serial.println("Left Turn");
  //myStepper.step(-stepsPerRevolution);
  small_stepper.setSpeed(1000);
                      Steps2Take  =  2048;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      //Right();
  }
  delay(20);                                           // delay 20ms
}


//-- Loop End ---------------------------------------------------------------------------------------------------------------------

Your board keeps resetting, right? With an ESP8266 you are going to have to worry about running into the wifi timer watchdog. Your code won't be able to tie up the CPU for very long before the wifi radio will want to do something. Because of this you have to avoid any code that blocks the CPU for any length of time. Because of this, you want to avoid any "delay"s and you will not be able to take 2048 steps in a row without some type of break in the code.

Take a look at this example that uses the accelstepper library but it is very similar.

I'm also curious about your setting for speed and steps per revolution. What kind of stepper are you using? A 28BYJ-48? If so, your steps per revolution will only be 32 if the stepper is completely ungeared. Most likely your stepper is geared 64:1 so you will get 2048 steps per revolution. With a ULN2003 you are not going to get 1000 RPMs. You're going to max out at around 20. Using the numbers you have might seem to work but you'll get much better results and be able to make adjustments to the code much easier if you use more accurate numbers. Trust me, I've been there.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.