Motors working without activating them

Hey guys, i advanced a little bit on my project. I connected 4 servo 360, hr-sr501 sensor and a LED. they all work good from blynk, but my problem now is that some of them work without me activating them. if i run servo 1 another one or two will have a small interference and move a little bit. how can i cancel that?
and also delay commend is pausing all other actions! i understand that there is a way to do it better than delay command, i found this online:

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

// constants won't change. Used here to set a pin number :
const int ledPin =  19;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 2000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

I didn’t understand how i use this code, can someone please explain me this?

And this is my code:

#define BLYNK_PRINT Serial
#include <Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>


char auth[] = "afALTS4s5s_lu3Z_yNeimE4wcAdpJ3o0";
char ssid[] = "Dvir";
char pass[] = "dvir5558";

int Sens =2;
int LED =4;
 
Servo FoodServo;
Servo TreatServo;
Servo WaterServo;
Servo GateServo;


void setup() {
  
  pinMode(Sens, INPUT);// Input from sensor
  pinMode(LED, OUTPUT);// OUTPUT to alarm or LED
  FoodServo.attach(22);// 
TreatServo.attach(23);
WaterServo.attach(19);
GateServo.attach(18);
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);// connect to blyk server
}

void loop() {
 Blynk.run();
  int motion =digitalRead(Sens);
  if(motion){
    Serial.println("Motion detected");
    digitalWrite(LED,HIGH);
  }
  else
  {
     Serial.println("===nothing moves");
     digitalWrite(LED,LOW);
  }
  delay(500);

}

BLYNK_WRITE(V1)
{
int FoodServoState =param.asInt();
if (FoodServoState==1)
{
FoodServo.writeMicroseconds(1000);
delay (2000);
FoodServo.writeMicroseconds(1500);
delay (2000);
FoodServo.writeMicroseconds(2000);
delay (2000);
FoodServo.writeMicroseconds(1500);
}
}
BLYNK_WRITE(V2)
{
int TreatServoState =param.asInt();
if (TreatServoState==1)
{
TreatServo.writeMicroseconds(1000);
delay (2000);
TreatServo.writeMicroseconds(1500);
delay (2000);
TreatServo.writeMicroseconds(2000);
delay (2000);
TreatServo.writeMicroseconds(1500);
}
}

BLYNK_WRITE(V3)
{
int WaterServoState =param.asInt();
if (WaterServoState==1)
{
WaterServo.writeMicroseconds(1000);
delay (2000);
WaterServo.writeMicroseconds(1500);
delay (2000);
WaterServo.writeMicroseconds(2000);
delay (2000);
WaterServo.writeMicroseconds(1500);
}
}
BLYNK_WRITE(V4)
{
int GateServoState =param.asInt();
if (GateServoState==1)
{
GateServo.writeMicroseconds(1000);
delay (2000);
GateServo.writeMicroseconds(1500);
delay (2000);
GateServo.writeMicroseconds(2000);
delay (2000);
GateServo.writeMicroseconds(1500);
}
}

Thank you everyone you are a great community!

You should read this…

Using an interval timer instead of the code in your void loop would be a good start.
Using timeout timers rather than the millis comparison in the example you found would be a good approach, although you may need to se nested timers if you want commands to run consecutively rather than concurrently.

Pete.

also, read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Thanks for your quick help, i will try this.

i didn’t understand the use of blynktimer and a clean void loop, can you please share a simple working code of this function so i can understand it better this way?

When you use blynk timer in your sketch, the ideal blynk void loop should look like this

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

I recommend you to read the article that @PeteKnight suggested carefully to understand how to use blynk timer.

Hey John, i read it and didn’t understand that really. That is why i ask for a simple sample if possible please.

The “keep your void loop clean” link that @John93 explains the reasoning behind keeping your void loop clean, and why a non-blocking timer such as BlynkTimer is needed instead of delays when using Blynk.

If you can’t follow that explanation then I’d suggest that you explain exactly which parts of that article you have problems grasping and we’ll attempt to clarify it for you.

Pete.