Hey guys, currently I'm doing my project that use an Arduino Mega to connect with a touch screen LCD which is the ILI9481 type. I have completed my coding that use the Arduino to collect data from IR sensor and also temperature sensor. But after I uploaded the coding, the LCD screen become distorted. Can somebody help me with this problem? I don't know much about the touch screen LCD and am still learning.
Here I included the coding for my project. There is no error when I compile them.
#include <Adafruit_GFX.h>
#include <URTouch.h>
#include <UTFTGLUE.h>
URTouch myTouch(6,5,4,3,2);
UTFTGLUE myGLCD(0x9488,A2,A1,A3,A4,A0);
//LCD connection
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RST A4
//Font used
#if !defined(SmallFont)
extern uint8_t SmallFont[];
#elif !defined(BigFont)
extern uint8_t BigFont[];
#endif
int x,y;
int rpm;
int tempC;
int tempF;
char currentPage,selectedUnit;
const int speedPin=22;
const int tempPin=A15;
const int battPin=A14;
unsigned long prevmillis;
unsigned long duration;
unsigned long lcdrefresh;
boolean currentstate;
boolean prevstate;
void setup()
{
//Setup the LCD
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(BigFont);
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
drawHomeScreen();
currentPage='0';
selectedUnit='0';
//Setup Sensor
pinMode(speedPin,INPUT);
pinMode(tempPin,INPUT);
pinMode(battPin,INPUT);
prevmillis=0;
prevstate=LOW;
}
//HomeScreen
void drawHomeScreen()
{
myGLCD.setBackColor(0,0,0);
myGLCD.setColor(255,255,255);
myGLCD.setFont(BigFont);
myGLCD.print("AUTOMOTIVE VISUAL", CENTER, 10);
myGLCD.setColor(255,255,255);
myGLCD.drawLine(0,32,480,32);
myGLCD.setColor(255,255,255);
myGLCD.setFont(BigFont);
myGLCD.print("by AZIM SYAMIL", CENTER, 41);
myGLCD.setFont(BigFont);
myGLCD.print("Select Example", CENTER, 64);
//Button - Speed Sensor
myGLCD.setColor(255,0,0);
myGLCD.fillRoundRect(100,90,380,130);
myGLCD.setColor(255,255,255);
myGLCD.drawRoundRect(100,90,380,130);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(255,0,0);
myGLCD.print("SPEED SENSOR", CENTER, 110);
//Button - Temperature Sensor
myGLCD.setColor(255,0,0);
myGLCD.fillRoundRect(100,150,380,190);
myGLCD.setColor(255,255,255);
myGLCD.drawRoundRect(100,150,380,190);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(255,0,0);
myGLCD.print("TEMPERATURE SENSOR", CENTER, 170);
//Button - Battery Capacity
myGLCD.setColor(255,0,0);
myGLCD.fillRoundRect(100,210,380,250);
myGLCD.setColor(255,255,255);
myGLCD.drawRoundRect(100,210,380,250);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(255,0,0);
myGLCD.print("BATTERY CAPACITY", CENTER, 230);
}
//Draw Frame
void drawFrame(int x1,int y1,int x2,int y2)
{
myGLCD.setColor(255,0,0);
myGLCD.drawRoundRect(x1,y1,x2,y2);
while (myTouch.dataAvailable())
myTouch.read();
myGLCD.setColor(255,255,255);
myGLCD.drawRoundRect(x1,y1,x2,y2);
}
//Speed Sensor
void drawSpeedSensor()
{
myGLCD.setColor(100,155,203);
myGLCD.fillRoundRect(10,10,60,36);
myGLCD.setColor(255,255,255);
myGLCD.drawRoundRect(10,10,60,36);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(100,155,203);
myGLCD.print("<-",18,15);
myGLCD.setBackColor(0,0,0);
myGLCD.setFont(BigFont);
myGLCD.print("Back to Main Menu",70,18);
myGLCD.setFont(BigFont);
myGLCD.print("SPEED SENSOR",CENTER,50);
myGLCD.print("IR SENSOR",CENTER,76);
myGLCD.setColor(255,0,0);
myGLCD.drawLine(0,100,319,100);
myGLCD.setFont(BigFont);
myGLCD.print("Speed :",CENTER,120);
}
//Temperature Sensor
void drawTemperatureSensor()
{
myGLCD.setColor(100, 155, 203);
myGLCD.fillRoundRect (10, 10, 60, 36);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 10, 60, 36);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(100, 155, 203);
myGLCD.print("<-", 18, 15);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(BigFont);
myGLCD.print("Back to Main Menu", 70, 18);
myGLCD.setFont(BigFont);
myGLCD.print("TEMPERATURE SENSOR",CENTER,50);
myGLCD.print("LM 35",CENTER,76);
myGLCD.setColor(255,0,0);
myGLCD.drawLine(0,75,319,75);
}
//Battery Capacity
void drawBatteryCapacity()
{
myGLCD.setColor(100, 155, 203);
myGLCD.fillRoundRect (10, 10, 60, 36);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 10, 60, 36);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(100, 155, 203);
myGLCD.print("<-", 18, 15);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(BigFont);
myGLCD.print("Back to Main Menu", 70, 18);
myGLCD.setFont(BigFont);
myGLCD.print("BATTERY CAPACITY",CENTER,50);
myGLCD.print("48 VOLTS",CENTER,76);
myGLCD.setColor(255,0,0);
myGLCD.drawLine(0,75,319,75);
}
//Input Speed Sensor
void getSpeed()
{
//RPM Measurement
currentstate=digitalRead(speedPin);
if(prevstate !=currentstate)
{
if(currentstate==HIGH)
{
duration=(micros()-prevmillis);
rpm=(60000000/duration);
prevmillis=micros();
}
}
prevstate=currentstate;
if((millis()-lcdrefresh)>=100)
{
myGLCD.setFont(BigFont);
myGLCD.setColor(255,255,255);
myGLCD.setBackColor(0,0,0);
myGLCD.printNumI(rpm,130,145,3,'0');
myGLCD.setFont(BigFont);
myGLCD.print("rpm ",235,178);
lcdrefresh=millis();
}
delay(10);
}
//Input Temperature Sensor
void getTemperature()
{
int value=analogRead(tempPin);
float millivolts=(value/1024.0)*5000;
float celcius=millivolts/10;
myGLCD.setColor(255,255,255);
myGLCD.setBackColor(0,0,0);
myGLCD.setFont(BigFont);
myGLCD.printNumI(celcius,130,175,3,'0');
myGLCD.print("CELCIUS",CENTER, 200);
delay(1000);
}
//Input Battery Capacity
void getBattery()
{
int voltReading=analogRead(battPin);
int volts=(voltReading/1023)*100;
myGLCD.setBackColor(0,0,0);
myGLCD.setColor(255,255,255);
myGLCD.setFont(BigFont);
myGLCD.printNumI(volts,CENTER,145,3,'0');
myGLCD.print(" %",200,145);
delay(1000);
}
void loop()
{
if (currentPage=='0')
{
if(myTouch.dataAvailable())
{
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
//Touch Speed
if((x>=100)&&(x<=380)&&(y>=90)&&(y<=130))
{
drawFrame(100,90,380,130);
currentPage='1';
myGLCD.clrScr();
drawSpeedSensor();
}
//Touch Temperature
if((x>=100)&&(x<=380)&&(y>=150)&&(y<=190))
{
drawFrame(100,150,380,190);
currentPage='2';
myGLCD.clrScr();
drawTemperatureSensor();
}
//Touch Battery
if((x>=100)&&(x<=380)&&(y>=210)&&(y<=250))
{
drawFrame(100,210,380,250);
currentPage='3';
myGLCD.clrScr();
drawBatteryCapacity;
}
}
}
if (currentPage=='1')
{
//ayat psal speed sensor
getSpeed();
if(myTouch.dataAvailable())
{
myTouch.read();
x=myTouch.getX();
y+myTouch.getY();
//Back Button
if ((x>=10)&&(x<=60)&&(y>=10)&&(y<=36))
{
drawFrame(10,10,60,36);
currentPage='0';
myGLCD.clrScr();
drawHomeScreen();
}
}
}
if (currentPage='2')
{
//ayat temperature sensor
getTemperature();
if(myTouch.dataAvailable())
{
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
//Back Button
if((x>=10)&&(x<=60)&&(y>=10)&&(y<=36))
{
drawFrame(10,10,60,36);
myGLCD.clrScr();
drawHomeScreen();
}
}
}
if(currentPage='3')
{
//ayat Battery Capacity
getBattery();
if(myTouch.dataAvailable())
{
myTouch.read();
x=myTouch.getX();
y-myTouch.getY();
//Back Button
if((x<=10)&&(x>=60)&&(y>=10)&&(y<=36))
{
drawFrame(10,10,60,36);
myGLCD.clrScr();
drawHomeScreen();
}
}
}
}
Thank you in advanced.