I am aware about the subject as two planes of conductive material separated by an inert layer and touching makes contact at that point which is read as a X,Y co-ordinate. In one such shield they are defining one pair X,Y pins as Analog and the next pair X1,Y1 as digital pins. Would like to know how this one works ? Any link to an article will help !
Also in the code below if I want to make the orientation as landscape what changes need to be done ? I tried some but ended up totally confused ![]()
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#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
// For 0 Rotation ( Portrait mode )
#define TS_MINX 125 // Right
#define TS_MAXX 910 // Left
#define TS_MINY 70 // Top
#define TS_MAXY 910 // Bottom
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 330); // Actually measured . RR_17Aug2020
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#define BOXSIZE 40
#define PENRADIUS 2
int oldcolor, currentcolor;
void setup(void) {
Serial.begin(9600);
Serial.println(F("Paint!"));
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.setRotation(0);
tft.fillScreen(BLACK);
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED); // For 0 Rotation, Top Left is 0,0. Bottom right is 240 x 320
tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW); // x,y,w,h,c
tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
currentcolor = RED;
pinMode(13, OUTPUT);
}
#define MINPRESSURE 200
#define MAXPRESSURE 1000
void loop()
{
TSPoint p = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
// We have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
//Serial.print("X = "); Serial.print(p.x); // These are the touch co-ordinates
//Serial.print("\tY = "); Serial.print(p.y);
//Serial.print("\tPressure = "); Serial.println(p.z);
if (p.y > (TS_MAXY - 25)) { // Corrected and works fine !
Serial.println("erase");
// press the bottom of the screen to erase
tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK);
}
// scale from 0->1023 to tft.width
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
//p.x = tft.width()-map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = (tft.height() - map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));
//p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
//Serial.print("("); Serial.print(p.x); // These are the Pixel co-ordinates
//Serial.print(", "); Serial.print(p.y);
//Serial.println(")");
if (p.y < BOXSIZE) {
oldcolor = currentcolor; // Load the touched colour...
if (p.x < BOXSIZE) { // Sorround the touched colour with white border
currentcolor = RED;
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
} else if (p.x < BOXSIZE * 2) {
currentcolor = YELLOW;
tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
} else if (p.x < BOXSIZE * 3) {
currentcolor = GREEN;
tft.drawRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, WHITE);
} else if (p.x < BOXSIZE * 4) {
currentcolor = CYAN;
tft.drawRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, WHITE);
} else if (p.x < BOXSIZE * 5) {
currentcolor = BLUE;
tft.drawRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, WHITE);
} else if (p.x < BOXSIZE * 6) {
currentcolor = MAGENTA;
tft.drawRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, WHITE);
}
}
if (((p.y - PENRADIUS) > BOXSIZE) && ((p.y + PENRADIUS) < tft.height())) { // Trace the styus in the chosen colour !
tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
}
}
}