Here is my code to Display lines and rects on screen and to read Touch with integrated touch signals to prevent missfunction by spikes.
//LCD_Reset cut and direct connected to Arduino Reset to save one pin
#define LCD_RD D14
#define LCD_WR D15
//#define LCD_RS_TouchAinYA D16 //RS, Touch aiY also analog in not working on UNOQ
#define LCD_RS_Touch_XA D11 //bridged between D11 and A2, so digital keeps digital and analog keeps analog the whole time
//#define LCD_CS_TouchAinXA D17 //CS, Touch aiX also analog in not working on UNOQ
#define LCD_CS_Touch_YA D10 //bridged between D10 and A3, so digital keeps digital and analog keeps analog the whole time
#define LCD_D0_Touch_XB D8 //D0, Touch Y
#define LCD_D1_Touch_YB D9 //D1, Touch X
#define LCD_D2 D2
#define LCD_D3 D3
#define LCD_D4 D4
#define LCD_D5 D5
#define LCD_D6 D6
#define LCD_D7 D7
#define LCD_Touch_Ain_XA A2
#define LCD_Touch_Ain_YA A3
//define Colors
#define ILI9341_BLACK 0x0000 /* 0, 0, 0 */
#define ILI9341_NAVY 0x000F /* 0, 0, 128 */
#define ILI9341_DARKGREEN 0x03E0 /* 0, 128, 0 */
#define ILI9341_DARKCYAN 0x03EF /* 0, 128, 128 */
#define ILI9341_MAROON 0x7800 /* 128, 0, 0 */
#define ILI9341_PURPLE 0x780F /* 128, 0, 128 */
#define ILI9341_OLIVE 0x7BE0 /* 128, 128, 0 */
#define ILI9341_LIGHTGREY 0xC618 /* 192, 192, 192 */
#define ILI9341_DARKGREY 0x7BEF /* 128, 128, 128 */
#define ILI9341_BLUE 0x001F /* 0, 0, 255 */
#define ILI9341_GREEN 0x07E0 /* 0, 255, 0 */
#define ILI9341_CYAN 0x07FF /* 0, 255, 255 */
#define ILI9341_RED 0xF800 /* 255, 0, 0 */
#define ILI9341_MAGENTA 0xF81F /* 255, 0, 255 */
#define ILI9341_YELLOW 0xFFE0 /* 255, 255, 0 */
#define ILI9341_WHITE 0xFFFF /* 255, 255, 255 */
#define ILI9341_ORANGE 0xFD20 /* 255, 165, 0 */
#define ILI9341_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define ILI9341_PINK 0xF81F
struct touch
{
int iCount;
unsigned int uiLeft;
unsigned int uiRight;
unsigned int uiTop;
unsigned int uiBottom;
unsigned int uiDisplayX;
unsigned int uiDisplayY;
unsigned int uiDisplayWidth;
unsigned int uiDisplayHeight;
bool bJustTouched;
bool bJustReleased;
bool bCurrentTouched;
bool bLastTouched;
};
unsigned int uiTouchCountMax = 50;
unsigned int uiTouchCountReleased = 10;
unsigned int uiTouchCountPressed = 40;
//4 touch fields: 0 = upper left, 1 = upper right, 2 = lower left, 3 = lower right
touch LCD_Touch[4];
//= {0, left right top bottom, false, false, false, false}; //not just touched, not just released, not current touched, not touched on last cycle
//LCD
unsigned int uiLCD_Height = 240;
unsigned int uiLCD_Width = 320;
//any test variables
int itest = 0;
unsigned int iColor;
int iTouchY;
int iXPosition = 30;
int iBTN_1 = HIGH;
int iBTN_2 = HIGH;
int iPOTI_1 = 0;
int iPOTI_2 = 0;
int iButtonCounter_Debug = 0;
//Sample Timer
long lSampleTimer = 0;
long lSampleTimeBase = 6; //milliseconds/sample
long lSampleTimeBaseMin = 2;
long lSampleTimeBaseMax = 15;
long lTickerMeasure = 0;
long lCurrentTicks = 0;
long lTicksGone = 0;
void TouchScreen_read(void)
{
unsigned int uiPosition_X; //as Resolution of ADC = 0..1023
unsigned int uiPosition_Y; //as Resolution of ADC = 0..1023
// Set Rx free
pinMode(LCD_RS_Touch_XA, INPUT_PULLUP); //XA = Analog input, so put digital pin to Input and Pullup to provide parasite values
pinMode(LCD_D0_Touch_XB, INPUT); //XB = Digital input
digitalWrite(LCD_RS_Touch_XA, LOW); //XA no pullup
digitalWrite(LCD_D0_Touch_XB, LOW); //XB no pullup
// Set Y+ to VCC
pinMode(LCD_CS_Touch_YA, OUTPUT);
digitalWrite(LCD_CS_Touch_YA, HIGH);
// Set Y- to ground
pinMode(LCD_D1_Touch_YB, OUTPUT);
digitalWrite(LCD_D1_Touch_YB, LOW);
delay(2) //so sattle input signel
//alternative call any function like ButtonPointered(&Button_Sample); //Read Button, to give the input time to sattle
uiPosition_X = 1023 - analogRead(LCD_Touch_Ain_XA);
pinMode(LCD_RS_Touch_XA, OUTPUT);
pinMode(LCD_D0_Touch_XB, OUTPUT);
digitalWrite(LCD_RS_Touch_XA, HIGH); //send Data
// Set Ry free
pinMode(LCD_CS_Touch_YA, INPUT_PULLUP); //YA = Analog input, so put digital pin to Input and Pullup to provide parasite values
pinMode(LCD_D1_Touch_YB, INPUT); //YB = Digital input
digitalWrite(LCD_CS_Touch_YA, LOW); //YA no pullup
digitalWrite(LCD_D1_Touch_YB, LOW); //YB no pullup
// Set X+ to VCC
pinMode(LCD_RS_Touch_XA, OUTPUT);
digitalWrite(LCD_RS_Touch_XA, HIGH);
// Set X- to ground
pinMode(LCD_D0_Touch_XB, OUTPUT);
digitalWrite(LCD_D0_Touch_XB, LOW);
delay(2) //so sattle input signel
//alternative call any function like ButtonPointered(&Button_2); //Read Button, to give the input time to sattle
//LCD_Touch.iTouchPoint_Y = 1023 - analogRead(LCD_Touch_Ain_YA);
uiPosition_Y = analogRead(LCD_Touch_Ain_YA);
pinMode(LCD_CS_Touch_YA, OUTPUT);
pinMode(LCD_D1_Touch_YB, OUTPUT);
//look if touchposition is in any touchfield
for (int iField = 0; iField < 4; iField++)
{
LCD_Touch[iField].bJustTouched = false; //Clear last Touch
LCD_Touch[iField].bJustReleased = false; //Clear last Release
if((uiPosition_X >= LCD_Touch[iField].uiLeft) && (uiPosition_X <= LCD_Touch[iField].uiRight)
&& (uiPosition_Y >= LCD_Touch[iField].uiTop) && (uiPosition_Y <= LCD_Touch[iField].uiBottom))
{
//Touched in field
LCD_Touch[iField].iCount++; //inc Counter
if (LCD_Touch[iField].iCount > uiTouchCountMax) {LCD_Touch[iField].iCount = uiTouchCountMax;} //max uiTouchCountMax
}
else
{
//Touched not in field
LCD_Touch[iField].iCount--; //dec Counter
if (LCD_Touch[iField].iCount < 0) {LCD_Touch[iField].iCount = 0;} //min 0
}
//Pressed or released in field? else do nothing!
//pressed in field?
if (LCD_Touch[iField].iCount > uiTouchCountPressed)
{
//pressed in field
if (LCD_Touch[iField].bLastTouched == false)
{
// not touched before
LCD_Touch[iField].bJustTouched = true; // new touch
}
LCD_Touch[iField].bCurrentTouched = true;
}
//not pressed in field?
if (LCD_Touch[iField].iCount < uiTouchCountReleased)
{
//pressed in field
if (LCD_Touch[iField].bLastTouched)
{
// touched before
LCD_Touch[iField].bJustReleased = true; // new released
}
LCD_Touch[iField].bCurrentTouched = false;
}
LCD_Touch[iField].bLastTouched = LCD_Touch[iField].bCurrentTouched;
}
}
void LCD_Writ_Bus(unsigned char d)
{
digitalWrite(LCD_D0_Touch_XB, (((d) & B00000001) != 0)); //D0 ==> D8
digitalWrite(LCD_D1_Touch_YB, (((d) & B00000010) != 0)); //D1 ==> D9
digitalWrite(LCD_D2, (((d) & B00000100) != 0)); //D2 ==> D2
digitalWrite(LCD_D3, (((d) & B00001000) != 0)); //D3 ==> D3
digitalWrite(LCD_D4, (((d) & B00010000) != 0)); //D4 ==> D4
digitalWrite(LCD_D5, (((d) & B00100000) != 0)); //D5 ==> D5
digitalWrite(LCD_D6, (((d) & B01000000) != 0)); //D6 ==> D6
digitalWrite(LCD_D7, (((d) & B10000000) != 0)); //D7 ==> D7
digitalWrite(LCD_WR, LOW); //set WR
digitalWrite(LCD_WR, HIGH); //reset WR
}
/// Write Command to Bus
void LCD_Write_Com(unsigned char VH)
{
digitalWrite(LCD_RS_Touch_XA, LOW); //send Command
LCD_Writ_Bus(VH);
}
/// Write Data to Bus
void LCD_Write_Data(unsigned char VH)
{
digitalWrite(LCD_RS_Touch_XA, HIGH); //send Data
LCD_Writ_Bus(VH);
}
void LCD_Write_Com_Data(unsigned char com,unsigned char dat)
{
LCD_Write_Com(com);
LCD_Write_Data(dat);
}
void Address_set(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2)
{
LCD_Write_Com(0x2a);
LCD_Write_Data(x1>>8);
LCD_Write_Data(x1);
LCD_Write_Data(x2>>8);
LCD_Write_Data(x2);
LCD_Write_Com(0x2b);
LCD_Write_Data(y1>>8);
LCD_Write_Data(y1);
LCD_Write_Data(y2>>8);
LCD_Write_Data(y2);
LCD_Write_Com(0x2c);
}
void LCD_Init(void)
{
digitalWrite(LCD_CS_Touch_YA,HIGH);
digitalWrite(LCD_WR,HIGH);
digitalWrite(LCD_CS_Touch_YA,LOW); //CS
LCD_Write_Com(0xCB); //Power Control
LCD_Write_Data(0x39);
LCD_Write_Data(0x2C);
LCD_Write_Data(0x00);
LCD_Write_Data(0x34);
LCD_Write_Data(0x02);
LCD_Write_Com(0xCF); //Power Control
LCD_Write_Data(0x00);
LCD_Write_Data(0XC1);
LCD_Write_Data(0X30);
LCD_Write_Com(0xE8); //Timing Control
LCD_Write_Data(0x85);
LCD_Write_Data(0x00);
LCD_Write_Data(0x78);
LCD_Write_Com(0xEA); //Timing Control
LCD_Write_Data(0x00);
LCD_Write_Data(0x00);
LCD_Write_Com(0xED); //Power on sequence Control
LCD_Write_Data(0x64);
LCD_Write_Data(0x03);
LCD_Write_Data(0X12);
LCD_Write_Data(0X81);
LCD_Write_Com(0xF7); //Pump ratio Control
LCD_Write_Data(0x20);
LCD_Write_Com(0xC0); //Power control
LCD_Write_Data(0x23); //VRH[5:0]
LCD_Write_Com(0xC1); //Power control
LCD_Write_Data(0x10); //SAP[2:0];BT[3:0]
LCD_Write_Com(0xC5); //VCM control
LCD_Write_Data(0x3e); //Contrast
LCD_Write_Data(0x28);
LCD_Write_Com(0xC7); //VCM control2
LCD_Write_Data(0x86); //--
LCD_Write_Com(0x36); // Memory Access Control
LCD_Write_Data(0x48);
LCD_Write_Com(0x3A);
LCD_Write_Data(0x55);
LCD_Write_Com(0xB1);
LCD_Write_Data(0x00);
LCD_Write_Data(0x18);
LCD_Write_Com(0xB6); // Display Function Control
LCD_Write_Data(0x08);
LCD_Write_Data(0x82);
LCD_Write_Data(0x27);
LCD_Write_Com(0x11); //Exit Sleep
delay(120);
LCD_Write_Com(0x29); //Display on
}
void LCD_V_Line(unsigned int uiX, unsigned int uiY, unsigned int uiLength, unsigned int uiColor) //Laenge = 0 malt kein Pixel
{
unsigned int i,j, uiY2;
LCD_Write_Com(0x02c); //write_memory_start
digitalWrite(LCD_RS_Touch_XA,HIGH);
digitalWrite(LCD_CS_Touch_YA,LOW);
uiY2=uiY+uiLength;
//l=l+y;
Address_set(uiY,uiX,uiY2,uiX);
// Address_set(y,x,l,x);
j=uiLength;//*2;
for(i=1;i<=j;i++)
{
LCD_Write_Data(uiColor>>8);
LCD_Write_Data(uiColor);
}
digitalWrite(LCD_CS_Touch_YA,HIGH);
}
void LCD_H_Line(unsigned int uiX, unsigned int uiY, unsigned int uiLength, unsigned int uiColor) //Laenge = 0 malt kein Pixel
{
unsigned int i,j, uiX2;
LCD_Write_Com(0x02c); //write_memory_start
digitalWrite(LCD_RS_Touch_XA,HIGH);
digitalWrite(LCD_CS_Touch_YA,LOW);
uiX2=uiX+uiLength;
Address_set(uiY,uiX,uiY,uiX2);
// Address_set(x,y,x,l);
j=uiLength;//*2;
for(i=1;i<=j;i++)
{
LCD_Write_Data(uiColor>>8);
LCD_Write_Data(uiColor);
}
digitalWrite(LCD_CS_Touch_YA,HIGH);
}
//int RGB(int r,int g,int b)
//{return r << 16 | g << 8 | b;
//}
void LCD_RectOutline(unsigned int uiX,unsigned int uiY,unsigned int uiWidth,unsigned int uiHeight,unsigned int uiColor)
{
LCD_H_Line(uiX, uiY, uiWidth, uiColor); //line top
LCD_H_Line(uiX, uiY + uiHeight, uiWidth, uiColor); //line buttom
LCD_V_Line(uiX, uiY, uiHeight, uiColor); //line left
LCD_V_Line(uiX + uiWidth, uiY, uiHeight, uiColor); //line right;
}
void LCD_RectFilled(unsigned int uiX,unsigned int uiY,unsigned int uiWidth,unsigned int uiHeight,unsigned int uiColor)
{
unsigned int i,m, uiX2, uiY2;
LCD_Write_Com(0x2c);
digitalWrite(LCD_RS_Touch_XA,HIGH);
digitalWrite(LCD_CS_Touch_YA,LOW);
uiX2 = uiX + uiWidth;
uiY2 = uiY + uiHeight;
Address_set(uiY,uiX,uiY2,uiX2);
for(i=0;i<=uiHeight;i++)
for(m=0;m<=uiWidth;m++)
{
LCD_Write_Data(uiColor>>8);
//LCD_Write_Data(255);
LCD_Write_Data(uiColor);
}
digitalWrite(LCD_CS_Touch_YA,HIGH);
}
//debug out number of LEDs
void Matrix_Set_N_LEDs(int iLeds)
{
int i;
for(i=0;i<104;i++)
{
matrixLED[i] = 0;
}
for(i=0;i<iLeds;i++)
{
matrixLED[i] = 1;
}
matrix.draw(matrixLED);
}
void setup()
{
//Monitor.begin(115200);
delay(5000); //wait for CPU
pinMode(LCD_RD, OUTPUT);
pinMode(LCD_WR, OUTPUT);
pinMode(LCD_RS_Touch_XA, OUTPUT);
pinMode(LCD_CS_Touch_YA, OUTPUT);
digitalWrite(LCD_RD,HIGH);
digitalWrite(LCD_WR,HIGH);
digitalWrite(LCD_RS_Touch_XA,HIGH);
digitalWrite(LCD_CS_Touch_YA,HIGH);
pinMode(LCD_D0_Touch_XB, OUTPUT);
pinMode(LCD_D1_Touch_YB, OUTPUT);
pinMode(LCD_D2, OUTPUT);
pinMode(LCD_D3, OUTPUT);
pinMode(LCD_D4, OUTPUT);
pinMode(LCD_D5, OUTPUT);
pinMode(LCD_D6, OUTPUT);
pinMode(LCD_D7, OUTPUT);
LCD_Init();
LCD_RectFilled(0,0,uiLCD_Width,uiLCD_Height,ILI9341_BLACK);
//Place touches
// Analog values of touch Displayed key fields
// left, right, top, bottom, left, top, width, height, not just touched, not just released, not current touched, not last touched
LCD_Touch[0] = {0, 100, 400, 100, 400, 30, 120, 100, 80, false, false, false, false};
LCD_Touch[1] = {0, 500, 800, 100, 400, 180, 120, 100, 80, false, false, false, false};
LCD_Touch[2] = {0, 100, 400, 500, 800, 30, 25, 100, 80, false, false, false, false};
LCD_Touch[3] = {0, 500, 800, 500, 800, 180, 25, 100, 80, false, false, false, false};
//setup 10ms timer
lSampleTimer = millis();
iButtonCounter_Debug = 0;
}
void loop()
{
if (!SampleTickerTicked()) //Handle timer
{
TouchScreen_read(); //also Button are read while setting time of analog Input
//left upper touch
if (LCD_Touch[0].bJustTouched)
{
//your own code
}
//right upper touch
if (LCD_Touch[1].bJustTouched)
{
//your own code
}
//left lower touch
if (LCD_Touch[2].bJustTouched)
{
//your own code
}
//right lower touch
if (LCD_Touch[3].bJustTouched)
{
//your own code
}
}
}