Disable / turn off button at the press of another button

Im currently trying to control a couple of servos via physical buttons and virtual buttons, the push button would act as a toggle switch and move one servo from part A and if I press it again it would move to B. I also added a virtual button so I could do the same thing with the same servo but from my phone. Im using a nodeMCU.

  buttonState1 = digitalRead(button1);
  if (directionState1 == 0) {

    if (buttonState1 == HIGH || pinValue1 == 1) {
      directionState1 = 1;

      digitalWrite(LED, HIGH);
      servo1.attach(D7);

      for (pos1 = lightOFF; pos1 <= lightON; pos1 += 1) {
        servo1.write(pos1);
        delay(15);
      }

      servo1.detach();
      digitalWrite(LED, LOW);
  
    }

 } else if (directionState1 == 1) {

   if (buttonState1 == HIGH || pinValue1 == 1) {
     directionState1 = 0;

     digitalWrite(LED, HIGH);
     servo1.attach(D7);

     for (pos1 = lightON; pos1 >= lightOFF; pos1 -= 1) {
       servo1.write(pos1);
       delay(15);
     }

     servo1.detach();
     digitalWrite(LED, LOW);
   }
}

This works but then I tried to add a sort of automation feature on my code where I add a photoresistor and on Blynk I add a slider a display value and a switch button. The idea is that when I press the automation button, if the value from the photoresistor is smaller than the value I added on the slider, my servo would move automatically from A to B. If I then decided to press either the virtual button or physical push button to move the servo manually, the automation button would turn off and not affect the movement of my servo. This is that code.

if (pinValue3 == 1) {

  if (photoresistor < sliderLuz) {

    if (servo1.read() == lightOFF) {
      directionState1 = 1;

      servo1.attach(D7);

      for (pos1 = lightOFF; pos1 <= lightON; pos1 += 1) {
        servo1.write(pos1);
        delay(15);
      }

      servo1.detach();

    }

    if (servo2.read() == lightOFF) {
      directionState2 = 1;

      servo2.attach(D8);

      for (pos2 = lightOFF; pos2 <= lightON; pos2 += 1) {
        servo2.write(pos2);
        delay(15);
      }

      servo2.detach();

    }
  }
}

This is the code to automate two servos and it works, the problem is now turning off the automate button when i decide to move the servo manual via the physical button or virtual one.

  if (buttonState1 == HIGH || pinValue1 == true) {
    Blynk.virtualWrite(V6,LOW);
  } else if (buttonState2 == HIGH || pinValue2 == true) {
    Blynk.virtualWrite(V6,LOW);
  }

So when I press the button either physical or virtual the automation switch button goes off but the problem is that. I dont understand exactly what is wrong with this code, from what I understand is that by just doing Blynk.virtualWrite(V6,LOW); the pinValue3 should equal to 0 and not let the code run right? But thats where im wrong, the code keeps running and the servos then keep moving no matter what I do until I reset the NodeMCU.

Im kinda new to Blynk so im not sure what im doing wrong and would really apreciate some help. Also im not a native english speaker so im sorry if my grammar is not that good. Thank you!

None of your code includes any virtual pin handling routines (there are no BLYNK_WEITE(vPin) callbacks), so I guess that when you say “virtual button” I assume that you mean widget button connected to a digital pin?

If so, then I’d strongly recommend that you start using virtual pins for your widgets.

BTW, posting snippets of code isn’t really very helpful,

Pete.

This is the code I made:

#include "Arduino.h"

#define BLYNK_PRINT Serial

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

char auth[] = "removed";

// Your WiFi credentials.
char ssid[] = "removed";
char pass[] = "removed";

#include <Servo.h>

Servo servo1; // LUZ 1
Servo servo2; // LUZ 2

int pos1 = 0;
int pos2 = 0;

#define button1 D1
#define button2 D2

#define LED D4

// Button State 1
int buttonState1 = 0;
int directionState1 = 0;

// Button State 2
int buttonState2 = 0;
int directionState2 = 0;

int lightON = 180;
int lightOFF = 90;

int pinValue1; // Virtual Button 1 // SERVO 1
int pinValue2; // Virtual Button 2 // SERVO 2
int pinValue3; // Automation button

BLYNK_WRITE(V1) // V1 is the number of Virtual Pin SERVO 1
{
  pinValue1 = param.asInt();
}

BLYNK_WRITE(V2) // V2 is the number of Virtual Pin SERVO 2
{
  pinValue2 = param.asInt();
}

BLYNK_WRITE(V6) // BOTON AUTOMATIZACIÓN LUZ
{
  pinValue3 = param.asInt();
}

