TFT touchscreen problems

I am trying to understand how to use the touchscreen on my TFT. I have a kuman 3.5" tft, and am using it on an Arduino mega. I can get the graphics to work, but I am having trouble getting my screen calibrated. I seem to be getting random numbers when i touch the screen. I have run the MCUFRIEND touchscreen calibration and have input the values of that into my program (which i assembled from other examples)

this is my code:

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin


#include <SPI.h>          // f.k. for Arduino-1.5.2
#include "Adafruit_GFX.h"// Hardware-specific library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
//#include <Adafruit_TFTLCD.h>
const int XP=6,XM=A2,YP=A1,YM=7; //ID=0x9341
const int TS_LEFT=907,TS_RT=136,TS_TOP=942,TS_BOT=139;


TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define MINPRESSURE 200
#define MAXPRESSURE 1000
TSPoint tp;


// 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


int x = 0;
int y = 0;


#ifndef min
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif








void setup() {
 // put your setup code here, to run once:
Serial.begin(9600);
uint16_t ID = tft.readID();  //begin the tft
   if (ID == 0xD3D3) ID = 0x9481; // write-only shield
//    ID = 0x9329;                             // force ID
   tft.begin(ID);
   tft.fillScreen(BLACK);
   tft.setRotation(0);
   tft.setCursor(10, 50);
tft.fillRect(75, 175, 50, 50, GREEN);
}




void loop() {
 // put your main code here, to run repeatedly:




tp = ts.getPoint(); //get data
long(tp.x);
long(tp.y);
int x = map(tp.x, 110, 960, 0, 320);
int y = map(tp.y, 954, 85, 0, 480);
Serial.println (tp.y);
Serial.println(y);
Serial.println("done");
delay(5000);
}

\

what im trying to do is to get the individual values from the function ts.getPoint(). I then try to map the values onto the screen resolution, but that is where I am getting my problems. Sometimes i get negative numbers, sometimes I get numbers far greater than 480. I am probably getting values that are larger than the 954 low but those are the values i got for calibrating my touchscreen using the MCUFRIEND calibration.

I dont understand what is going wrong.

I added code tags for you, select More:Modify and clean up all the blank lines.

Have you tried the MAP function, it may do exactly what you need. Rather then explain it, you can look it up on this website, there are instructions much better then i could give you and yes it works with negative numbers.

This topic was automatically closed after 77 days. New replies are no longer allowed.