Timing Function in Loop not working after correcting Braces.

I pieced together a data logger/webcllient sketch that was working well for me but I wanted to clean it up and format it better and that's when I got into trouble. To compound the problem I also accidentally deleted a working backup. The timer function is declared at the start of the loop and should wait for about 2 minutes before performing a 'get' that sends the data to my server. What really happens is that it loops at about one minute generally and sometimes happens much faster. I have checked that the braces seem to be where I need them and the loop seems to be declared properly but I am unable to determine what I did wrong. Can someone see what I messed up and help me fix it?

#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>
#include <NewPing.h>
#define DHTPIN 37     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11 

#define TRIGGER_PIN  35  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     33  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

DHT dht(DHTPIN, DHTTYPE);

// this must be unique
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// change to your network settings
IPAddress gateway(192, 168, 209, 1);
IPAddress subnet(255, 255, 255, 0);

// change to your server
IPAddress server(xxx,xxx,xxx,xxx); // "your server.com"

//Change to your domain name for virtual servers
char serverName[] = "xxxxxxxx.com";
// If no domain name, use the ip address above
// char serverName[] = "xxx.xxx.xxx.xxx";

// change to your server's port
int serverPort = 80;

EthernetClient client;
char pageAdd[64];


// set this to the number of milliseconds delay
// this is 30 seconds
#define delayMillis 120000UL

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);

  dht.begin();

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // Start ethernet
  Serial.println(F("Starting ethernet..."));
  // Ethernet.begin(mac, ip, gateway, gateway, subnet);

  // If using dhcp, comment out the line above 
  // and uncomment the next 2 lines

  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else Serial.println(F("ok"));
  digitalWrite(10,HIGH);

  Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("Ready"));
}

void loop()
{
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  int volt1 = digitalRead(31);
  // if (volt1 < 2){volt1 = 11;} 
  int volt2 = analogRead(12);
  volt2 = (volt2 / 68.12 ,1);
  int humid1 = dht.readHumidity();
  int level1 = (uS / US_ROUNDTRIP_CM);
  int temp1 = dht.readTemperature();
  int spare2 = digitalRead (38);
  if (isnan(temp1) || isnan(level1)) 


  {
    Serial.println("Failed to read from DHT");
  } 
  else
  {
    Serial.print("Humidity: "); 
    Serial.print(humid1);
    Serial.println(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(temp1);
    Serial.println(" *C");
    Serial.print("Water Level: "); 
    Serial.println(level1);
    Serial.print("House Volt: ");
    Serial.println(volt1);
    Serial.print("Battery: "); 
    Serial.println(volt2);

  }
  {
    thisMillis = millis();

    if(thisMillis - lastMillis > delayMillis)
    {
      lastMillis = thisMillis;
    }





    // Modify next line to load different page
    // or pass values to server
    sprintf(pageAdd,"/data.php?temp1=%d&level1=%d&volt1=%d&volt2=%d&humid1=%d&spare2=%d",temp1,level1,volt1,volt2,humid1,spare2);


    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
    else Serial.println(F("Pass "));
  }    

}
byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,thisPort))
  {
    Serial.println(F("connected"));

    sprintf(outBuf,"GET %s HTTP/1.0",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",serverName);
    client.println(outBuf);
    client.println(F("Connection: close\r\n"));
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }


  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

  return 1;
}
}
{

It's hard to see a good reason for that construct.

Oh my, That was just a piece of the code I had tried for troubleshooting. Here is the whole code.

#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>
#include <NewPing.h>
#define DHTPIN 37     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11 

#define TRIGGER_PIN  35  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     33  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

DHT dht(DHTPIN, DHTTYPE);

// this must be unique
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// change to your network settings
IPAddress gateway(192, 168, 209, 1);
IPAddress subnet(255, 255, 255, 0);

// change to your server
IPAddress server(xxx,xxxx,xxx,xxx); // "mysever.com"

//Change to your domain name for virtual servers
char serverName[] = "myservr.com";
// If no domain name, use the ip address above
// char serverName[] = "xxx.xxx.xxx.xxx";

// change to your server's port
int serverPort = 80;

EthernetClient client;
char pageAdd[64];


// set this to the number of milliseconds delay
// this is 30 seconds
#define delayMillis 120000UL

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
  
  dht.begin();
  
  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // Start ethernet
  Serial.println(F("Starting ethernet..."));
  // Ethernet.begin(mac, ip, gateway, gateway, subnet);

  // If using dhcp, comment out the line above 
  // and uncomment the next 2 lines

  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else Serial.println(F("ok"));
  digitalWrite(10,HIGH);

  Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("Ready"));
}

void loop()
{
    thisMillis = millis();
    if(thisMillis - lastMillis > delayMillis)
    lastMillis = thisMillis;
    
    unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
    
    int volt1 = digitalRead(31);
    if (volt1 < 2)(volt1 = 12.7);  
    int volt2 = analogRead(12);
        volt2 = (volt2 / 68.12 ,1);
    if (volt2 < 2)(volt2 = 11.3);    
    int humid1 = dht.readHumidity();
    int level1 = (uS / US_ROUNDTRIP_CM);
    int temp1 = dht.readTemperature();
    int spare2 = digitalRead (38);
    
    if (isnan(temp1) || isnan(level1)) 
      (Serial.println("Failed to read from DHT"));
     
      else
  {
        Serial.print("Humidity: "); 
        Serial.print(humid1);
        Serial.println(" %\t");
        Serial.print("Temperature: "); 
        Serial.print(temp1);
        Serial.println(" *C");
        Serial.print("Water Level: "); 
        Serial.println(level1);
        Serial.print("House Volt: ");
        Serial.println(volt1);
        Serial.print("Battery: "); 
        Serial.println(volt2);
        
    }
  
    
    // Modify next line to load different page
    // or pass values to server
    sprintf(pageAdd,"/data.php?temp1=%d&level1=%d&volt1=%d&volt2=%d&humid1=%d&spare2=%d",temp1,level1,volt1,volt2,humid1,spare2);

 
    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
    else Serial.println(F("Pass "));
        
 
  }    
 

byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,thisPort))
  {
    Serial.println(F("connected"));

    sprintf(outBuf,"GET %s HTTP/1.0",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",serverName);
    client.println(outBuf);
    client.println(F("Connection: close\r\n"));
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

  return 1;
}

This at least might look like a usable construct/

OK. I removed the second brace as you see in the above revision but it did not change the timing loop. The auto formatting checks ok. I know that the delay loop is not getting to the end of the code but I haven't found out why.

I am going to try again to fix this myself. At first I was sure that I had just placed or removed a brace and that still might be true but just guessing which one is wrong was a futile task. I know that I can use the serial port to to output debug info so I am going to rem out parts of code and inject some serial output to see if I can figure out where I messed up.

Pizzanova:

void loop()
{
    thisMillis = millis();
    if(thisMillis - lastMillis > delayMillis){
        lastMillis = thisMillis;
    }

What is that supposed to achieve ? If it's a delay, then move the last curly brace to the end of the bit you want to delay.

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)

How do you know that the preceding code takes more than 1 millisecond?

This at least might look like a usable construct

Not to me.

Thank you all for your suggestions with this timing issue. I am rather new to programming but even I know that the above code is not well written. As it turns out though the biggest issue with this sketch was the declaration of the char addpage which was not large enough and caused the buffer to overflow which as many might already know ( and I have just learned ) causes all kinds of strange behaviour. After doubling the size of this buffer, the sketch runs and times properly. I'm posting this now just in case anyone who follows this post would find some use in the above sketch and I am giving myself a pat on the back for being able to troubleshoot this issue by myself.