Remove delay from inside function

I am wanting to eliminate all Delays in my code.
I have a function in my main loop that accepts some data, uploads it to my web site ( php page that adds data to an online log file ), and then closes the connection.

From experience, I have found that I have to add a short delay ( in this example, a delay of 250 ms ) before client.stop so that the preceding code completes fully to the web based php page. The code at present is working as required.

void loop(void){ 
	unsigned long currentMillis = millis();
	void LogItWeb(int Location, float LogTxt){
		if (DSLcount > 0){  // if there was a previous failure, then wait 10 loops before trying again
			DSLcount++;
			if (DSLcount > 10) DSLcount = 0; // now try again
		}  
		if (DSLcount == 0){  
			DSLcount = 1;  // if the following connect fails, then DSLcount will be 1 and activate the 10 loop delay.
			if (client.connect(myserver, 80)) {
				DSLcount = 0;  // if the connect succeeds, then set the DSLcount to 0, so no delay loop next time
				client.print("GET http://www.xxx.com/xxx.php?data=");
				client.print(Location);
				client.print("--");
				client.print(LogTxt);
				client.println(" HTTP/1.1");
				client.println("Host: www.xxx.com");
				client.println();
				delay(250);
				client.stop();
			} 
		}
	} 
	LogItWeb(1,tempC2); // tempC2 contains a float value from a sensor reading
} // end of loop

Would it work to remove the client.stop to outside the function like this ? :
Is the function LogItWeb able to change the value of variable ClientStopMillis for use outside the function ?

void loop(void){ 
	unsigned long currentMillis = millis();
// add a new variable
	long ClientStopMillis = 0;
	void LogItWeb(int Location, float LogTxt){
		if (DSLcount > 0){  // if there was a previous failure, then wait 10 loops before trying again
			DSLcount++;
			if (DSLcount > 10) DSLcount = 0; // now try again
		}  
		if (DSLcount == 0){  
			DSLcount = 1;  // if the following connect fails, then DSLcount will be 1 and activate the 10 loop delay.
			if (client.connect(myserver, 80)) {
				DSLcount = 0;  // if the connect succeeds, then set the DSLcount to 0, so no delay loop next time
				client.print("GET http://www.xxx.com/xxx.php?data=");
				client.print(Location);
				client.print("--");
				client.print(LogTxt);
				client.println(" HTTP/1.1");
				client.println("Host: www.xxx.com");
				client.println();
// set the Stop time as the current millis + 250 ms
				ClientStopMillis = currentMillis + 250;
//	 			delay(250);
//				client.stop();
			} 
		}
	} 
// add this code to the general section of the Main loop
	if(ClientStopMillis != 0 && ClientStopMillis <= currentMillis){
		client.stop();
		ClientStopMillis = 0;
	}
	LogItWeb(1,tempC2); // tempC2 contains a float value from a sensor reading
} // end of loop

The code at present is working as required.

Given that LogItWeb() appears to be embedded in loop(), that's hard to believe.

The fine folks at http://snippets-r-us.com will be able to help you, though.

Thanks Paul

Sorry, my error when I tried to extract the text relevant to the question.

The LogItWeb function is in fact outside the main loop.

So the following should work ?

long ClientStopMillis = 0;

void setup(void) {

}

void loop(void){ 
	unsigned long currentMillis = millis();

	if(ClientStopMillis != 0 && ClientStopMillis <= currentMillis){
		client.stop();
		ClientStopMillis = 0;
	}

	LogItWeb(1,tempC2); // tempC2 contains a float value from a sensor reading

} // end of main loop

void LogItWeb(int Location, float LogTxt){
	if (DSLcount > 0){  // if there was a previous failure, then wait 10 loops before trying again
		DSLcount++;
		if (DSLcount > 10) DSLcount = 0; // now try again
	}  
	if (DSLcount == 0){  
		DSLcount = 1;  // if the following connect fails, then DSLcount will be 1 and activate the 10 loop delay.
		if (client.connect(myserver, 80)) {
			DSLcount = 0;  // if the connect succeeds, then set the DSLcount to 0, so no delay loop next time
			client.print("GET http://www.xxx.com/xxx.php?data=");
			client.print(Location);
			client.print("--");
			client.print(LogTxt);
			client.println(" HTTP/1.1");
			client.println("Host: www.xxx.com");
			client.println();
// set the Stop time as the current millis + 250 ms
			ClientStopMillis = currentMillis + 250;
//	 			delay(250);
//				client.stop();
		} 
	}
}

So the following should work ?

Maybe. It’s not the best way, though.

			ClientStopMillis = currentMillis + 250;

Suppose currentMillis is near the point where rollover will occur. ClientStopMillis could then be assigned a a very large value.

	if(ClientStopMillis != 0 && ClientStopMillis <= currentMillis){

Then, currentMillis will be a very small value, and the very large value will not be less than it, so the stop won’t happen.

Record when the event of interest occurred (the event of interest is NOT client stop). Then, compare now minus then to the interval.

If you want to avoid a problem when millis() rolls over you should always use subtraction. For example

if (miilis() millis() - previousMillis > interval) {

}

…R

What does miilis() do? Never heard of that function... 8)

PaulS:
What does miilis() do? Never heard of that function... 8)

!! smartass !!

...R

!! smartass !!

You rang?