Some problems with 2,2" SPI TFT

I purchased this TFT screen http://www.dx.com/p/2-2-serial-spi-tft-color-lcd-module-for-arduino-red-silver-234675
and i’m having some problems using it atm.

Here’s my code: [code]// This #include statement was automatically added by the Spark IDE.
#include “Adafruit_mfGFX/Adafruit_mfGFX.h”

// This #include statement was automatically added by the Spark IDE.
#include “Adafruit_ILI9341/Adafruit_ILI9341.h”

Adafruit_ILI9341 tft = Adafruit_ILI9341(A2, A1, A0);

    int i= 0;

void setup() {

tft.begin();

}

void loop() {

// testText();
i++;
tft.setRotation(1);
unsigned long start = micros();
tft.setCursor(50, 50);
tft.setTextColor(ILI9341_BLACK); tft.setTextSize(8);
tft.print(i);

    //return micros() - start;
delay(1000);
    tft.fillScreen(ILI9341_WHITE);

}

[/code]
Like this it displays i++ numbers correctly but they are blinking since after every loop cycle there is tft.fillScreen(ILI9341_WHITE);
Now if i remove tft.fillScreen(ILI9341_WHITE); then numbers start to overwrite themselves and i end up with black squares (previous numbers are not ‘deleted’ and new ones are printed on top).

What am i doing wrong? Or is there some problem with this library?

The problem here is that only the characters are drawn and not their background color.

So you do need the tft.fillScreen(ILI9341_WHITE); line, but I would put it directly before the tft.print(i); line to reduce the time between “clearing” the screen and reprinting and hence reduce flicker.

Some possible other solutions would be to set the text backcolor or to use double buffering - but I’m not sure if this is available in this display lib.

I think @ScruffR is right here–the display is doing what you told it to do, just not what you want. His solution is a good idea but I have two other points:

  • You don’t have to fill the entire screen with white, you can just draw a white rectangle that covers the text area before writing a new number
  • You can also draw the old value in white to erase it back to background and then draw the new value in black. This simulates an XOR drawing mode which this display does not seem to have.
  tft.setTextColor(ILI9341_WHITE);
  tft.setCursor(50,50);
  tft.print(i);  //erase old
  i++;
  tft.setTextColor(ILI9341_BLACK);
  tft.setCursor(50,50);
  tft.print(i);  //print new

The rectangle method may be faster since you don’t have to render fonts–it depends on the controller that is being used.

1 Like

That’s right, I totaly ignored the rect-route since when I had a similar problem with my RGB-LED matrix that also uses Adafruit_mfGFX lib I found that filling the entire screen white or black (or some other special colors) via memcpy it was way faster than rect, but this was only due to some peculiarities of that display, which don’t play any role for your display :blush:

After trying all the options is stumbled across this while googling:

[quote]Folks,

Y’all making same mistake (for what you are trying to do) - providing single color argument.

If
you carefully read API notes you will notice that tft.setTextColor can
take either one or two arguments. If single argument provided - it will
print text color with clear background (junk pixels will accumulate). If
two arguments provided - it will fill background of each character with
color of second argument.

Nice and simple. No need for any blank space overwrite before new print.[/quote]

This option works best for me: tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);

1 Like

That was what I meant with this :wink:

But with my display this was slower than doing a screen fill, but with yours I’d guess this will be faster than going the rect-route.