• Arduino Mega 2560 + ESP8266
• Android
• v1.0.0-beta.3
Hello, and thank you in advance for whoever helps. I am a novice so please excuse improper structure and errors in my code. Any tips and whatnot are always appreciated.
So, my program currently will run either a basic motor function with set duration, speed, and interval if one button is pressed (V1). The other function will run with a selected speed read from a slider widget (V3) on a button press (V5). Both these functions need to loop infinitely, and so far from my testing I believe this code achieves this. However, I am sure there is a better way.
The problem that I am currently facing is that when I do press my stop buttons (V2 and V6), the functions will stop only when they reach the delay(5000) at the end of the function, and in some cases it does not stop at all if I press the buttons only once.
//Include the Arduino Stepper Library
#include <Stepper.h>
//Include the Blynk Library
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
//Blynk Initialization
char auth[] =
char ssid[] =
char pass[] =
#define EspSerial Serial3
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
//Blynk Variables
int switchStatus1; //start button for default function
int latch1; //latch for default function loop
int switchStatus2; //start button for custom function
int latch2; //latch for custom function loop
int speedInput; //variable for custom function speed
int rotationInput; //variable for custom function # of rotations
//Motor Variables
// Number of steps per internal motor revolution
const float STEPS_PER_REV = 32;
// Number of Steps Required
int StepsRequired;
// The pins used are 8,9,10,11
// Connected to ULN2003 Motor Driver In1, In2, In3, In4
// Pins entered in sequence 1-3-2-4 for proper step sequencing
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);
void setup()
{
Serial.begin(115200);
delay(10);
EspSerial.begin(ESP8266_BAUD); //serial initialization and default baud rate for ESP module
delay(10);
Blynk.begin(auth, wifi, ssid, pass); //Reguler server
//Blynk.begin(auth, wifi, ssid, pass,"blynk-cloud.com", 8080); //Local server
//Blynk.begin(auth, ssid, pass, IPAddress(45,55,96,146), 8080);
}
//Blynk Functions
//Default Function
// write data from blynk on virtual 1 (start default function)
BLYNK_WRITE(V1) {
switchStatus1 = param.asInt();
latch1 = true;
if(latch1 == true){ //if latch is set loop function
defaultRun();
if(latch1 == false){
return;
}
}
}
// write data from blynk on virtual 2 (stop functions)
BLYNK_WRITE(V2) {
if (param.asInt());
latch1 = false; //unlatch
}
//Custom Function
// write data from blynk on virtual 3 (speed input)
BLYNK_WRITE(V3) {
speedInput = param.asInt();
}
/*// write data from blynk on virtual 4 (rotation input)
BLYNK_WRITE(V4) {
rotationInput = param.asInt();
}*/
// write data from blynk on virtual 6 (start custom function)
BLYNK_WRITE(V5) {
switchStatus2 = param.asInt();
latch2 = true;
if(latch2==true){ //if latch is set loop function
customRun();
if(latch2 == false){
return;
}
}
}
// write data from blynk on virtual 2 (stop functions)
BLYNK_WRITE(V6) {
if (param.asInt());
latch2 = false; //unlatch
}
void loop()
{
Blynk.run(); //begin blynk
}
void defaultRun(){
steppermotor.setSpeed(1000);
StepsRequired = 2050;
steppermotor.step(StepsRequired);
delay(2000);
steppermotor.setSpeed(1000);
StepsRequired = - 2050;
steppermotor.step(StepsRequired);
// Wait before rotation again
delay(5000);
}
void customRun(){
steppermotor.setSpeed(speedInput);
StepsRequired = 2050;
steppermotor.step(StepsRequired);
delay(2000);
steppermotor.setSpeed(speedInput);
StepsRequired = - 2050;
steppermotor.step(StepsRequired);
// Wait before rotation again
delay(5000);
}