Push button and Virtual Button (not switch)

Hello All,

I am working with a Adafruit Feather huzzah.
Set-up
Servo Pin 5
Puss button pin 8
Virtual Pin 2

My project consist on controlling a stepper motor with a virtual pin in push button mode. So far, I have this step already completed, the stepper reach 90 degrees and goes back to initial position.

My problem is that I need to connect a physical push button doing the same procedure, I been reading examples but all of them are related to the sync of the buttons working in switch mode for example to turn on a light with the physical button and turn off with the virtual button, that is not my case.


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


Servo servo1;
#define SERVO1 5

char auth[] = "*************";

char ssid[] = "*******************";
char pass[] = "***********************";

// Set your LED and physical button pins here
const int btnPin = 8;

BlynkTimer timer;
void checkPhysicalButton();

int btnState = HIGH;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V2);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
  int pinData = param.asInt();

  Blynk.email("***********************", "Cat Feeder", "Hey, your pet feeder here! I just fed your cat!");

  servo1.write(0);
  delay(150);
  servo1.write(90);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

  //Blynk.notify("Hey! I just fed your cat!");
  Blynk.email("heyleen70@gmail.com", "Cat Feeder", "Hey, your pet feeder here! I just fed your cat!");


  servo1.write(0);
  delay(150);
  servo1.write(90);

      // Update Button Widget
      Blynk.virtualWrite(V2);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

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", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);


  pinMode(btnPin, INPUT_PULLUP);
  Blynk.email("************", "Cat Feeder", "Hey, your pet feeder here! I just fed your cat!");


  servo1.write(0);
  delay(150);
  servo1.write(90);

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);
}

void loop()
{
  Blynk.run();
  timer.run();
}

@HeyAndre: Some initial changes I’d make:

  • delete Blynk.virtualWrite(V2); from checkPhysicalButton()
  • change timer.setInterval(100L, checkPhysicalButton); to timer.setInterval(300L, checkPhysicalButton);

In general delay() used with Blynk is not a good idea (see documentation). But, let’s leave that for later.

There is another sketch that I been trying but the buttons does not reflect any action

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>

Servo servo1;
#define SERVO1 5


char auth[] = "5e24d93aa5be4bc1ab55e1b847c65100";

char ssid[] = "****************";
char pass[] = "**************";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  servo1.attach(SERVO1);
  pinMode(0, INPUT_PULLUP);
}

void loop()
{

  Blynk.run();
}

BLYNK_WRITE(V1) { //Button Widget is writing to pin V1

  int pinData = param.asInt();

  //Blynk.notify("Hey! I just fed your cat!");
  Blynk.email("**********************", "Cat Feeder", "Hey, your pet feeder here! I just fed your cat!");


  servo1.write(0);
  delay(150);
  servo1.write(90);

  //read the pushbutton value into a variable
  int sensorVal = digitalRead(8);
  //print out the value of the pushbutton
  Serial.println(sensorVal);
  
  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the 
  // button's pressed, and off when it's not:
  if (sensorVal == LOW) {                             
    servo1.write(0);               
    delay(900);  
    servo1.write(90);                                               
}
}

Please visit docs.blynk.cc and swarch for “state sync”