How to delay the Servomotor Blynk2.0?

i want some help me to delay the srevo motor and tern off the servo to change the angle manual ,
i working on lockdoor with servomotor and i want the angle of the servo change from 0 to 180 degree and delay 7sec and change the angle from 180 to 0 and when the angle 0 i want to move the servo manually ,

   digitalWrite(LED_G, HIGH);
     tone(BUZZER, 500);
    delay(500);
    noTone(BUZZER);
     
for (angle =0 ; angle <180; angle +=10)
    {
   myservo.write(angle); //servo start position
   delay(150); //speed time to open
   
    }

    delay(7000);
   for(angle = 180; angle>=0; angle -=10)
    {
      myservo.write(angle);
    delay(150); // speed time to close
    }
    digitalWrite(LED_G, LOW); 

please help me :broken_heart:

this is my code :


// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPLB8_djuKD"
#define BLYNK_DEVICE_NAME "SMART LOCK"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>


const char* ssid = "snap: pxx.7";                     // your network SSID (name)
const char* password = "55443322";                   // your network key

#define SS_PIN 4
#define RST_PIN 5
#define LED_G 16 //define green LED pin
#define LED_R 0 //define red LED
#define BUZZER 15 //buzzer pin
#define SERVO_PIN 2
Servo myservo;
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
 int angle =0;
int fflag = 0; 
int eflag = 0; 
int jflag = 0;

#include "BlynkEdgent.h"

void Open()
{
   digitalWrite(LED_G, HIGH);
     tone(BUZZER, 500);
    delay(500);
    noTone(BUZZER);
     
for (angle =0 ; angle <180; angle +=10)
    {
   myservo.write(angle); //servo start position
   delay(150); //speed time to open
   
    }

   delay(7000);
   for(angle = 180; angle>=0; angle -=10)
    {
      myservo.write(angle);
    delay(150); // speed time to close
    }
    digitalWrite(LED_G, LOW);
}
void NotAuthorized()
{
    Serial.println(" Not Authorized to Enter ");
   digitalWrite(LED_R, HIGH);
   tone(BUZZER, 300);
    delay(1500);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
    delay(DENIED_DELAY);
}

void setup()
{
  Serial.begin(115200);
  delay(100);

SPI.begin();          // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  myservo.attach(SERVO_PIN);
  myservo.write( 0 );
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  digitalWrite(LED_R, LOW);
  digitalWrite(LED_G, LOW);

  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  Serial.println("Put your card to the reader...");
  Serial.println();

  WiFi.disconnect();
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  BlynkEdgent.begin();
}

void loop() {

//________________________if there is NO internet_____________________
   // attempt to connect to Wifi network:
  while ( WiFi.status() != WL_CONNECTED) {  

    // Look for new cards
    if (  (! mfrc522.PICC_IsNewCardPresent()) )
  {
    return;
  }
  // Select one of the cards
  if (  (! mfrc522.PICC_ReadCardSerial()))
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase(); 
  


  if (  (content.substring(1) == "E9 CB BC B2")) //change here the UID of the card
  {
    Serial.println("Authorized access");
    Serial.println("Abdulrahman_201712803");
    Serial.println();
    
  Open();

 
  }
 
 else   {
 NotAuthorized();
  } 


    // wait 10 seconds for connection:
    delay(1000);
  }
  
//________________________if there is internet_____________________
   while ( WiFi.status() == WL_CONNECTED) {
  // once you are connected :
 BlynkEdgent.run();
  // check the network status connection once every 1 seconds:
  delay(1000);

    // Look for new cards
    if (  (! mfrc522.PICC_IsNewCardPresent()) )
  {
    return;
  }
  // Select one of the cards
  if (  (! mfrc522.PICC_ReadCardSerial()))
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase(); 

//change here the ( Student's )UID of the card_______________________________________________
  if ( ( (content.substring(1) == "E9 CB BC B2") ||     //the first card for Abdulrahman_201712803 
         (content.substring(1) == "C9 F4 CA BB")        //the second card for Ibrahim_201716556
         )&& (fflag== 1)) 
  {
    //the first card
    if ( (content.substring(1) == "E9 CB BC B2") && (fflag== 1)){
    Serial.println("The student Enter:");
    Serial.println("Abdulrahman_201712803");
    Blynk.virtualWrite(V2, "The student Enter:" );
    Blynk.virtualWrite(V2, "Abdulrahman_201712803" );
    }
    // the second card
    else if ( (content.substring(1) == "C9 F4 CA B1") && (fflag== 1)){
    Serial.println("The student Enter:");
    Serial.println("Ibrahim_201716556");
    Blynk.virtualWrite(V2, "The student Enter:" );
    Blynk.virtualWrite(V2, "Ibrahim_201716556" );
    }
    
    Serial.println();
    // here we want from you do the change please____
       digitalWrite(LED_G, HIGH);
     tone(BUZZER, 500);
    delay(500);
    noTone(BUZZER);
     
for (angle =0 ; angle <180; angle +=10)
    {
   myservo.write(angle); //servo start position
   delay(150); //speed time to open
   
    }

   delay(7000);
   for(angle = 180; angle>=0; angle -=10)
    {
      myservo.write(angle);
    delay(150); // speed time to close
    }
    digitalWrite(LED_G, LOW);
//until here
  }
//change here the ( Doctors's )UID of the card_______________________________________________
  else if (  ((content.substring(1) == "C9 F4 CA B2")||        //the first card
              (content.substring(1) == "C9 F4 CA B1")         //the second card
              )&& (eflag== 1)) 
  {
    //the first card
    if ( (content.substring(1) == "C9 F4 CA B2") && (eflag== 1)){
    Serial.println("The Doctor Enter:");
    Serial.println("Dr.Abdullah Albaker");
    Blynk.virtualWrite(V2, "The Doctor Enter:" );
    Blynk.virtualWrite(V2, "Dr.Abdullah Albaker" );
    }
    //the second card
    else if ( (content.substring(1) == "C9 F4 CA B1") && (eflag== 1)){
    Serial.println("The Doctor Enter:");
    Serial.println("Dr.Abdulaziz almalaq");
    Blynk.virtualWrite(V2, "The Doctor Enter:" );
    Blynk.virtualWrite(V2, "Dr.Abdulaziz almalaq" );
    
    }
    
    Serial.println();    
    Open();

  }
  //change here the ( Head of Department )UID of the card_______________________________________________
   else if (  (content.substring(1) == "C9 F4 CA CC") ) //change here the UID of the card
  {
    Serial.println("Head of Department Enter");
    Serial.println("Dr.Ahmad saud");
    Blynk.virtualWrite(V2, "Head of Department Enter" );
    Blynk.virtualWrite(V2, "Dr.Ahmad saud" );
    
    Serial.println();
    Open();

  }
 else   {
  Blynk.virtualWrite(V2, "Some one (Not authorized) try to Enter" );
   NotAuthorized();

  }
  delay(1000);
  
  }
  
}
BLYNK_CONNECTED(){
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);
  Blynk.syncVirtual(V2);
}
BLYNK_WRITE(V3)
{
  
   fflag = param.asInt(); // assigning incoming value from pin V3 to a variable
  
   
 
}
BLYNK_WRITE(V4)
{
   eflag = param.asInt(); // assigning incoming value from pin V4 to a variable
 
}

