time-share tasks, was:eth connection and lcd

Hi,

I need a suggestion as I am out of ideas (yes i am a newbie :).

I want to turn on a relay upon a telnet command and, every minute, have the lcd display temp and command status.
Now the two activities separately work fine, but i cannot achieve this together as it seems that lcd.print will need to print continuosly. Setting a delay will bother the ethernet connection (i am guessing however don't think i am too far from reality). Here is my code:

void loop()
{
    lcd.backlight();  //Backlight ON if under program control
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client == true){
      char c = client.read();
      readString.concat(c);
      if ( c == '\n') {
        if (readString.startsWith("heaton")){
          server.write("Heating is ON!\n");
          digitalWrite(outPin, HIGH);          
        } else if (readString.startsWith("hoff") >0 ) {
            digitalWrite(outPin, LOW);
            server.write("Heating is OFF!\n");
        } else if (readString.startsWith("status") >0 ) {
          server.write("Status: " && "\n");
      }
      readString = "";
    }
  }
  tempo++;
  if (tempo == 36000000) {
    lcd.home();
    float h = dht.readHumidity();
    float t = dht.readTemperature();
   
  // Print our characters on the LCD
    lcd.setCursor(0,0); //Start at character 0 on line 0
    lcd.print("T=");
    lcd.setCursor(2,0); //Start at character 0 on line 0
    lcd.print(t);
    lcd.setCursor(9,0); //Start at character 0 on line 1 
    lcd.print("H=");
    lcd.setCursor(11,0); //Start at character 0 on line 0
    lcd.print(h);
    tempo=0;
  //}

}

Am i doing something wrong or else have you got any guideline on how to achieve this? TIA

VAlerio

as it seems that lcd.print will need to print continuosly.

Why? You only need to call lcd.print() when you have something new to print.

Setting a delay will bother the ethernet connection

No, it won't. It'd pointless (see the blink without delay example), but it could work. You need to separate the "deal with client" code from the "show temp and status" code. They are completely separate activities. One needs to be done on every pass through loop(). The other doesn't.

Thaks for your comment PAul.
Actually the code works satisfactory, i just sent the post too quickly and my tests did not take into account that if we are in the ethernet loop, nothing will get printed on the lcd. after i reset the connection, the temperature showed up on the lcd.

so bottom line is that i can live with this, however my question is: what would be my approach if i wanted the two activities to be dealt with in a time-share fashion? just code the two activities one after the otehr hoping that either of them does not take too much operations to disrupt the workflow ot the other?

TIA

Regards,

Valerio

just code the two activities one after the otehr hoping that either of them does not take too much operations to disrupt the workflow ot the other?

I think "hoping" is the wrong word choice. "Planning" is a better choice.

PaulS:
I think "hoping" is the wrong word choice. "Planning" is a better choice.

yeah, wrong wording.
my point is that obviously my code does not do any of that: the ethernet code will keep busy the microcontroller for as long as the connection is up.

I understand that this may fall way beyond my knowledge, but my question is: how would i approach this kind of requirement in general?

my point is that obviously my code does not do any of that: the ethernet code will keep busy the microcontroller for as long as the connection is up.

True, but the "connection is up" only as long as it takes to read the client request and generate the response.

PaulS:
"connection is up" only as long as it takes to read the client request and generate the response.

hmm not so sure that i get you right:

if (client == true){

will be true until the connection is up. that was empyrically tested. so way longer than reading a request/generate response.

lordvee:
I understand that this may fall way beyond my knowledge, but my question is: how would i approach this kind of requirement in general?

You need to design your code to be non-blocking i.e. so that it does not stop (block) and wait for external events to happen. Instead, it tests whether expected events have occurred and handles them as and when they occur. This implies that your sketch has some way to know what state it is in so that it can know what events are expected and handle them appropriately. Because of this, non-blocking designs often use a finite state machine approach to control sequences of events and actions, where a blocking approach would use simple sequential code.

Hi,
Tks for the input :slight_smile: