Rectangle not showing (TouchScreen & TFT)

So I'm making this project, and nothing I draw shows up! It's just white!

Here's the code:

#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h>

const int XP = 8, XM = A2, YP = A3, YM = 9; //320x480 ID=0x9486
const int TS_LEFT = 138, TS_RT = 911, TS_TOP = 929, TS_BOT = 145;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


MCUFRIEND_kbv tft;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF


int pixel_x, pixel_y;
void setup() {
  tft.reset();
  tft.setRotation(1);
  tft.fillRect(0, 0, 20,200, BLACK);
  Serial.begin(9600);
  

}

void loop() {
  TSPoint p = ts.getPoint();



  tft.drawCircle(1, 180, 20, BLACK);

  Serial.print("\nX:");
  Serial.print(p.x);
  Serial.print("\tY:");
  Serial.print(p.y);


}

In this line of code:

tft.fillRect(0, 0, 20,200, BLACK);

The rectangle does not appear.

When I run the library test, That works, but this doesn't!

I am using Apple M1

And here is a link to info about my TFT screen

That was the link, not this one (don't click it, its useless)

Thanks for the help!

Two very important points:

  1. you must call tft.begin(ID); in setup()

  2. you must always restore XM, YP pins to OUTPUT after calling ts.getPoint();

Start with the examples. Yes, it is a good idea to copy an example to your user scripts. Then simplify it as part of the learning process. Just do it in small stages.

David.

1 Like

Thanks David,

You seem to help a lot with other people as well, so from the newbies in Arduino, Thanks

Just two questions:

1: how do I find my ID

2: for

Do you mean like:

pinMode(XM,OUTPUT)
pinMode(YP,OUTPUT)

after ts.getPoint()

Thanks a lot!

@david_prentice
I now tried that, but it still doesn't work!

here is my updated code:

#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h>

const int XP = 8, XM = A2, YP = A3, YM = 9; //320x480 ID=0x9486
const int TS_LEFT = 138, TS_RT = 911, TS_TOP = 929, TS_BOT = 145;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


MCUFRIEND_kbv tft;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF


int pixel_x, pixel_y;
void setup() {
  
  int ID = tft.readID();
  tft.begin(ID);
  tft.reset();
  tft.setRotation(1);
  tft.fillRect(100, 0, 20,200, BLACK);
  Serial.begin(9600);
  Serial.println(tft.readID());
  

}

void loop() {
  TSPoint p = ts.getPoint();

  pinMode(XM,OUTPUT);
  pinMode(YP,OUTPUT);



  tft.drawCircle(1, 180, 20, BLACK);

  

}
void setup() {
  
  int ID = tft.readID();
  tft.begin(ID);
  //tft.reset();   //.kbv you call reset BEFORE begin
  tft.setRotation(1);
  tft.fillRect(100, 0, 20,200, BLACK);
  Serial.begin(9600);
  Serial.println(tft.readID());
}

My general advice is to run all of the library examples before attempting to write anything yourself.

If you call reset() after begin() you need to call begin() again.
In general you never call reset(). The begin() function calls it internally (if necessary).

Yes, you have done the pinMode() correctly.
It is pointless calling getPoint() if you don't use the result e.g. Serial.print() or map() it to the pixel coordinates.

David.

1 Like

Hi @david_prentice,

Thanks a lot!

It finally works!

I have had this LCD for 6 months and couldn't manage to get it working!

However, I do have another problem.

This is my code:

#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h>

const int XP = 8, XM = A2, YP = A3, YM = 9; //320x480 ID=0x9486
const int TS_LEFT = 138, TS_RT = 911, TS_TOP = 929, TS_BOT = 145;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


MCUFRIEND_kbv tft;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF


int pixel_x, pixel_y;
void setup() {
  
  tft.begin(38022);
  
  tft.fillRect(0, 0, 20,200, BLACK);
  Serial.begin(9600);
  Serial.println(tft.readID());
  

}

void loop() {
  TSPoint p = ts.getPoint();

  pinMode(XM,OUTPUT);
  pinMode(YP,OUTPUT);

  int px= map(p.x, TS_LEFT, TS_RT, 0, tft.width()); 
  int py = map(p.y, TS_TOP, TS_BOT, 0, tft.height());

  tft.fillCircle(px,py, 10,BLACK);

  

}

I am trying to make a paint program.

I mean, it does sort of work, I can draw with it.

But, every now and then, random circles appear, and I'm not quite sure why. Here is a picture of what happens:

I have tried to circle them, but it's not that visible.

Thanks in advance,
sdsarduino

Such is life. You will always get the occasional random "touch". It is a real-life device.

You can take several samples. And reject unusual points.

Look at the Touch_shield_new.ino example. That turns into a Paint program. No effort is made to reject "unlikely" touches.

David.

1 Like

Sorry @david_prentice

It works!

I just needed to make sure that the person was actually touching the screen! I forgot to update you.

There is a tiny, tiny, tiny problem though, The further I go away from 0, 0 on the Y-axis, the mapping becomes more inaccurate.

Thanks a lot though!

P.S: did you get Rick-Rolled on the first post?

Run the Calibration sketch. Copy-paste the results into Touch_shield example. I would expect perfect position of each touch.

Shields and Touch panels vary. You need to calibrate for accuracy.

For simple big buttons you can get away with just guessing. e.g. run the Paint, draw horiz line. If left and right are wrong, swap left annd right values. If appears as vertical, swap x, y.

1 Like

Thank You Sooo Much @david_prentice!

It finally works!

Here is my updated code (In case anyone wants it):

#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h>

const int XP = 8, XM = A2, YP = A3, YM = 9; //320x480 ID=0x9486
const int TS_LEFT = 911, TS_RT = 138, TS_TOP = 135, TS_BOT = 929;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


MCUFRIEND_kbv tft;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define LIME    0x07E0
#define GREEN   0x3b03
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define BROWN   0x80a0
#define ORANGE  0xf980

int colour = RED;
int pixel_x, pixel_y;
void setup() {

  tft.begin(38022);


  Serial.begin(9600);
  Serial.println(tft.readID());

  tft.fillScreen(WHITE);


  //drawing buttons, yay
  tft.fillRect(0, 0, 32, 32, BLACK);
  tft.drawRect(0, 0, 32, 32, BLACK);

  tft.fillRect(32, 0, 32, 32, WHITE);
  tft.drawRect(32, 0, 32, 32, BLACK);

  tft.fillRect(64, 0, 32, 32, RED);
  tft.drawRect(64, 0, 32, 32, BLACK);

  tft.fillRect(96, 0, 32, 32, ORANGE);
  tft.drawRect(96, 0, 32, 32, BLACK);

  tft.fillRect(128, 0, 32, 32, YELLOW);
  tft.drawRect(128, 0, 32, 32, BLACK);

  tft.fillRect(160, 0, 32, 32, LIME);
  tft.drawRect(160, 0, 32, 32, BLACK);



  tft.fillRect(192, 0, 32, 32, CYAN);
  tft.drawRect(192, 0, 32, 32, BLACK);

  tft.fillRect(224, 0, 32, 32, BLUE);
  tft.drawRect(224, 0, 32, 32, BLACK);

  tft.fillRect(256, 0, 32, 32, MAGENTA);
  tft.drawRect(256, 0, 32, 32, BLACK);

  tft.fillRect(288, 0, 32, 32, BROWN);
  tft.drawRect(288, 0, 32, 32, BLACK);

  /*
    drawing size wheel

    tft.setCursor(320,440);
    tft.setTextSize(2);
    tft.setTextColor(BLACK);
    tft.print("Size");


    tft.drawRoundRect(55,450,30,30,3,BLACK);
  */
}

void loop() {


  TSPoint p = ts.getPoint();



  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);

  double Ssize = 1;
  int size = 0;
  bool pressed = (p.z > 200 && p.z < 1000);
  if (pressed) {

    int  px = map(p.x, 133, 916, 0, 320);
    int py = map(p.y, 934, 92, 0, 480);


    if (p.x < 32 && p.y < 32) {
      colour = BLACK;

      tft.fillRect(0, 0, 32, 32, BLACK);
      tft.drawRect(0, 0, 32, 32, BLACK);
    }

    if (p.z > 800) {
      size = 2;
      Serial.println("Drew Circle of r 5");
    }
    if (p.z > 600 && p.z < 800) {
      size = 3 * Ssize;
    }
    if (p.z > 400 && p.z < 600) {
      size = 5 * Ssize;
    }
    if (p.z > 200 && p.z < 400) {
      size = 7 * Ssize;
    }
    if (p.z > 200 && p.z < 400) {
      size = 9 * Ssize;
    }


    tft.fillCircle(px, py, size , colour);



  }

}

If anyone else has the same problem, and that's why you are looking at this post, then look at all of David's Posts, It'll help.

Thanks Again David!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.