Non-blocking delay for Blynk

I want to use blynk and I need delays whereby the rest of the code IS NOT running (I’d rather not use new libraries or call millis() every single time), my understanding is that the Blynk.run() function needs to be called upon over and over to prevent it from disconnecting. Will this function solve my problem ?

void MyDelay(int DelayTime)
{
  unsigned long timenow = millis();
  unsigned long TimeAtEnd = timenow + DelayTime;
  for( ; ; )
  {
    Blynk.run();
    if (millis()>=TimeAtEnd)
    break;
  }
}

There’s no conflict in skipping delay and Blynk.
Let Blynk run every turn i loop. For the other code You use millis() and set a time for the next time to run that other code.
Reading millis() every turn and comparing is a quick process.
Post an entire code. The snippet You sent is not much useful…

Firstly thank you for your reply

"
case 5592512: // Close Buttons

    mySwitch.disableReceive();

    if (open == true)
    {

      int TimeSpentClosing = 0;
      
      for (;;)
      {
        if(TimeSpentClosing>=ClosingTime)
        {
          digitalWrite(CloseRelay, HIGH);               
          break;
        }
        
        if (digitalRead(Sensor) == HIGH)
        {
          digitalWrite(CloseRelay, LOW);
          Serial.println("Closing...");
          MyDelay(100);
          TimeSpentClosing += 100;
        }
        else
        {
          digitalWrite(CloseRelay, HIGH);
          Serial.println("OBSTRUCTION DETECTED");
          MyDelay(3000);

          for (;;)
          {
            if (digitalRead(LimitSwitch))
            {
              digitalWrite(OpenRelay, LOW);
              Serial.println("Opening...");
            }
            else
            {
              digitalWrite(OpenRelay, HIGH);
              TimeSpentClosing = 0;
              Serial.println("Open");
              MyDelay(3000);
              break;
            }
          }
        }
      }

      Serial.println("----------");
      Serial.println("Current Position: Closed");
      open = false;
      MyDelay(3000); // Prevent jolting

    }

    break;

"

This is part of how I’ll be using the MyDelay function, all that matters to me is that the blynk app doesn’t disconnect or crash (It still isn’t modified completely for Blynk but hopefully this will be sufficient to make it clear what I want to do)

Don’t post snippets but post the entire code if You want useful answers. Else You will get guesses, and experienced coders don’t spend time on guessing.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.