float sliderLuz; // Slider variable
BLYNK_WRITE(V7) // Slider para luces
{
  sliderLuz = param.asFloat();
}

int photoresistor;
void lightSensor() {
  photoresistor = analogRead(A0);
  Blynk.virtualWrite(V5, photoresistor);
}

WidgetLED ledBlynk1(V3); // LED LUZ 1
WidgetLED ledBlynk2(V4); // LED LUZ 2

BlynkTimer timer;

void setup() {
  // Debug Console
  Serial.begin(9600);

  // LED FOR LIGHTS
  pinMode(LED, OUTPUT);

  // SETUP FIRST BUTTON
  pinMode(button1, INPUT);
  servo1.attach(D7);

  // SETUP SECOND BUTTON
  pinMode(button2, INPUT);
  servo2.attach(D8);

  // TIMER BLYNK
  timer.setInterval(500L, lightSensor);

  Blynk.begin(auth, ssid, pass);

}

void loop() {

  Blynk.run();
  timer.run();

 if (buttonState1 == HIGH || pinValue1 == true) {
    Blynk.virtualWrite(V6, LOW);
  } else if (buttonState2 == HIGH || pinValue2 == true) {
    Blynk.virtualWrite(V6, LOW);
  }

  // SERVO 1
  buttonState1 = digitalRead(button1);
  if (directionState1 == 0) {
    if (buttonState1 == HIGH || pinValue1 == 1) {
      directionState1 = 1;

      digitalWrite(LED, HIGH);
      servo1.attach(D7);

      for (pos1 = lightOFF; pos1 <= lightON; pos1 += 1) {
        servo1.write(pos1);
        delay(15);
      }

      servo1.detach();
      digitalWrite(LED, LOW);

    }

  } else if (directionState1 == 1) {
    if (buttonState1 == HIGH || pinValue1 == 1) {
      directionState1 = 0;

      digitalWrite(LED, HIGH);
      servo1.attach(D7);

      for (pos1 = lightON; pos1 >= lightOFF; pos1 -= 1) {
        servo1.write(pos1);
        delay(15);
      }

      servo1.detach();
      digitalWrite(LED, LOW);
    }
  }

  if (servo1.read() == lightON) { // ESTADO LED 1 EN BLYNK
    ledBlynk1.on();
  } else {
    ledBlynk1.off();
  }


  // SERVO 2 
  buttonState2 = digitalRead(button2);
  if (directionState2 == 0) {
    if (buttonState2 == HIGH || pinValue2 == 1) {
      directionState2 = 1;
      digitalWrite(LED, HIGH);
      servo2.attach(D8);

      Serial.println("Servo Luz 2 en movimiento");

      for (pos2 = lightOFF; pos2 <= lightON; pos2 += 1) {
        servo2.write(pos2);
        delay(15);
      }

      servo2.detach();
      digitalWrite(LED, LOW);
    }

  } else if (directionState2 == 1) {
    if (buttonState2 == HIGH || pinValue2 == 1) {
      directionState2 = 0;
      digitalWrite(LED, HIGH);
      servo2.attach(D8);

      Serial.println("Servo Luz 2 en movimiento");

      for (pos2 = lightON; pos2 >= lightOFF; pos2 -= 1) {
        servo2.write(pos2);
        delay(15);
      }

      servo2.detach();
      digitalWrite(LED, LOW);
    }
  }

  if (servo2.read() == lightON) { // ESTADO LED 2 EN BLYNK
    ledBlynk2.on();
  } else {
    ledBlynk2.off();
  }


  // AUTOMATION
  if (pinValue3 == 1) {

    if (photoresistor < sliderLuz) {

      if (servo1.read() == lightOFF) {
        directionState1 = 1;

        servo1.attach(D7);

        for (pos1 = lightOFF; pos1 <= lightON; pos1 += 1) {
          servo1.write(pos1);
          delay(15);
        }

        servo1.detach();

      }

      if (servo2.read() == lightOFF) {
        directionState2 = 1;

        servo2.attach(D8);

        for (pos2 = lightOFF; pos2 <= lightON; pos2 += 1) {
          servo2.write(pos2);
          delay(15);
        }

        servo2.detach();

      }
    }
  }

}

The problem is that even if the automation button was off but the photoresistor value is lower than my slider value it still turns the servos and I dont understand why.
This is how my widgets look.

Well, the first thing you need to do is to read this, and restructure your code accordingly…

I’d also suggest adding-in some serial print statements which show the position of the program flow within your sketch, and prints the key variable values.
This will kelp you to debug what is happening in various scenarios.

Pete.