text on screen moves position after first loop

Ok, so. I am having issues displaying a line of text on my screen.

I have a water level sensor/switch that I am detecting HIGH/LOW on the pin and displaying LOW / NORMAL on the display.

links to images - https://ibb.co/dqYioJ

It is hard to tell from the images, but if you look closely enough. One image (the one zoomed in a little closer) is what is displayed after the first loop iteration. Notice that the top of the text does not match the top of the “73” on the temp reading. The other image is aligned how I expected it to be after I toggle the sensor/switch. The top of the text is in line with the temp reading.

I’ve tried several things in an attempt to fix this. But the first time the loop() runs. The text is not displayed correctly. When I bump the switch to either off/on, the code then updates upon the next loop() and the text is displayed correctly from there on out.

I find it strange that the text is not aligned correctly on the first loop run, but every time it updates after that it works fine.

I’m not sure what all code to post. So I will post the pieces I know are in question.

There is where I am setting up my level at the top of the sketch

//float switch stuff
int switchpin = 32;
volatile int level = "START"; // init variable

Here is my setup. note the //CLEAR TANK SETTINGS portion of this was an attempt to fix this, it didn’t work

void setup(void)
{
    Serial.begin(9600);
    //display stuff
    uint16_t ID = tft.readID();
    if (ID == 0xD3) ID = 0x9481;
    tft.begin(ID);
    tft.setRotation(1);
    tft.fillScreen(BLACK);
    //pin modes
    pinMode(switchpin, INPUT);
    //CLEAR TANK SETTINGS - first loop is always 10-20 pixels below normal.
    homescreen_level(level);
    int x = 20;
    int y = 25;
    tft.fillRect(x , y - 20, 150, 30, BLACK);
    
}

Here is my main loop

void loop() {

  int chk = DHT11.read(DHT11PIN);
  int tempnow = Fahrenheit(DHT11.temperature);
  volatile int levelnow = digitalRead(switchpin); // low high

  //check and display temperature
  if(tempnow != temp){ 
    temp = tempnow;
    homescreen_temp(tempnow);
    printdegrees();
  }

  //check and display water level
  
  //check float switch 
  if(levelnow != level){
    level = levelnow;
    homescreen_level(levelnow);
  }
  
  delay(1000);
}

and here is the function to which I am displaying the TANK: level

void homescreen_level(int level){
  
  tft.setCursor(0,0);
  tft.setTextColor(GREEN,BLACK);
  int x = 20;
  int y = 25;
  tft.setCursor(x, y);
  tft.fillRect(x , y - 20, 150, 30, BLACK);
  tft.setFont(&FreeSans9pt7b);
  tft.print("TANK: ");
  if(level == 0){
    tft.print("LOW");
  }
  else{
    tft.print("NORMAL");
  }
  
}

Life is much simpler if you paste or attach the complete sketch.

It is not clear what you are complaining about.

Before printing something you need to setCursor() to specify where the printing should be located.
Subsequent text will appear in the next position. e.g. if I write "David" a subsequent "Prentice" will appear as "DavidPrentice"

It you have tft.setTextWrap(true) it will continue on the next line if a letter does not fit (default).
In practice this means that you use setCursor() once. All printing flows naturally.
You only set a new cursor position if you want to change position e.g. update a Sensor.

Note that FreeFonts are printed from the base line. The 7x5 System font is printed from the top line.

Hey-ho, that is how Adafruit_GFX works. Just live with it.

David.