How do I keep the code in the loop repeating WifiS3.h library

I am trying to display the time from a RTC on a ST7796S display and change text color from a web server, I copied code from the AP_SimpleWebServer example and the time only updates when a button is pushed on the web server her is my code.

#include <WiFiS3.h>
#include <Wire.h>
#include "Arduino.h"
#include "uRTCLib.h"
#include <Adafruit_GFX.h>
#include <Adafruit_ST7796S_kbv.h>
#include <SPI.h>
#include "arduino_secrets.h"

#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8

Adafruit_ST7796S_kbv tft = Adafruit_ST7796S_kbv(TFT_CS, TFT_DC, TFT_RST);


uRTCLib rtc(0x68);

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;        // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key index number (needed only for WEP)

int led =  LED_BUILTIN;
int status = WL_IDLE_STATUS;
WiFiServer server(80);



int rtchour;

int day;
int hour;
int minute;
int second;

int lday;
int lhour;
int lminute;
int lsecond;

int inDisTime = 1;

int Pm = 0;
int lPm = 0;

int timeColor = 2;

void setup() {
  // put your setup code here, to run once:

 Serial.begin(9600);
  delay(3000); // wait for console opening
  URTCLIB_WIRE.begin();

  // Comment out below line once you set the date & time.
  // Following line sets the RTC with an explicit date & time
        rtc.set(45 ,59, 23, 2, 11, 2, 25);
  // rtc.set(second, minute, hour, dayOfWeek, dayOfMonth, month, year)
  // set day of week (1=Sunday, 7=Saturday)

    tft.begin();
  tft.setRotation(1);
  tft.fillScreen(ST7796S_BLACK);

//wifi setup
         while (!Serial) {
         ; // wait for serial port to connect. Needed for native USB port only
        }
        Serial.println("Access Point Web Server");

        // check for the WiFi module:
        if (WiFi.status() == WL_NO_MODULE) {
          Serial.println("Communication with WiFi module failed!");
          // don't continue
          while (true);
        }

        String fv = WiFi.firmwareVersion();
        if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
          Serial.println("Please upgrade the firmware");
        }

        // by default the local IP address will be 192.168.4.1
        // you can override it with the following:
        WiFi.config(IPAddress(192,48,56,2));

        // print the network name (SSID);
        Serial.print("Creating access point named: ");
        Serial.println(ssid);

        // Create open network. Change this line if you want to create an WEP network:
        status = WiFi.beginAP(ssid, pass);
        if (status != WL_AP_LISTENING) {
          Serial.println("Creating access point failed");
          // don't continue
          while (true);
        }

        // wait 10 seconds for connection:
        delay(10000);

        // start the web server on port 80
        server.begin();

        // you're connected now, so print out the status
        Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
      }



