numbers overwitting on tft

Hi to all,
I have created a new project stitching codes from here and there. I don’t know if it’s messy but it seems to run pretty well. I ended up with an overwriting problem. I can bypass it by drawing a black rectangle on the screen, but like this, I go back to flickering problems. The “ILI9341_BLACK” part of the command tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK) seems that it doesn’t work. I put the specific code here but I attach the whole code for better review as well as an image of the screen. Any solution will be appreciated. Thank you.

    /*elev. calculations*/
    Changed = true;

    if (Changed)
    {
      if (dive >= 1000) {
        tft.setCursor(55, 187); tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK); tft.print(dive / 1000.0, 1);
      }
      if (dive < 8)
      {
        tft.setCursor(235, 187);
      }
      else if (dive < 98) {
        tft.setCursor(160, 187);
      }
      else if (dive < 998) {
        tft.setCursor(85, 187);
      }
      tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK); tft.print(dive);

      Changed = false;
    }

hike.ino (9.48 KB)

I can bypass it by drawing a black rectangle on the screen

That is what one does.

To avoid flicker, update the screen only when necessary.

Can’t read the .ini from my cell phone.

What’s dive’s type?
If it’s > 1000 you are Printing a float with 1 decimal and if not you are printing just that variable. So the display format might vary

Erasing some text could be done by printing in reverse color the same value at the same exact location

The usual way of solving the problem is indeed to overwrite the previous value before printing the new one.

To avoid flickering as far as possible only write to the screen when a value changes, only erase that value, write any fixed text only once in the program, if possible only set the font size and text colour once in the program, do not erase and write the whole screen again, if a value changes by a small amount (particularly a float) do not print it until it changes by a significant amount

If you have done all that then you have probably done as much as you can

J-M-L:
Can’t read the .ini from my cell phone.

What’s dive’s type?
If it’s > 1000 you are Printing a float with 1 decimal and if not you are printing just that variable. So the display format might vary

Erasing some text could be done by printing in reverse color the same value at the same exact location

dive it's an integer.

So sometimes you print it as a float and sometimes as an int?

J-M-L:
So sometimes you print it as a float and sometimes as an int?

yes, that's true.

So have you tried printing the former value at the very same position but in black over a black background before painting the new value?

J-M-L:
So have you tried printing the former value at the very same position but in black over a black background before painting the new value?

yes, I did. And it's not good. If the result has one digit, is ok. If the result has two digits, also ok. But from two digits back to one digit, there comes the problem.

But from two digits back to one digit, there comes the problem.

That’s probably not the way you do it. You memorize the OLD value somewhere. When it comes to changing the value (ie the NEW value is different than the OLD one), then you first write again the OLD one in black to clean up and then you write the NEW one in white, and you store NEW in OLD for next time.

void displayTemparature(int t)
{
  static int oldTemp = -1000; // start with something impossible
  if (oldTemp != -1000) {    // we have something to erase
    tft.setCursor(55, 187);
    tft.setTextColor(ILI9341_BLACK, ILI9341_BLACK); // black on black to erase
    tft.print(oldTemp);// erase old temperature 
  }
  if (t != oldTemp) { // no display needed otherwise, save time, no blink
    tft.setCursor(55, 187);
    tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK); // white on black to display
    tft.print(t); // display new temperature 
    oldTemp = t; // memorize it for erasing later
  }
}

J-M-L:
That’s not the way you do it. You memorize the OLD value somewhere. When it comes to changing the value (ie the NEW value is different than the OLD one), then you first write again the OLD one in black to clean up and then you write the NEW one in white, and you store NEW in OLD for next time.

sorry, I should have uploaded my last test code. When everything is under 8, it's fine. When it comes over it, everything is ok also. But when it goes from values more than 8 to under 8, here is the problem (from 2 digits to one digit, or from 3 to 2). Apparently, the 2nd part "ILI9341_BLACK" of the command "tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK)" which would have solved the problem, doesn't work either.

  if (dive <8)
  { 
    if (divenew != dive) {
    tft.setFont(&xlmonoalt34pt7b);
    tft.setTextSize(4);
    tft.setCursor(235, 187);
    tft.setTextColor(ILI9341_BLACK);
    tft.print(divenew);
    tft.setCursor(235, 187);
    tft.setTextColor(ILI9341_WHITE);
    divenew = dive;
    tft.print(divenew); 
  } 
 }
  else if (dive < 98)
  {
    if (divenew != dive) {
    tft.setFont(&xlmonoalt34pt7b);
    tft.setTextSize(4);
    tft.setCursor(160, 187);
    tft.setTextColor(ILI9341_BLACK);
    tft.print(divenew);
    tft.setCursor(160, 187);
    tft.setTextColor(ILI9341_WHITE);
    divenew = dive;
    tft.print(divenew);
    }
 }

I don’t get it. Is dive the new value to display or is it diveNew?

You need to erase based on the old value then display the new one and save it.

J-M-L:
I don’t get it. Is dive the new value to display or is it diveNew?

You need to erase based on the old value then display the new one and save it.

divenew

So why do you erase with diveNew

    tft.setCursor(235, 187);
    tft.setTextColor(ILI9341_BLACK);
    tft.print(divenew); // shouldn’t that be dive

and dive should be used to memorize the old value, so not divenew = dive;

J-M-L:
So why do you erase with diveNew

    tft.setCursor(235, 187);

tft.setTextColor(ILI9341_BLACK);
   tft.print(divenew); // shouldn’t that be dive



and dive should be used to memorize the old value, so not divenew = dive;

sorry, it doesn't work, either.

it doesn't work, either.

What does "it" do ?

Please post your full revised code

UKHeliBob:
What does "it" do ?

Please post your full revised code

just to to be more understandable, I changed the "divenew" into "diveold":

int diveold = 0;
.
.
 if (dive <8)
  {
    if (diveold != dive) {
    tft.setFont(&xlmonoalt34pt7b);
    tft.setTextSize(4);
    tft.setCursor(235, 187);
    tft.setTextColor(ILI9341_BLACK);
    tft.print(diveold);
    tft.setCursor(235, 187);
    tft.setTextColor(ILI9341_WHITE);
    tft.print(dive);
    diveold = dive;
  }
 }
  else if (dive < 98)
  {
    if (diveold != dive) {
    tft.setFont(&xlmonoalt34pt7b);
    tft.setTextSize(4);
    tft.setCursor(160, 187);
    tft.setTextColor(ILI9341_BLACK);
    tft.print(diveold);
    tft.setCursor(160, 187);
    tft.setTextColor(ILI9341_WHITE);
    tft.print(dive);
    diveold = dive;
    }
 }

Why is the different cursor positioning based on the new value of dive before writing diveold in black ? Surely the positioning should depend on the value of diveold

UKHeliBob:
Why is the different cursor positioning based on the new value of dive before writing diveold in black ? Surely the positioning should depend on the value of diveold

dive value should always appear on the screen.

johntikis:
dive value should always appear on the screen.

Yes, but if you write the value of diveold in black using cursor positioning based on the value of dive won't it sometimes be in the wrong place ?