Infinite Loop error on virtual pin

You hello, first I apologize for my language.

Device: WEMOS D1 R2
OS: Windows and Mac OS

I created a program that has failed the electronic board that controls the garage door.
With the function “mandoDistancia” and an RF remote I open and close the door smoothly, the code works fine.
The problem I have in Blynk, specifically with BLYNK_WRITE (V7) function, on the phone I created a button in v7, pressing push mode and enters a loop opening and closing the door constantly.

Thank you for your help.

void mandoDistancia () {
  mandoSW = digitalRead(Mando); // Lee estado mando
      if ((mandoSW == LOW) && (puertaabierta == 0)) {
        terminal.println("Open Garage Door");
        openDoor();
      }
      mandoSW = digitalRead(Mando);
      if ((mandoSW == LOW) && (puertaabierta == 1)) {
        terminal.println("Close Garage Door");
        closeDoor();
      }
}```

`    // ========{ RF remote control}==================
void mandoDistancia () {
  mandoSW = digitalRead(Mando); // Lee estado mando
      if ((mandoSW == LOW) && (puertaabierta == 0)) {
        terminal.println("Open Garage Door");
        openDoor();
      }
      mandoSW = digitalRead(Mando);
      if ((mandoSW == LOW) && (puertaabierta == 1)) {
        terminal.println("Close Garage Door");
        closeDoor();
      }
}`

Hello. Button sends 1 on press and 0 on release. You need to use Button with Switch mode. http://docs.blynk.cc/#widgets-controllers-button

1 Like

Can we see the code you have in V7?

Do you need to read the “Mando” pin twice in the void mandoDistancia() function?

Would an if, else statement work i.e. if puertaabierta !=0 then surely it is == 1.

Oh, sorry, I put twice the same function. Publish the full program.

Yes, you must read it twice, since it is a push, if not read then close the door. You could also use an “else” but for safety I preferred not to do so, the engine has a force of 3.5 tons.

Thanks again, a help to solve my mistake will be well received

```include <ESP8266WiFi.h>
    include <BlynkSimpleEsp8266.h>
    include <Wire.h>
    include <elapsedMillis.h>
    elapsedMillis timeElapsed;// timeElapsed permite el bucle sin paralizar el funcionamiento, al contrario de delay
    unsigned int interval = (1000 * 6) * 5;  // 5 MINUTOS  tiempo de espera

    WidgetLED led1(V1); //led rojo si puerta abierta
    WidgetLED led2(V2); //led verde si puerta cerrada
    WidgetTerminal terminal(V3);

    bool puertaabierta = 0; //0 cerrada; 1 abierta
    bool mandoSW; // Estado de lectura del pin mando distancia
    int inPinVal ; // Virtual Pin Input
    bool conectado = false;

    //CONSTANTES
    const char auth[] = "xxxxxxx";
    const char* ssid = "xxxxxxx";
    const char* pass = "xxxxxxx";
    define FCC D1
    define FCA D2
    define Infra D5
    define Mando D3
    define MotorA D6
    define MotorC D7

    void setup() {
      Serial.begin(115200);
      Blynk.begin(auth, ssid, pass); //conexión
      delay(100);
      // Intentará conectarse 3 veces al servidor blynk
      int con = 0;
       do {
      conectado = Blynk.connect();
      con = con+1;
       } while (!conectado && con <=2);

      pinMode(FCC, INPUT);   // CLOSE FCC Final Carrera Puerta Cerrada //Sensor Magnético
      pinMode(FCA, INPUT);   // OPEN FCA Final Carrera Puerta Abierta //Sensor Magnético
      pinMode(Infra, INPUT);   // Sensor barrera infrarojos
      pinMode(MotorA, OUTPUT);  // Pin conectado a relé para giro motor apertura
      pinMode(MotorC, OUTPUT);  // Pin conectado a relé para giro motor cierre
      pinMode(Mando, INPUT);   // Mando distancia

      digitalWrite(FCC, LOW);
      digitalWrite(FCA, LOW);
      digitalWrite(Infra, LOW);
      digitalWrite(MotorA, HIGH);
      digitalWrite(MotorC, HIGH);
      digitalWrite(Mando, LOW);

      terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
      terminal.flush();  // y limpia caché
      Serial.println(F("Blynk v" BLYNK_VERSION ": Device started"));
      
    }


    // =================={ Virtual LEDs }=========
    void checkLeds() {
      if (puertaabierta == 0) // 
      {
        led1.off();
        led2.on();
      } else {
        led1.on();
        led2.off();
      }
    }

    // ========{ Sensor InfraRojos }==================
    void sensorIR (){

    }

    // ========{ Abre Puerta  }==================
    void abrePuerta () {
      bool fcaSW = digitalRead(FCA); // FCA Final carrera puerta abierta
        terminal.println("Abriendo puerta");
          do {
          digitalWrite(MotorA, LOW); // MOTOR ABRE
          delay(100);
          fcaSW = digitalRead(FCA);//actualiza el final de carrera
          } while ( fcaSW == LOW );
        digitalWrite(MotorA, HIGH); // STOP MOTOR
        terminal.println("Puerta abierta fca");
        puertaabierta = 1;
        delay(300);
    }
    // ========{ Cierra Puerta }==================
    void cierraPuerta () {
      bool fccSW = digitalRead(FCC); // FCC Final carrera puerta cerrada
        terminal.println("Cerrando puerta");
          do {
          digitalWrite(MotorC, LOW); // MOTOR CIERRA
          delay(100);
          fccSW = digitalRead(FCC); //actualiza el final de carrera
          } while ( fccSW == LOW );
        digitalWrite(MotorC, HIGH); // STOP MOTOR
        terminal.println("Puerta cerrada fcc");
        puertaabierta = 0;
        delay(300);
    }
    // ---------[ Virtual pin # 7 ]--------------------
    BLYNK_WRITE(V7) { //SIMULA EL BOTÓN DEL MANDO
          if  ((param.asInt() == 1) && (puertaabierta == 0)) {
            terminal.println("Telefono abre");
            abrePuerta();
          }
          if ((param.asInt() == 1) && (puertaabierta == 1)) {
              terminal.println("Telefono cierra");
              cierraPuerta();
          }
    }
    // ========{ Mando distancia }==================
    void mandoDistancia () {
      mandoSW = digitalRead(Mando); // Lee estado mando
      if ((mandoSW == LOW) && (puertaabierta == 0)) {
        terminal.println("Mando coche abre");
        abrePuerta();
      }
      mandoSW = digitalRead(Mando);
      if ((mandoSW == LOW) && (puertaabierta == 1)) {
        terminal.println("Mando coche cierra");
        cierraPuerta();
      }
    }
    // ==============( Void Loop  ) ====================================
    void loop(){
      if (conectado) {Blynk.run();}

      unsigned long currentMillis = millis();// Empezar a contar milisegundos
      if (puertaabierta == 1){
          if (timeElapsed > interval){
          Blynk.email("xxxxxxx@xxxxx", "GARAJE ABIERTO!", "La puerta de garaje está abierta.");
          timeElapsed = 0;  
          }
      }
      mandoDistancia();
      checkLeds();
    }```

edit last post, highlight code and click </> button so we can read it.

Still missing your include statements.

loop is all wrong, use SimpleTimer and call functions, even the send email every 5 minutes if door is open, at intervals

I do not understand the function of the RF remote control has loop and works perfectly. v7 function has no loop is not working properly

Did you change the button to Switch mode as recommended by @Dmitriy?

I have made the changes that @Dmitriy and the loop continues.
I still do not know where the fault is.

@actio post your current code and we will try to find the bugs.

Is unsigned long currentMillis = millis();// Empezar a contar milisegundos in loop() redundant as I can’t see where currentMillis is used in your sketch.

The recommendation is to use SimpleTimer as it has many more features than the basic elapsedMillis library. In the old “Arduino days” when memory came at a premium small libraries were important but they are much less important now to have 1MB of program memory to play with on the ESP’s.