void loop() {
  // Capture last known values
  lday = rtc.day();
  if((rtchour > 0) && (rtchour < 13)){hour = rtchour;}
  if(rtchour > 12){lhour = rtchour - 12; lPm = 1;}
  if(rtchour == 0){lhour = 12; lPm = 0;}
  lminute = rtc.minute();
  lsecond = rtc.second();

  // Refresh RTC and get new values
  rtc.refresh();
  rtchour = rtc.hour();
  day = rtc.day();
  if((rtchour > 0) && (rtchour < 13)){hour = rtchour;}
  if(rtchour > 12){hour = rtchour - 12; Pm = 1;}
  if(rtchour == 0){hour = 12; Pm = 0;}
  minute = rtc.minute();
  second = rtc.second();

  Serial.print("Last Second: ");
  Serial.println(lsecond);
  Serial.print("Current Second: ");
  Serial.println(second);

  if ((lsecond != second) || (inDisTime == 1)) {
    tft.setTextColor(ST7796S_BLACK);
    tft.setTextSize(8);
    if(lhour > 9){tft.setCursor(40, 140); tft.print(lhour);}
    else{tft.setCursor(100, 140); tft.print(lhour);}

      tft.print(":");

    if(lminute < 10){tft.print("0"); tft.print(lminute);}
    else{tft.print(lminute);}

      tft.print(":");

    tft.print(lsecond);

    tft.setTextSize(3);

    if((lPm == 1) || (rtchour == 12)){tft.print("PM");}
    if((lPm == 0) && (rtchour != 12)){tft.print("AM");}
    
    if(timeColor == 1){tft.setTextColor(ST7796S_GREEN);}
    if(timeColor == 2){tft.setTextColor(ST7796S_BLUE);}
    tft.setTextSize(8);

    if(hour > 9){tft.setCursor(40, 140); tft.print(hour);}
    else{tft.setCursor(100, 140); tft.print(hour);}

      tft.print(":");

    if(minute < 10){tft.print("0"); tft.print(minute);}
    else{tft.print(minute);}

      tft.print(":");

    tft.print(second);

    tft.setTextSize(3);

    if((Pm == 1) || (rtchour == 12)){tft.print("PM");}
    if((Pm == 0) && (rtchour != 12)){tft.print("AM");}
    inDisTime = 0;

    tft.setTextSize(3);
    tft.setCursor(10, 210);
    tft.print(daysOfTheWeek[rtc.dayOfWeek()]);
    tft.print(", ");
    if(rtc.month() == 1){tft.print("January ");}
    if(rtc.month() == 2){tft.print("February ");}
    if(rtc.month() == 3){tft.print("March ");}
    if(rtc.month() == 4){tft.print("April ");}
    if(rtc.month() == 5){tft.print("May ");}
    if(rtc.month() == 6){tft.print("June ");}
    if(rtc.month() == 7){tft.print("July ");}
    if(rtc.month() == 8){tft.print("August ");}
    if(rtc.month() == 9){tft.print("Septeber ");}
    if(rtc.month() == 10){tft.print("October ");}
    if(rtc.month() == 11){tft.print("November ");}
    if(rtc.month() == 12){tft.print("December ");}
    tft.print(rtc.day());
    tft.print(", ");
    tft.print(rtc.year());


    if(hour != lhour){tft.fillScreen(ST7796S_BLACK); lhour = hour;}
  }
  
  Serial.print("Hour: ");
  Serial.println(hour);
  Serial.print("Minute: ");
  Serial.println(minute);
  Serial.print("Second: ");
  Serial.println(second);





    if (status != WiFi.status()) {
    // it has changed update the variable
    status = WiFi.status();

    if (status == WL_AP_CONNECTED) {
      // a device has connected to the AP
      Serial.println("Device connected to AP");
    } else {
      // a device has disconnected from the AP, and we are back in listening mode
      Serial.println("Device disconnected from AP");
    }
  }
  
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      delayMicroseconds(10);                // This is required for the Arduino Nano RP2040 Connect - otherwise it will loop so fast that SPI will never be served.
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out to the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("<p style=\"font-size:7vw;\">Click <a href=\"/timeGreen\">here</a> to make time GREEN<br></p>");
            client.print("<p style=\"font-size:7vw;\">Click <a href=\"/timeBlue\"</a> to make time Blue<br></p>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            continue;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /timeGreen")) {
          timeColor = 1;              // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /timeBlue")) {
          timeColor = 2;                // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}




What is your question?

the time only updates when a button is pushed on the web server how do I make it update 24/7

Are you talking about the RTC?

No, I am talking about why clock stops updating when a device is connected to the Wi-Fi.

it is not the RTC the clock works fine when a device is not connected.

What clock? I thought you had an RTC (Real Time Clock with battery backup) They run for 8 yrs in timekeeping mode.

clock, as in displaying the time on my ST7796S display but the time on the display stops updating when a device is connected to the Wi-Fi and only updates when the color of the text is changed via the button on the web server.

the Arduino does not detect the RTC when the battery it has a backup battery in it but that is another story.

continue restarts the loop. Try -- as the comment says -- break

I fixed that already I forgot to update it on the forum but that's what my code has always been

if it is not clear I still need help on this

Post the new code here, do not edit the code in post #1.

Do not bump your topic.

#include <WiFiS3.h>
#include <Wire.h>
#include "Arduino.h"
#include "uRTCLib.h"
#include <Adafruit_GFX.h>
#include <Adafruit_ST7796S_kbv.h>
#include <SPI.h>
#include "arduino_secrets.h"

#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8

Adafruit_ST7796S_kbv tft = Adafruit_ST7796S_kbv(TFT_CS, TFT_DC, TFT_RST);


uRTCLib rtc(0x68);

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;        // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key index number (needed only for WEP)

int led =  LED_BUILTIN;
int status = WL_IDLE_STATUS;
WiFiServer server(80);



int rtchour;

int day;
int hour;
int minute;
int second;

int lday;
int lhour;
int lminute;
int lsecond;

int inDisTime = 1;

int Pm = 0;
int lPm = 0;

int timeColor = 2;

void setup() {
  // put your setup code here, to run once:

 Serial.begin(9600);
  delay(3000); // wait for console opening
  URTCLIB_WIRE.begin();

  // Comment out below line once you set the date & time.
  // Following line sets the RTC with an explicit date & time
        rtc.set(45 ,59, 23, 2, 11, 2, 25);
  // rtc.set(second, minute, hour, dayOfWeek, dayOfMonth, month, year)
  // set day of week (1=Sunday, 7=Saturday)

    tft.begin();
  tft.setRotation(1);
  tft.fillScreen(ST7796S_BLACK);

//wifi setup
         while (!Serial) {
         ; // wait for serial port to connect. Needed for native USB port only
        }
        Serial.println("Access Point Web Server");

        // check for the WiFi module:
        if (WiFi.status() == WL_NO_MODULE) {
          Serial.println("Communication with WiFi module failed!");
          // don't continue
          while (true);
        }

        String fv = WiFi.firmwareVersion();
        if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
          Serial.println("Please upgrade the firmware");
        }

        // by default the local IP address will be 192.168.4.1
        // you can override it with the following:
        WiFi.config(IPAddress(192,48,56,2));

        // print the network name (SSID);
        Serial.print("Creating access point named: ");
        Serial.println(ssid);

        // Create open network. Change this line if you want to create an WEP network:
        status = WiFi.beginAP(ssid, pass);
        if (status != WL_AP_LISTENING) {
          Serial.println("Creating access point failed");
          // don't continue
          while (true);
        }

        // wait 10 seconds for connection:
        delay(10000);

        // start the web server on port 80
        server.begin();

        // you're connected now, so print out the status
        Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
      }



