Hi,
I’m trying to control some roller blinds using a stepper motor and an ESP8266 (Wemos D1 Mini).
I have set up two virtual pins connected to two buttons in Blynk app, each triggering a different function to rotate the motor CW or CCW. I also have 2 HALL sensors that detect position and break the function execution. Everything works fine.
I now want to add a third virtual pin that will set a boolean variable and stop the function execution based on it’s value. However, when I press the V7 button in the application, it’s not detected until the function already completes it’s execution, making it useless.
I need help in modifying the code so that the controller detect the virtual pin value just like it does for the physical pins.
My code below:
// Arduino Program to control blinds using stepper motor
#define BLYNK_TEMPLATE_ID "asd" //The template ID can be find in Blynk web console
#define BLYNK_TEMPLATE_NAME "asd" // Device name can be found in Blynk Console
#define BLYNK_AUTH_TOKEN "asd" //Auth token can be found in Blynk web console
#define BLYNK_PRINT Serial
#include <FastLED.h>
#include <ESP8266WiFi.h> // Library for ESP8266 chip
#include <BlynkSimpleEsp8266.h> //Library for Blynk
#define BLYNK_PRINT Serial
#define LED_PIN D0
#define NUM_LEDS 1
CRGB leds[NUM_LEDS];
const int stepPin = D1;
const int dirPin = D2;
const int TMC2208enablePin = D3;
const int hall1Pin = D5;
const int hall2Pin = D6;
const int MS1Pin = D7;
const int MS2Pin = D8;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "asd";
char pass[] = "asd";
boleean stopbutton = false;
///////////////////////////////////////
// SETUP AREA //
/////////////////////////////////////
void setup()
{
Serial.begin(115200);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
FastLED.clear();
FastLED.show();
delay(100);
Serial.print("Connecting to "); // Waiting for network connection
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,pass);
while (WiFi.status() != WL_CONNECTED) //You will hang up in this loop until you connect...
{ delay(1000);
Serial.println(".");
FastLED.clear();
FastLED.show();
}
Serial.print("\r\nConnected: local ip address is http://"); // Now it's connected to network
Serial.println(WiFi.localIP());
Serial.println();
Blynk.config(auth); // For connecting to Blynk service
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(TMC2208enablePin,OUTPUT);
pinMode(hall1Pin,INPUT);
pinMode(hall2Pin,INPUT);
pinMode(MS1Pin,OUTPUT); //Used for Microstepping
pinMode(MS2Pin,OUTPUT); //Used for Microstepping
digitalWrite(TMC2208enablePin,HIGH); //D3 High disables the A4988 driver to prevent overheating
digitalWrite(MS1Pin,HIGH);
digitalWrite(MS2Pin,LOW);
leds[0] = CRGB(0, 0, 255);
FastLED.show();
}
//////////////////////////////////
// BLYNK FUNCTIONS //
////////////////////////////////
BLYNK_WRITE(V1) //OPEN the BLINDS
{
Serial.println("V1 pressed");
openblinds();
}
BLYNK_WRITE(V2) //CLOSE the BLINDS
{
Serial.println("V2 pressed");
closeblinds();
}
BLYNK_WRITE(V7)
{
Serial.println("V7 pressed");
stopbutton = true;
}
///////////////////////////
// LOOP //
/////////////////////////
void loop()
{
Blynk.run(); // We run blynk continously
if (WiFi.status() == WL_CONNECTED)
{
ledblue();
}
}
//////////////////////////////////
//FUNCTIONS //
////////////////////////////////
void openblinds() // This is called by Blynk pin V1 to OPEN the BLINDS
{
if (digitalRead(hall1Pin) == HIGH)
{
digitalWrite(MS1Pin,HIGH);
digitalWrite(MS2Pin,LOW);
leds[0] = CRGB(0, 255, 0 );
FastLED.show();
digitalWrite(TMC2208enablePin,LOW);
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
for(int x = 0; x < 8000; x++) {
digitalWrite(stepPin,HIGH);
delay(1);
digitalWrite(stepPin,LOW);
delay(1);
if (digitalRead(hall2Pin) == LOW) {
break;
FastLED.clear();
}
}
}
digitalWrite(TMC2208enablePin,HIGH); //We disable the stepper.
}
void closeblinds() // This is called by Blynk pin V2 to CLOSE the BLINDS
{
if (digitalRead(hall2Pin) == HIGH) {
digitalWrite(MS1Pin,HIGH);
digitalWrite(MS2Pin,LOW);
leds[0] = CRGB(255, 0, 0 );
FastLED.show();
digitalWrite(TMC2208enablePin,LOW);
digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
for(int x = 0; x < 8000; x++) {
digitalWrite(stepPin,HIGH);
delay(1);
digitalWrite(stepPin,LOW);
delay(1);
if (digitalRead(hall1Pin) == LOW) {
break;
FastLED.clear();
}
}
}
digitalWrite(TMC2208enablePin,HIGH); //We disable the stepper.
}
void ledblue()
{
leds[0] = CRGB(0, 0, 255 );
FastLED.show();
}