OTA udate problem due to delay function

Dear experts

I completed my first Arduino/ESP32 project and it works so far to my satisfaction. I intend now to include the OTA Update function. Here I get the interference with my sketch below.

In order to make the scrolling routine (displayed text on TFT) not too fast, I had to include a delay of 200(ms) quite at the end of my code below. This however prevents the ArduinoOTA.handle() from working correctly. As soon as I get rid of the delay (3rd last line) all works fine but of course the scrolling text is displayed way too fast.

I am aware that I need to avoid the delay function in order to make OTA work, but how can I rewrite the scrolling routine to avoid the 200ms delay function in order to make ArduinoOTA.handle() work?

As I obviously will have to change the code anyway I also consider to completely rewrite the routine as an other option. In my project the text "scrolls" letter by letter. Is there a way I could make the scrolling text scrolling smooth lets say e.g. pixel by pixel and of course working with ArduinoOTA.handle()?

Thanks alot for helping me newby.

void loop()
{
  ArduinoOTA.handle(); // OTA Job

  //Scroll Routine
  String text = (furtherstops[furtherstops_id]); 

  const int width = 14; // width of the marquee display (in characters)

  // Loop once through the string

  for (int offset = 0; offset < text.length(); offset++)

  {
    // Construct the string to display for this iteration

    String t = ""; 
    for (int i = 0; i < width; i++)
      t += text.charAt((offset + i) % text.length());

    if (scroll_renew == (true)) 
    {
      scroll_renew = (false);   
      offset = (text.length()); 
      tft.fillScreen(ST7735_WHITE);
    }
    // Print the string for this iteration
    tft.setCursor(20, 74); // display position
    delay(200);    // Short delay so the text doesn't move too fast
    tft.print(t); 
  }
}

Untested

void loop()
{
  ArduinoOTA.handle(); // OTA Job
  unsigned long currentTime = millis();
  static unsigned long previousScrollTime = currentTime;
  const unsigned long scrollPeriod = 200;
  {
    previousScrollTime = currentTime; //save for next time
    //Scroll Routine
    String text = (furtherstops[furtherstops_id]);
    const int width = 14; // width of the marquee display (in characters)
    // Loop once through the string
    for (int offset = 0; offset < text.length(); offset++)
    {
      // Construct the string to display for this iteration
      String t = "";
      for (int i = 0; i < width; i++)
        t += text.charAt((offset + i) % text.length());
      if (scroll_renew == (true))
      {
        scroll_renew = (false);
        offset = (text.length());
        tft.fillScreen(ST7735_WHITE);
      }
      // Print the string for this iteration
      tft.setCursor(20, 74); // display position
      //delay(200);    // Short delay so the text doesn't move too fast
      tft.print(t);
    }
  }
}

Or the obvious, instead of the delay() use millis() and call ArduinoOTA.handle(); repeatedly while you wait.

uint32_t moment = millis();
while (millis() - moment < 200) {
  ArduinoOTA.handle();
  yield();
}

as always, great support. Thank you both very much indeed. I understand now the concept of how to avoid delay()

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