can text be entered on TFT while a sketch is running

I tried to ask this question in another forum, but without success. Being that is involves displays as much as anything else, maybe this is the best place to ask.

If I have a sketch running, say reporting a changing temperature on line 2 of the display, and I want to enter real time text into line 1, can I do this without having to stop the program and recode the sketch ?

How else would you do it?

This is a very general question regarding programming, and an important concept to master.

The whole purpose of the "loop" in the program is to execute a sequence of (potentially) separate operations, each of which works without any "wait" or "delay" element (including "while" loops). Whatever each function (which may be coded as a function or as a "straight" segment of code) cannot do immediately having tested for what it needs to do, it exits in favour of the following function and when all others have had their turn, then this particular function will again be entered and again test for what - if anything - it can now do.

In your situation, you decide on how often your temperature needs to be measured. Your code for this then reads the "system clock" (a function called "millis()") and determines whether the appropriate interval has passed in which case it performs whatever parts of the display function can be executed with no implicit further delay (and also determines the next clock reading for which to wait). The important thing is that if the necessary time has not passed, it will simply cede to the next process without delay. And if the corresponding action can not be performed in one swift move, then it must be broken down into parts and the procedure must keep a tally called a "state variable" as to which is the next step to be executed on the next pass.

Your text entry will function in the same way. On each pass through that procedure, you "poll" whatever input device you are using - such as the serial port - for waiting information. If there is none, you pass on, if a character is waiting, then you process that character (just the one at a time) and move on. In some case, it might be that the output device is not ready to receive the character, in which case you need to provide a buffer and separate the two processes of placing characters into the buffer and transferring them from the buffer to the output device though these processes would reasonably be successive in the chain.

The important thing is that each of these steps is completed extremely quickly, so that the whole chain always loops so fast that each function appears to be performed independently and simultaneously as long as no function breaks the rule of causing a delay.

And to give you an idea of how this happens as well as a structure for performing such delays and de-bouncing switches, push-buttons or keys, here is some code to flash (many) LEDs:

// Blink without "delay()" - multi!

const int led1Pin =  13;    // LED pin number
const int led2Pin =  10;
const int led3Pin =  11;
const int button1 =  4;
int led1State = LOW;        // initialise the LED
int led2State = LOW;
int led3State = LOW;
char bstate1 = 0;
unsigned long count1 = 0;   // will store last time LED was updated
unsigned long count2 = 0;
unsigned long count3 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check 
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);      
  pinMode(led2Pin, OUTPUT);      
  pinMode(led3Pin, OUTPUT);      
  pinMode(button1, INPUT);      
  digitalWrite(button1,HIGH);        // internal pullup all versions
}

void loop() {
  // Toggle LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    if (led1State == LOW) {
      led1State = HIGH;
    }
    else {
      led1State = LOW; 
    } 
    digitalWrite(led1Pin, led1State);
  } 

  // Act if the latter time (ms) has now passed on this particular counter,
  if (timeout(&count2, 300UL )) {
    if (led2State == LOW) {
      led2State = HIGH;
    }
    else {
      led2State = LOW; 
    } 
    digitalWrite(led2Pin, led2State);
  } 

  if (timeout(&count3, 77UL )) {
    if (led3State == LOW) {
      led3State = HIGH;
    }
    else {
      led3State = LOW; 
    } 
    digitalWrite(led3Pin, led3State);
  } 
}

PB - Thank you very much for taking the time to make that explanation. While I can't say that I completely understand your code, I do somewhat understand looping in a sketch, and something of the interrupt concept (which i used to stop a flashing LED when a button is pushed).

But I was thinking along a different line. I am feeling around in the dark here, but my goal is something like this :

On a TFT screen from a touch screen keypad I type in "#12 wire with a 50k resistor" which is to be displayed on the top line. Then I hook up the wires and I run an arduino sketch that measures the current and displays the results on the second line. I stop the test & program and type in new text on the top line "#12 wire with a 20k resistor", wire it up, then run the test over again.

The test title and current data for each test run could go to an sd card. The title (top line), which tells about the test parameters, needs to be recorded along with the output data, so that a later review makes sense. But if a non-arduino programmer is going to use this test setup, they can't be required to open up a sketch and change the code.

They should be able to key in the new test parameters, hook it up, hit record and run the test. For a whole battery of conditions.

The sd card and recording the data are a future task. But I am trying to get started on what might work for the text entry on line one, and data displayed on line two. Get me ?