Introduction: 2.4 TFT LCD Display + Arduino Code Fixed

I'm recently experimenting the 2.4" TFT LCD Display with Arduino, I bought the LCD not intended to do something but just want to know if it work or not. I searched the instructables, youtube but a lot of code didn't work, so I proceed an intensive case study to check it why, maybe my version of board differs, I start amend the existing code and finally got a breakthrough and hopefully can highlight to those others who are facing the same problem.

The original code name called tftpaint.ino, a simple online search can easily found it, I only highlighting the changes I made here to get it work.

1. Some code have used the library of SWTFT.h cannot work on my board, I change back to Adafruit_TFTLCD.h.

2. Corrected the define pins as

#define YP A3 // must be an analog pin, use "An" notation!

#define XM A2 // must be an analog pin, use "An" notation!

#define YM 9 // can be a digital pin

#define XP 8 // can be a digital pin

3. Re-allocated the anchor point for color blocks, seeming the XY coordinates of my board is differ

tft.fillRect(BOXSIZE*5, BOXSIZE*0, BOXSIZE, BOXSIZE, RED);
tft.fillRect(BOXSIZE*4, BOXSIZE*0, BOXSIZE, BOXSIZE, YELLOW);

tft.fillRect(BOXSIZE*3, BOXSIZE*0, BOXSIZE, BOXSIZE, GREEN);

tft.fillRect(BOXSIZE*2, BOXSIZE*0, BOXSIZE, BOXSIZE, CYAN);

tft.fillRect(BOXSIZE*1, BOXSIZE*0, BOXSIZE, BOXSIZE, BLUE);

tft.fillRect(BOXSIZE*0, BOXSIZE*0, BOXSIZE, BOXSIZE, MAGENTA);

Step 1: Change the Coordinates

4. Changed the point scale as stated

p.x = tft.width()-(map(p.x, TS_MINX, TS_MAXX, 0, tft.width()));

p.y = tft.height()-(map(p.y, TS_MINY, TS_MAXY, 0, tft.height()));

5. Amended the coordinates to display blocks and colors

if (p.x > BOXSIZE * 5) {

currentcolor = RED;

tft.drawRect(BOXSIZE * 5, BOXSIZE*0, BOXSIZE, BOXSIZE, WHITE);

} else if (p.x > BOXSIZE * 4) {

currentcolor = YELLOW;

tft.drawRect(BOXSIZE * 4, BOXSIZE*0, BOXSIZE, BOXSIZE, WHITE);

} else if (p.x > BOXSIZE * 3) {

currentcolor = GREEN;

tft.drawRect(BOXSIZE*3, BOXSIZE*0, BOXSIZE, BOXSIZE, WHITE);

} else if (p.x > BOXSIZE * 2) {

currentcolor = CYAN;

tft.drawRect(BOXSIZE*2, BOXSIZE*0, BOXSIZE, BOXSIZE, WHITE);

} else if (p.x > BOXSIZE * 1) {

currentcolor = BLUE;

tft.drawRect(BOXSIZE*1, BOXSIZE*0, BOXSIZE, BOXSIZE, WHITE);

} else if (p.x > BOXSIZE * 0) {

currentcolor = MAGENTA;

tft.drawRect(BOXSIZE * 0, BOXSIZE * 0, BOXSIZE, BOXSIZE, WHITE);

}

if (oldcolor != currentcolor) {

if (oldcolor == RED) tft.fillRect(BOXSIZE*5, BOXSIZE*0, BOXSIZE, BOXSIZE, RED);

if (oldcolor == YELLOW) tft.fillRect(BOXSIZE*4, BOXSIZE*0, BOXSIZE, BOXSIZE, YELLOW);

if (oldcolor == GREEN) tft.fillRect(BOXSIZE*3, BOXSIZE*0, BOXSIZE, BOXSIZE, GREEN);

if (oldcolor == CYAN) tft.fillRect(BOXSIZE*2, BOXSIZE*0, BOXSIZE, BOXSIZE,CYAN);

if (oldcolor == BLUE) tft.fillRect(BOXSIZE*1, BOXSIZE*0, BOXSIZE, BOXSIZE, BLUE);

if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE*0, BOXSIZE*0, BOXSIZE, BOXSIZE, MAGENTA);

}

}

if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {

tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);

}

Step 2: Finally Done

Last step to amend the clear screen code

if (p.y > TS_MAXY-30) {
Serial.println("erase"); // press the bottom of the screen to erase

tft.fillRect(0, BOXSIZE*1, tft.width(), tft.height()-BOXSIZE*1, BLACK);

}