void loop() {
  // Capture last known values
  lday = rtc.day();
  if((rtchour > 0) && (rtchour < 13)){hour = rtchour;}
  if(rtchour > 12){lhour = rtchour - 12; lPm = 1;}
  if(rtchour == 0){lhour = 12; lPm = 0;}
  lminute = rtc.minute();
  lsecond = rtc.second();

  // Refresh RTC and get new values
  rtc.refresh();
  rtchour = rtc.hour();
  day = rtc.day();
  if((rtchour > 0) && (rtchour < 13)){hour = rtchour;}
  if(rtchour > 12){hour = rtchour - 12; Pm = 1;}
  if(rtchour == 0){hour = 12; Pm = 0;}
  minute = rtc.minute();
  second = rtc.second();

  Serial.print("Last Second: ");
  Serial.println(lsecond);
  Serial.print("Current Second: ");
  Serial.println(second);

  if ((lsecond != second) || (inDisTime == 1)) {
    tft.setTextColor(ST7796S_BLACK);
    tft.setTextSize(8);
    if(lhour > 9){tft.setCursor(40, 140); tft.print(lhour);}
    else{tft.setCursor(100, 140); tft.print(lhour);}

      tft.print(":");

    if(lminute < 10){tft.print("0"); tft.print(lminute);}
    else{tft.print(lminute);}

      tft.print(":");

    tft.print(lsecond);

    tft.setTextSize(3);

    if((lPm == 1) || (rtchour == 12)){tft.print("PM");}
    if((lPm == 0) && (rtchour != 12)){tft.print("AM");}
    
    if(timeColor == 1){tft.setTextColor(ST7796S_GREEN);}
    if(timeColor == 2){tft.setTextColor(ST7796S_BLUE);}
    tft.setTextSize(8);

    if(hour > 9){tft.setCursor(40, 140); tft.print(hour);}
    else{tft.setCursor(100, 140); tft.print(hour);}

      tft.print(":");

    if(minute < 10){tft.print("0"); tft.print(minute);}
    else{tft.print(minute);}

      tft.print(":");

    tft.print(second);

    tft.setTextSize(3);

    if((Pm == 1) || (rtchour == 12)){tft.print("PM");}
    if((Pm == 0) && (rtchour != 12)){tft.print("AM");}
    inDisTime = 0;

    tft.setTextSize(3);
    tft.setCursor(10, 210);
    tft.print(daysOfTheWeek[rtc.dayOfWeek()]);
    tft.print(", ");
    if(rtc.month() == 1){tft.print("January ");}
    if(rtc.month() == 2){tft.print("February ");}
    if(rtc.month() == 3){tft.print("March ");}
    if(rtc.month() == 4){tft.print("April ");}
    if(rtc.month() == 5){tft.print("May ");}
    if(rtc.month() == 6){tft.print("June ");}
    if(rtc.month() == 7){tft.print("July ");}
    if(rtc.month() == 8){tft.print("August ");}
    if(rtc.month() == 9){tft.print("Septeber ");}
    if(rtc.month() == 10){tft.print("October ");}
    if(rtc.month() == 11){tft.print("November ");}
    if(rtc.month() == 12){tft.print("December ");}
    tft.print(rtc.day());
    tft.print(", ");
    tft.print(rtc.year());


    if(hour != lhour){tft.fillScreen(ST7796S_BLACK); lhour = hour;}
  }
  
  Serial.print("Hour: ");
  Serial.println(hour);
  Serial.print("Minute: ");
  Serial.println(minute);
  Serial.print("Second: ");
  Serial.println(second);





    if (status != WiFi.status()) {
    // it has changed update the variable
    status = WiFi.status();

    if (status == WL_AP_CONNECTED) {
      // a device has connected to the AP
      Serial.println("Device connected to AP");
    } else {
      // a device has disconnected from the AP, and we are back in listening mode
      Serial.println("Device disconnected from AP");
    }
  }
  
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      delayMicroseconds(10);                // This is required for the Arduino Nano RP2040 Connect - otherwise it will loop so fast that SPI will never be served.
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out to the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("<p style=\"font-size:7vw;\">Click <a href=\"/timeGreen\">here</a> to make time GREEN<br></p>");
            client.print("<p style=\"font-size:7vw;\">Click <a href=\"/timeBlue\"</a> to make time Blue<br></p>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /timeGreen")) {
          timeColor = 1;              // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /timeBlue")) {
          timeColor = 2;                // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}




How have your changes effected the sketch? Any improvement? Does it compile? Where do you first notice any problem? Are you able to write a minimal sketch to duplicate the error? Are you able to write a minimal sketch to create a clock that auto-updates? Did you write this, and if not, where did you find it?

How long between "new client" and "client disconnected"? Are you seeing "Last Second: " and all the other time info being printed (repeatedly and frequently) even if the clock does not change? Is the "Current Second: " different only after using the web page?

If there was no HTTP request+response, the loop runs pointlessly fast; it's hard to see those clock messages. To debug, you can try a delay

  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    // all that stuff, ending with
    client.stop();
    Serial.println("client disconnected");
  } else {        // add
    delay(100);   // this -- or even for longer
  }