Whoops! My apologies.
Here is a copy of my code. It is incomplete, so it is messy and some of it may not make sense, but the touchscreen calibrations are there. I originally ran this code on a smaller 2.4" display and it worked fine there -- not so much on the 2.8" display.
/**** INCLUDE THE REQUIRED LIBRARIES FOR THE CODE ****/
// Stepper motor library:
#include <AccelStepper.h>
// Touchscreen display libraries:
#include <TouchScreen.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
/**** DEFINE THE ARDUINO PINS ****/
// Stepper motor pins:
#define dirPin 12 // Pin connecting to DIR+ port on motor driver:
#define stepPin 11 // Pin connecting to PUL+ port on motor driver:
#define motorInterfaceType 1 // Motor interface type defaulted to 1:
// Touchscreen display pins:
#define YP A1 // Pin connecting to 5V input on touchscreen:
#define XM A2 // Pin connecting to ground on touchscreen:
#define YM 7 // Pin connecting to ground on touchscreen:
#define XP 6 // Pin connecting to 5V input on touchscreen:
#define LCD_CS A3 // Pin connecting to CD_CS on touchscreen shield:
#define LCD_CD A2 // Pin connecting to CD_RS on touchscreen shield:
#define LCD_WR A1 // Pin connecting to CD_WR on touchscreen shield:
#define LCD_RD A0 // Pin connecting to CD_RD on touchscreen shield:
#define LCD_RESET A4 // Pin connecting to CD_RST on touchscreen shield:
/**** CREATE OBJECTS TO MANIPULATE IN CODE ****/
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
/**** CREATE AND DEFINE COLORS ****/
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define DARKORANGE 0xFB60
#define MAROON 0x7800
#define GRAY 0x8410
/**** DEFINE PRESSURE SETTINGS ****/
#define MINPRESSURE 100 // Defines minimum pressure applied to touchscreen display for input:
#define MAXPRESSURE 2000 // Defines maximum pressure applied to touchscreen display for input:
/**** CALIBRATE TOUCHSCREEN ACCORDING TO SCREEN SIZE ****/
#define TS_MINX 916 // Defines left bound of the screen:
#define TS_MINY 75 // Defines top bound of the screen:
#define TS_MAXX 109 // Defines right bound of the screen:
#define TS_MAXY 905 // Defines bottom bound of the screen:
/**** INITIATE TRUE/FALSE CONDITIONS FOR BUTTON PRESS ****/
bool change = 0;
bool touch = 0; // Initial condition for start/stop button:
bool rst = 0; // Initial condition for reset button:
/**** DEFINE VARIABLES ****/
int origin = 0; // Initial condition for center of machine:
int speedset = 0; // Initial condition for speed:
int command = 0; // Initial condition for touch settings:
int back = 0; // Initial condition for back button:
int back2 = 0;
int spdbtn = 0; // Initial condition for speed setting button:
int locbtn = 0; // Initial condition for location setting button:
int set = 0; // Initial condition for settings button:
int units = 0; // Initial condition for units buttons:
int calibrate = 0; // Initial condition for calibrate button:
/**** CREATE HOME SCREEN FUNCTION ****/
void homescreen() {
if (change == 0 && touch == 0 && rst == 0 && set == 0) {
// Parameters for stepper motor:
stepper.setMaxSpeed(speedset);
// Code motor movement:
stepper.moveTo(origin);
stepper.runSpeedToPosition();
delay(100);
tft.fillRoundRect(25, 25, 150, 195, 10, GREEN); // Creates button shape. Syntax: tft.fillRoundRect(x, y, width, height, radius, color):
tft.setCursor(45, 110); // Text location. Syntax: tft.setCursor(x, y):
tft.setTextSize(4); // Font size:
tft.println("START"); // Message:
tft.fillRoundRect(200, 25, 100, 90, 10, GRAY);
tft.setCursor(205, 65);
tft.setTextSize(3);
tft.println("RESET");
tft.fillRoundRect(200, 130, 100, 90, 10, GRAY);
tft.setCursor(205, 165);
tft.setTextSize(2);
tft.println("SETTINGS");
change = 1;
command = 0;
back = 0;
back2 = 0;
spdbtn = 0;
locbtn = 0;
set = 0;
delay(125);
}
if (change == 0 && touch == 1 && rst == 0 && set == 0) {
// Parameters for stepper motor:
stepper.setMaxSpeed(speedset);
// Code motor movement:
stepper.moveTo(origin + 1000);
stepper.runSpeedToPosition();
delay(100);
stepper.moveTo(origin - 1000);
stepper.runSpeedToPosition();
delay(100);
tft.fillRoundRect(25, 25, 150, 195, 10, RED); // Creates button shape. Syntax: tft.fillRoundRect(x, y, width, height, radius, color):
tft.setCursor(55, 110); // Text location. Syntax: tft.setCursor(x, y):
tft.setTextSize(4); // Font size:
tft.println("STOP"); // Message:
tft.fillRoundRect(200, 25, 100, 90, 10, GRAY);
tft.setCursor(205, 65);
tft.setTextSize(3);
tft.println("RESET");
tft.fillRoundRect(200, 130, 100, 90, 10, GRAY);
tft.setCursor(205, 165);
tft.setTextSize(2);
tft.println("SETTINGS");
change = 1;
command = 0;
back = 0;
back2 = 0;
spdbtn = 0;
locbtn = 0;
set = 0;
delay(25);
}
if (change == 0 && touch == 0 && rst == 1 && set == 0) {
origin = 0; // Reset platform to the center of the machine at motor position 0:
}
if (change == 0 && touch == 0 && rst == 0 && set == 1) {
settings();
command = 1;
}
}
/**** CREATE SETTINGS FUNCTION ****/
void settings() {
if (change == 0 && back == 0 && spdbtn == 0 && locbtn == 0) {
tft.fillScreen(ORANGE);
tft.fillRoundRect(12, 15, 75, 35, 5, GRAY); // Creates button shape. Syntax: tft.fillRoundRect(x, y, width, height, radius, color):
tft.setCursor(25, 25); // Text location. Syntax: tft.setCursor(x, y):
tft.setTextSize(2); // Font size:
tft.println("BACK"); // Message:
tft.fillRoundRect(12, 75, 300, 70, 10, GRAY);
tft.setCursor(50, 100);
tft.setTextSize(3);
tft.println("SPEED SETTING");
tft.fillRoundRect(12, 160, 300, 70, 10, GRAY);
tft.setCursor(20, 185);
tft.setTextSize(3);
tft.println("LOCATION SETTING");
change = 1;
command = 1;
back = 0;
back2 = 0;
spdbtn = 0;
locbtn = 0;
set = 1;
delay(225);
}
if (change == 0 && back == 1 && spdbtn == 0 && locbtn == 0) {
tft.fillScreen(WHITE);
tft.fillRoundRect(25, 25, 150, 195, 10, GREEN); // Creates button shape. Syntax: tft.fillRoundRect(x, y, width, height, radius, color):
tft.setCursor(45, 110); // Text location. Syntax: tft.setCursor(x, y):
tft.setTextSize(4); // Font size:
tft.println("START"); // Message:
tft.fillRoundRect(200, 25, 100, 90, 10, GRAY);
tft.setCursor(205, 65);
tft.setTextSize(3);
tft.println("RESET");
tft.fillRoundRect(200, 130, 100, 90, 10, GRAY);
tft.setCursor(205, 165);
tft.setTextSize(2);
tft.println("SETTINGS");
change = 1;
command = 0;
back = 0;
spdbtn = 0;
locbtn = 0;
set = 0;
delay(225);
}
if (change == 0 && back == 0 && spdbtn == 1 && locbtn == 0) {
speedsetting();
command = 2;
}
if (change == 0 && back == 0 && spdbtn == 0 && locbtn == 1) {
locsetting();
command = 3;
}
}
/**** CREATE SPEED SETTING FUNCTION ****/
void speedsetting() {
if (change == 0 && back2 == 0 && units == 0) {
tft.fillScreen(WHITE);
tft.fillRoundRect(12, 15, 75, 35, 5, GRAY); // Creates button shape. Syntax: tft.fillRoundRect(x, y, width, height, radius, color):
tft.setCursor(25, 25); // Text location. Syntax: tft.setCursor(x, y):
tft.setTextSize(2); // Font size:
tft.println("BACK"); // Message:
tft.fillRoundRect(235, 15, 75, 35, 5, GRAY);
tft.setCursor(245, 25);
tft.setTextSize(2);
tft.println("UNITS");
tft.setCursor(12, 150);
tft.setTextSize(2);
tft.print("Motor Speed:");
tft.drawRect(12, 170, 295, 40, BLACK);
change = 1;
command = 2;
back2 = 0;
spdbtn = 1;
locbtn = 0;
set = 1;
delay(225);
}
if (change == 0 && back2 == 1 && units == 0) {
tft.fillScreen(ORANGE);
tft.fillRoundRect(12, 15, 75, 35, 5, GRAY); // Creates button shape. Syntax: tft.fillRoundRect(x, y, width, height, radius, color):
tft.setCursor(25, 25); // Text location. Syntax: tft.setCursor(x, y):
tft.setTextSize(2); // Font size:
tft.println("BACK"); // Message:
tft.fillRoundRect(12, 75, 300, 70, 10, GRAY);
tft.setCursor(50, 100);
tft.setTextSize(3);
tft.println("SPEED SETTING");
tft.fillRoundRect(12, 160, 300, 70, 10, GRAY);
tft.setCursor(20, 185);
tft.setTextSize(3);
tft.println("LOCATION SETTING");
change = 1;
command = 1;
back2 = 0;
spdbtn = 0;
locbtn = 0;
set = 1;
delay(225);
}
}
/**** CREATE LOCATION SETTING FUNCTION ****/
void locsetting() {
if (change == 0 && back2 == 0 && calibrate == 0 && units == 0) {
tft.fillScreen(WHITE);
tft.fillRoundRect(12, 15, 75, 35, 5, GRAY); // Creates button shape. Syntax: tft.fillRoundRect(x, y, width, height, radius, color):
tft.setCursor(25, 25); // Text location. Syntax: tft.setCursor(x, y):
tft.setTextSize(2); // Font size:
tft.println("BACK"); // Message:
tft.fillRoundRect(103, 15, 115, 35, 5, GRAY);
tft.setCursor(108, 25);
tft.setTextSize(2);
tft.println("CALIBRATE");
tft.fillRoundRect(235, 15, 75, 35, 5, GRAY);
tft.setCursor(245, 25);
tft.setTextSize(2);
tft.println("UNITS");
tft.setCursor(12, 150);
tft.setTextSize(2);
tft.print("Location:");
tft.drawRect(12, 170, 295, 40, BLACK);
change = 1;
command = 3;
back = 0;
back2 = 0;
spdbtn = 0;
locbtn = 1;
set = 1;
delay(225);
}
if (change == 0 && back2 == 1 && calibrate == 0 && units == 0) {
tft.fillScreen(ORANGE);
tft.fillRoundRect(12, 15, 75, 35, 5, GRAY); // Creates button shape. Syntax: tft.fillRoundRect(x, y, width, height, radius, color):
tft.setCursor(25, 25); // Text location. Syntax: tft.setCursor(x, y):
tft.setTextSize(2); // Font size:
tft.println("BACK"); // Message:
tft.fillRoundRect(12, 75, 300, 70, 10, GRAY);
tft.setCursor(50, 100);
tft.setTextSize(3);
tft.println("SPEED SETTING");
tft.fillRoundRect(12, 160, 300, 70, 10, GRAY);
tft.setCursor(20, 185);
tft.setTextSize(3);
tft.println("LOCATION SETTING");
change = 1;
command = 1;
back = 0;
back2 = 0;
spdbtn = 0;
locbtn = 0;
set = 1;
delay(225);
}
}
/**** SET UP PARAMETERS FOR MACHINE ****/
void setup() {
// Parameters for touchscreen display:
Serial.begin(9600);
tft.reset();
tft.begin(0x9341); // Defines driver type for touchscreen display:
tft.setRotation(1); // Locks screen in an orientation (0 = vertical & 1 = horizontal):
tft.setTextColor(BLACK); // Defines text color:
tft.fillScreen(WHITE); // Defines background color:
}
/**** CREATE MAIN CODE TO RUN MACHINE ****/
void loop() {
homescreen();
TSPoint p = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
if (command == 0) {
if (p.x > 65 && p.x < 300) {
if (p.y > 105 && p.y < 210) {
change = 0;
touch = !touch;
}
}
if (p.x > 200 && p.x < 300) {
if (p.y > 20 && p.y < 90) {
change = 0;
rst = !rst;
}
}
if (p.x > 65 && p.x < 175) {
if (p.y > 20 && p.y < 90) {
change = 0;
set = !set;
}
}
}
if (command == 1) {
if (p.x > 255 && p.x < 300) {
if (p.y > 170 && p.y < 223) {
change = 0;
back = !back;
}
}
if (p.x > 140 && p.x < 230) {
if (p.y > 12 && p.y < 223) {
change = 0;
spdbtn = !spdbtn;
}
}
if (p.x > 55 && p.x < 135) {
if (p.y > 15 && p.y < 223) {
change = 0;
locbtn = !locbtn;
}
}
}
if (command == 2) {
if (p.x > 255 && p.x < 300) {
if (p.y > 170 && p.y < 223) {
change = 0;
back2 = !back2;
}
}
if (p.x > 140 && p.x < 230) {
if (p.y > 12 && p.y < 223) {
change = 0;
units = !units;
}
}
}
if (command == 3) {
if (p.x > 255 && p.x < 300) {
if (p.y > 170 && p.y < 223) {
change = 0;
back2 = !back2;
}
}
if (p.x > 140 && p.x < 230) {
if (p.y > 12 && p.y < 223) {
change = 0;
units = !units;
}
}
if (p.x > 55 && p.x < 135) {
if (p.y > 15 && p.y < 223) {
change = 0;
calibrate = !calibrate;
}
}
}
}
}