Read the Timeout Timer section of this topic…

Pete.

Thank you for that, but I am a beginner in writing code, can you just write me the code that I add to my previous code to make it work correctly

You need to write the code and test it with your hardware.

Pete.

[Unformatted code removed by moderator]

This is my code and just i want to change the delay of the servo when the degree is 180

The angle gose from 0 to 180

first of all you must read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

@eng.abdulrahman134 please edit your post, using the pencil icon at the bottom, and remove the block quotes that you’ve used to format your code and put triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:

```
Pete.


    Serial.println();
    // here we want from you do the change please____
       digitalWrite(LED_G, HIGH);
     tone(BUZZER, 500);
    delay(500);
    noTone(BUZZER);
     
for (angle =0 ; angle <180; angle +=10)
    {
   myservo.write(angle); //servo start position
   delay(150); //speed time to open
   
    }

   delay(7000);
   for(angle = 180; angle>=0; angle -=10)
    {
      myservo.write(angle);
    delay(150); // speed time to close
    }
    digitalWrite(LED_G, LOW);
//until here

when Blynk is connect i want delay the servo 0 to 180 and delay for 7sec and gose from 180 to 0

like this?

I’m curious about why, when I ask you to edit your existing post - using the pencil icon, you choose to ignore that request and post your code again, leaving the unformatted code in your original post.

Can you explain to me why you did that?

Pete.

sorry i dont understand you

sir like this you want?

You posted your sketch in post #5, but you used blockquotes instead of triple backticks.

I asked you to edit post #5 and fix the formatting of the code. Instead you posted the code again in post #8, so I had to go back and remove your blockquoted code from post #5

I was curious why you didn’t do what I asked…

and instead created extra work for me.

Pete.

1 Like

oh , now I understanding you sorry about it
I’m new again sorry

@eng.abdulrahman134 please keep your issue in one place, and avoid posting in other people’s topics.

You’ve posted questions about replacing delays with timers in two other topics, when I’ve already provided you with a link to a tutorial on how to do that.

Please don’t spam the forum in this way. If you continue your forum account will be suspended.

If you want to get this working, you will need to improve your C++ programming skills so I suggest that’s where you begin.

Pete.

I didn’t know that forum systems are like that
My project has a month left to deliver and I am nervous because it is ready. I only have this problem

Thank you all

Okay, use timeout timer instead of delay.
Like this

timer.setTimeout(7000L, []()

So you want other people to write your code for you, so that you can pass it off as your own work?

Your biggest problem - from a Blynk point of view at least - is your void loop. It’s far too cluttered to work reliably with Blynk, and you haven’t taken on board the feedback from John about restructuring your code to fix that.

Also, as you are using the Edgent sketch, I’m fairly sure that you will have some pin conflicts between these:

and the pins used for the Edgent functionality in your Settings.h file. Read this for more info…

Pete.

Yes, I used it and it was showing me an error in the code
I used a library
#include <SimpleTimer.h>

I think I have a problem when I call the library in
Void loop()
timer.run()

You’re using simple timer not blynk timer.
Read the article in post number 2