Updating more than one field in ThingSpeak

Hello, I am new to this forum and a noob when it comes to programming. I farm and have set up a moisture sensor on a grain dryer where two analog voltages are read by an Uno and the data uploaded to ThingSpeak so I can monitor it remotely. I can get both the moisture and the temperature values to be read and to upload to field 1 in my ThingSpeak channel. But I cannot figure out how to get them both to write in one update, moisture in field 1 and temperature in field 2. I have both fields initiated in ThingSpeak and have successfully written to both fields, but can only write to one or the other. Please find my code below. If you have suggestions of any, I want to learn.

This is are the specific lines in question as to why they won't write to both fields...

  // Update ThingSpeak
  if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
  {
    updateThingSpeak("field1="+analogPin0);
    updateThingSpeak("field2="+analogPin1);
    
  }

and this is the whole program...

#include <SPI.h>
#include <Ethernet.h>

int moistPin = 0; // analog moisture pin
int tempPin = 1; // analog temperature pin
float calNum = 0; // adjust number for cal
float moist = 0; // moisture variables
float temp = 0;
float temp1 = 0;
float tempCorrection = 0; // temp correction variable
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;

// Local Network Settings
byte mac[]     = { x }; // Must be unique on local network
byte ip[]      = { x };                // Must be unique on local network
byte gateway[] = { x};
byte subnet[]  = { x };

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "x";    // Write API Key for a ThingSpeak Channel
const int updateInterval = 20000;        // Time interval in milliseconds to update ThingSpeak   

// Variable Setup
long lastConnectionTime = 0; 
boolean lastConnected = false;
int failedCounter = 0;

// Initialize Arduino Ethernet Client
EthernetClient client;

void setup()
{

  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, subnet);
  delay(1000);
  Serial.print("ETHERNET SHIELD ip  is     : ");
  Serial.println(Ethernet.localIP()); 
   // Start Ethernet on Arduino
  startEthernet();
}

void loop()
{
  temp = ((((analogRead(tempPin) * 5.0) /1023.0)/ .0347)-9.4 );
  temp1 = (analogRead(tempPin)); //volts
  tempCorrection = ((3.1 - ((analogRead(tempPin) * 5.0) /1023.0)) / .0347 * .05) ;
  //added decimals to the intergers
  moist = (( 4.8 * analogRead(moistPin) * 5) / 1023) + 6.5 + calNum + tempCorrection;

  String analogPin0 = String(moist);
  String analogPin1 = String(temp);
  String tempvolt = String(temp1);

    //Serial.print(tempvolt);
    //Serial.print(analogPin1);
    //Serial.println();
 
  // Print Update Response to Serial Monitor
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
  
  // Disconnect from ThingSpeak
  if (!client.connected() && lastConnected)
  {
    Serial.println();
    Serial.println("...disconnected.");
    Serial.println();
    
    client.stop();
  }
  
  // Update ThingSpeak
  if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
  {
    updateThingSpeak("field1="+analogPin0);
    updateThingSpeak("field2="+analogPin1);
    
  }
  
  lastConnected = client.connected();
}

void updateThingSpeak(String tsData)
{
  if (client.connect(thingSpeakAddress, 80))
  { 
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("\n\n");

    client.print(tsData);
    
    lastConnectionTime = millis();
    
    if (client.connected())
    {
      Serial.println("Connecting to ThingSpeak...");
      Serial.println();
      
      failedCounter = 0;
    }
    else
    {
      failedCounter++;
  
      Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");   
      Serial.println();
    }
    
  }
  else
  {
    failedCounter++;
    
    Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");   
    Serial.println();
    
    lastConnectionTime = millis(); 
  }
}

void startEthernet()
{
  
  client.stop();

  Serial.println("Connecting Arduino to network...");
  Serial.println();  

  delay(1000);
  
  // Connect to network amd obtain an IP address using DHCP
  if (Ethernet.begin(mac) == 0)
  {
    Serial.println("DHCP Failed, reset Arduino to try again");
    Serial.println();
  }
  else {
    Serial.println("Arduino connected to network using DHCP");
    Serial.println();
    Serial.println("Data being uploaded to THINGSPEAK Server.......");
    Serial.println();
  }
  
  delay(1000);
}

By the way, in the example above, what ends up happening is that field 1 gets updated continually with the moisture reading and field 2 is never updated. IF I flip the two lines of code then field 2 gets updated and field one gets nothing.

Maybe try adding a 5 sec or so delay between writes..

Else you can use a statement to alternate between sending between the two fields every other pass

Else you can just merge both measurements into one field

Else...

TS has a 15 sec upload rate limit. Delay at least that amount.

any examples of how to alternate between writes?

Here's one (possible) way of doing it. It's a bit elaborate but might give you ideas for a different/better way.
The actual test can be rewritten for brevity/performance/programming practice later.

// just a define for readability later on.
#define FIELD1 true

// used to determine which field will get updates this time around
bool whos_turn = FIELD1;

void loop() {
	
	..................
	
	// all your stuff
	
	...............
	
	// Update ThingSpeak
	if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
	{
		// check to see which field should get sent this time around
		if (whos_turn == FIELD1)
			updateThingSpeak("field1="+analogPin0);
		else
			updateThingSpeak("field2="+analogPin1);
		
		// toggle whos_turn so the other field will get updated next time around
		whos_turn = !whos_turn;
	}
	
	............
	
	// the rest of your stuff
 
}

Thanks for that example. I will study it to see if I understand what it is doing and how to adapt it.