Hello! Hoping you can help me with this ![]()
I'm new to touch screen lcds. I'm using sainsmart 3.2" tft lcd with sd card slot along with its shield and an arduino mega 2560.

I have a project that alarms when the pressure read by the sensor falls out of range.
The display will look like this:
P1 _____ kPa [ + ] [ - ]
P2 _____ kPa [ + ] [ - ]
I'm still on the 'Selecting Range' step. I can decrease/increase P1 and P2 values by pressing +/- button but since the range should be from -101.3 to 101.3 and I can only add or subtract 0.1 per press, I'm not satisfied. I know the user will get tired just getting to 10 kPa.
This is my current code:
// UTouch_ButtonTest (C)2010-2014 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program is a quick demo of how create and use buttons.
//
// This program requires the UTFT library.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin
// numbers in the setup.
//
//Jiah was here
#include <UTFT.h>
#include <UTouch.h>
//#include <stdlib.h>
// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!
UTFT myGLCD(ITDB32S,38,39,40,41);
// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield : 6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due : 6, 5, 4, 3, 2
// Teensy 3.x TFT Test Board : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
UTouch myTouch( 6, 5, 4, 3, 2);
// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SmallFont[];
extern uint8_t SevenSegNumFont[];
long startTime = 0;
int test=0;
//int x, y;
float pressureOne=0.0;
float pressureTwo=0.0;
char stpOne[20]="";
char stpTwo[20]="";
String convOne;
String convTwo;
extern unsigned int logo[0x200];
/*************************
** Custom functions **
*************************/
void drawButtons()
{
myGLCD.setFont(BigFont);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(0, 112, 185);
// Draw the upper row of buttons
myGLCD.setColor(0, 112, 185);
myGLCD.fillRoundRect (170, 90, 198, 118);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (170, 90, 198, 118);
myGLCD.print("+", 176, 96, 0);
myGLCD.setColor(0, 112, 185);
myGLCD.fillRoundRect (206, 90, 234, 118);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (206, 90, 234, 118);
myGLCD.print("-", 212, 96, 0);
// Draw the lower row of buttons
myGLCD.setColor(0, 112, 185);
myGLCD.fillRoundRect (170, 134, 198, 162);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (170, 134, 198, 162);
myGLCD.print("+", 176, 140, 0);
myGLCD.setColor(0, 112, 185);
myGLCD.fillRoundRect (206, 134, 234, 162);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (206, 134, 234, 162);
myGLCD.print("-", 212, 140, 0);
myGLCD.setBackColor (0, 112, 185);
}
void calculateVal(){
// convOne=dtostrf(pressureOne, 4, 1, stpOne);
// convTwo=dtostrf(pressureTwo, 4, 1, stpTwo);
myGLCD.setFont(BigFont);
myGLCD.setColor(0, 0, 0);
// myGLCD.print("HERE", 40, 96);
myGLCD.printNumF(pressureOne, 1, 40, 96, 46, 6, 32);
myGLCD.printNumF(pressureTwo, 1, 40, 140, 46, 6, 32);
}
void processTouch() {
if (myTouch.dataAvailable())
{
// test += 1;
// myGLCD.printNumI(x , 40, 184, 0);
// myGLCD.printNumI(y , 40, 200, 0);
myTouch.read();
int x=myTouch.getX();
int y=myTouch.getY();
if ((y>=90) && (y<=118)) // Upper row
{
if((pressureOne >= -101.3) && (pressureOne <= 101.3)){
if ((x>=170) && (x<=198)) // Button: P1 +
{
// calculateVal();
pressureOne += 0.1;
waitForIt(170, 90, 198, 118);
// convOne=dtostrf(pressureOne,4,1,stpOne);
}
if ((x>=206) && (x<=234)) // Button: P1 -
{
pressureOne -= 0.1;
waitForIt(206, 90, 234, 118);
// convOne=dtostrf(pressureOne,4,1,stpOne);
}
}
}
if ((y>=134) && (y<=162)) // Center row
{
if((pressureTwo >= -101.3) && (pressureTwo <= 101.3)){
if ((x>=170) && (x<=198)) // Button: P2 +
{
pressureTwo += 0.1;
waitForIt(170, 134, 198, 162);
// convTwo=dtostrf(pressureTwo,4,1,stpTwo);
}
if ((x>=206) && (x<=234)) // Button: P2 -
{
pressureTwo -= 0.1;
waitForIt(206, 134, 234, 162);
// convTwo=dtostrf(pressureTwo,4,1,stpTwo);
}
}
}
// myGLCD.print(" ", 40, 96);
// myGLCD.printNumF(pressureOne, 1, 40, 96, 46, 6, 32);
// myGLCD.printNumF(pressureTwo, 1, 40, 140, 46, 6, 32);
calculateVal();
processTouch();
}
}
// Draw a red frame while a button is touched
void waitForIt(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);
}
/*************************
** Required functions **
*************************/
void setup()
{
// Initial setup
myGLCD.InitLCD(PORTRAIT);
myGLCD.clrScr();
myTouch.InitTouch(PORTRAIT);
myTouch.setPrecision(PREC_MEDIUM);
// Logo with white fill
myGLCD.setColor(255, 255, 255);
myGLCD.fillRect(0, 0, 239, 73);
myGLCD.drawBitmap(35, 5, 171, 63, logo, 0, 0, 0);
// Setup, Gray fill
myGLCD.setColor(192, 192, 192);
myGLCD.fillRect(0, 73, 239, 178);
// Home, White fill
myGLCD.setColor(255, 255, 255);
myGLCD.fillRect(0, 290, 239, 319);
drawButtons();
}
void loop()
{
myGLCD.setFont(BigFont);
myGLCD.setColor(0, 112, 185);
myGLCD.setBackColor(255, 255, 255);
myGLCD.print("HOME" , 87, 298, 0);
myGLCD.setBackColor(192, 192, 192);
myGLCD.print("P1 " , 3, 96, 0);
myGLCD.print("P2 " , 3, 140, 0);
myGLCD.setFont(SmallFont);
myGLCD.setColor(0, 0, 0);
myGLCD.print(" kPa" , 133, 99, 0);
myGLCD.print(" kPa" , 133, 143, 0);
while(true)
{
startTime = millis();
// run measurements for one second and process keypresses during that time
// while (millis() - startTime < 1000) {
// while (myTouch.dataAvailable())
// {
processTouch();
// }
// calculateVal();
myGLCD.setFont(BigFont);
myGLCD.setColor(0, 0, 0);
// myGLCD.printNumI(test, 40, 156);
// myGLCD.print(convOne, 40, 96, 0);
// myGLCD.print(convTwo, 40, 140, 0);
// myGLCD.printNumF(pressureOne, 1, 40, 96, 46, 6, 32);
// myGLCD.printNumF(pressureTwo, 1, 40, 140, 46, 6, 32);
}
}
Forgive me it has a lot of comment since I've been changing and changing the sequence trying to make the (hold) button work.