I am trying to write a code with a home screen and then multiple menu options.
I need to change the 'page' so I can have a fresh touch screen for the next menu.
Im using the mega2560 with 2.8 TFT LCD Screen.
Im a beginner and very inexperienced, any help would be great.
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#if defined(SAM3X8E)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).
// D0 connects to digital pin 22
// D1 connects to digital pin 23
// D2 connects to digital pin 24
// D3 connects to digital pin 25
// D4 connects to digital pin 26
// D5 connects to digital pin 27
// D6 connects to digital pin 28
// D7 connects to digital pin 29
/*
#define YP 9 // must be an analog pin, use "An" notation!
#define XM 8 // must be an analog pin, use "An" notation!
#define YM A2 // can be a digital pin
#define XP A3 // can be a digital pin
*/
#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
#define TS_MINX 100
#define TS_MAXX 920
#define TS_MINY 70
#define TS_MAXY 900
/*
#define TS_MINX 125
#define TS_MAXX 913
#define TS_MINY 118
#define TS_MAXY 833*/
//Touchscreen pressure sensitivity
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4
// Color definitions
#define BLACK 0x0000
#define RED 0x001F
#define BLUE 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Color definitions
#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_RED 0x001F / 0, 0, 255 /
#define ILI9341_GREEN 0x07E0 / 0, 255, 0 /
#define ILI9341_CYAN 0x07FF / 0, 255, 255 /
#define ILI9341_BLUE 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
// text box where numbers go
#define TEXT_X 10
#define TEXT_Y 10
#define TEXT_W 220
#define TEXT_H 50
#define TEXT_TSIZE 3
#define TEXT_LEN 12
char textfield[TEXT_LEN+1] = "";
uint8_t textfield_i=0;
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
//Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
#define BOXSIZE 40
#define PENRADIUS 3
#define BOXSTATE 0
//int oldcolor, currentcolor;
const int ledPin = 22; // the number of the 5v output pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
//*****//
void setup(){
Serial.begin(9600);
Serial.println(F("Paint!"));
tft.reset();
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
Serial.println(F("Found ILI9325 LCD driver"));
} else if(identifier == 0x9328) {
Serial.println(F("Found ILI9328 LCD driver"));
} else if(identifier == 0x4535) {
Serial.println(F("Found LGDP4535 LCD driver"));
}else if(identifier == 0x7575) {
Serial.println(F("Found HX8347G LCD driver"));
} else if(identifier == 0x9341) {
Serial.println(F("Found ILI9341 LCD driver"));
}else if(identifier == 0x7783) {
Serial.println(F("Found ST7781 LCD driver"));
}else if(identifier == 0x8230) {
Serial.println(F("Found UC8230 LCD driver"));
}
else if(identifier == 0x8357) {
Serial.println(F("Found HX8357D LCD driver"));
} else if(identifier==0x0101)
{
identifier=0x9341;
Serial.println(F("Found 0x9341 LCD driver"));
}else {
Serial.print(F("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
Serial.println(F("If using the Adafruit 2.8" TFT Arduino shield, the line:"));
Serial.println(F(" #define USE_ADAFRUIT_SHIELD_PINOUT"));
Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
Serial.println(F("If using the breakout board, it should NOT be #defined!"));
Serial.println(F("Also if using the breakout, double-check that all wiring"));
Serial.println(F("matches the tutorial."));
identifier=0x9341;
}
tft.begin(identifier);
#define MINPRESSURE 10 //Min pressure for touchscreen
#define MAXPRESSURE 1000 //Max pressure for touchcreen
pinMode(22, OUTPUT); //Assign pin 22 as output (for Digital Output 5v)
{{
tft.setRotation(1); //Rotate screen 90deg
tft.fillScreen(BLACK); //Fill screen Black
tft.fillRect(70, 20, 50, 50, RED); //Box, from top left corner x=70 y=20 size 50,50 filled red
tft.setTextColor(WHITE); //Text colour
tft.setTextSize(1); //Text size
tft.setCursor(76, 27); //Position of start of text
tft.println("Digital"); //Text wording
}
{
tft.setTextColor(WHITE); //Text colour
tft.setTextSize(1); //Text size
tft.setCursor(76, 42); //Position of start of text
tft.println("Output"); //Text wording
}
{
tft.setTextColor(WHITE); //Text colour
tft.setTextSize(1); //Text size
tft.setCursor(76, 57); //Position of start of text
tft.println("5v"); //Text wording
}
}
}
//******//
void loop(){
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
{ if (p.y < (TS_MINY-5)) {
Serial.println("erase");
tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);
}
{
p.x = map(p.x, TS_MINX, TS_MAXX, tft.height(), 0);
//p.x = tft.width()-map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = (tft.width()-map(p.y, TS_MINY, TS_MAXY, tft.width(), 0));
//p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
}
{
if (((p.x > 170) && (p.x < 220)) && ((p.y > 70) && (p.y < 120))){ //If touched in that location from bottom left corner
(tft.fillRect(70, 20, 50, 50, GREEN)); //Green rectangle shown
(delay(500)); //Delay of 0.5 sec
}
{
tft.fillScreen(BLACK); //Background Black
}
{
tft.fillRect(120, 120, 80, 40, YELLOW); //Coloured in rectangle
}
{ tft.setTextColor(BLACK); //Text colour
tft.setCursor(126, 130); //Position of start text
tft.setTextSize(2); //Text size
tft.println("ON OFF"); //Text wording
tft.setTextColor(WHITE); //Text colour
tft.setCursor(60, 40); //Position of start text
tft.setTextSize(2); //Text size
tft.println("Digital Output 5v"); //Text wording
}
{
if ((digitalRead(22) == HIGH) && (((p.x > 80) && (p.x < 120)) && ((p.y > 160) && (p.y < 200)))){ //If pin 22 (digital output 5v) is ON and the 'OFF' box is touched
digitalWrite(22, LOW); //Switch pin 22 (digital output 5v) OFF
tft.fillRect(120, 80, 80, 40, RED); //Red rectangle to symbolise OFF
}}
{
if ((digitalRead(22) == LOW) && (((p.x > 80) && (p.x < 120)) && ((p.y > 120) && (p.y < 160)))){ //If pin 22 (digital output 5v) is OFF and the 'ON' box is touched
digitalWrite(22, HIGH); //Switch pin 22 (digital output 5v) ON
tft.fillRect(120, 80, 80, 40, GREEN); //Green rectangle to symbolise ON
}}}}}
//End Of Loop Brackets