using X and Y but can not go back up. please someone help me

I’m using 2.4 TFT touchscreen with Arduino doing my project, but can only run one time. when I want to loop back its not working. the code is below. please help thanks.``

__.ino (12.8 KB)

I finish with {x==x-1} That's not going to happen, is it?

This is an example of an XY problem. You want us to tell you why your solution to a problem does not work without us having a clue as to the problem.

#include <Adafruit_TFTLCD.h> //hardware-specific library
#include <Adafruit_GFX.h> //Core graphics library
#include <TouchScreen.h> //TouchScreen library

#define LCD_RESET A4 //touchscreen pin
#define LCD_CS A3 //touchscreen pin
#define LCD_CD A2 //touchscreen pin
#define LCD_WR A1 //touchscreen pin
#define LCD_RD A0 //touchscreen pin

#define TS_MINX 122
#define TS_MINY 111
#define TS_MAXX 942
#define TS_MAXY 890

#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 //digital pin
#define XP 8 //digital pin

#define pusherMotor_1 30 //for pusher
#define pusherMotor_2 31 //for pusher
#define enA 32 //enable for motor 1

#define cutterDown_1 33 //for cutter move down
#define cutterDown_2 34 //for cutter move down
#define enB 35 //enable for motor 2

#define cutterUp_1 36 //for cutter go up
#define cutterUp_2 37 //for cutter go up
#define enA2 38 //enable for motor3

#define cutter_1 39 //for cutter
#define cutter_2 40 //for cutter
#define enB2 41 //enable for motor4

int delaytime = 20; //delay 3seconds

int sensor1 = 22; //sensor 1 detect whether inside the machine got can or not
int sensor2 = 24; //sensor 2 detect whether the door is closed or open
int sensor3 = 50; //sensor 3 place at the wall when pusher push the can near this sensor the pusher will stop
int sensor4 = 26; //sensor 4 place at the cutter, when the cutter comes down to sense the can then will stop
int sensor5 = 27; //sensor 5 place at the cover, when the cutting is finished the cutter move up once it is sense the cover the cutter move up it will stop

int detect1; //use for sensor 1, whether inside the machine got can or not
int detect2; //use for sensor 2, whether the door is close or open
int detect3; //use for sensor 3, whether the pusher push the can to the sensor
int detect4; //use for sensor 4, whether the cutter come down or not
int detect5; //use for sensor 5, whether the cutter go up back to its orginal position

int x;
int y;

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);//Start communication with LCD

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);//364 is the sensitivity

boolean buttonEnabled = true;
boolean buttonEnabled1 = true;

void setup() {
Serial.begin(9600);
pinMode(pusherMotor_1, OUTPUT);
pinMode(pusherMotor_2, OUTPUT);
pinMode(enA, OUTPUT);

pinMode(cutterDown_1, OUTPUT);
pinMode(cutterDown_2, OUTPUT);
pinMode(enB, OUTPUT);

pinMode(cutterUp_1, OUTPUT);
pinMode(cutterUp_2, OUTPUT);
pinMode(enA2, OUTPUT);

pinMode(cutter_1, OUTPUT);
pinMode(cutter_2, OUTPUT);
pinMode(enB2, OUTPUT);

pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
pinMode(sensor3, INPUT);
pinMode(sensor4, INPUT);
pinMode(sensor5, INPUT);

tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.setRotation(1);
tft.fillScreen(WHITE);
tft.drawRect(0, 0, 319, 240, YELLOW);

tft.setCursor(30, 40);
tft.setTextColor(BLACK);
tft.setTextSize(4);
tft.print(“Please wait while machine is getting ready…”);
delay(3000);
tft.fillScreen(WHITE);
pusherBack();
cutterUp();
delay(3000);
pusherstop();
cutterUpstop();

detect1 = digitalRead(sensor1); //to sense inside the machine got can or not
if (detect1 == LOW) {
tft.fillScreen(WHITE); //tell the customer already got one can inside the machine
tft.drawRect(0, 0, 319, 240, YELLOW);
tft.setCursor(30, 40);
tft.setTextColor(MAGENTA);
tft.setTextSize(3);
tft.print(“A can is present,Please remove it!”);
x = x + 0;
}
}

void loop() {

while (x == 0) {
detect1 = digitalRead(sensor1); //to sense inside the machine got can or not
if (detect1 == HIGH) {
tft.fillScreen(WHITE); //if inside the machine dont have any can tell the customer place a new can in the machine
tft.drawRect(0, 0, 319, 240, YELLOW);
tft.setCursor(30, 40);
tft.setTextColor(MAGENTA);
tft.setTextSize(3);
tft.print(“Place a can in the machine!”);
x = x + 1;
}
}

while (x == 1) {
detect1 = digitalRead(sensor1); //to sense inside the machine got can or not
if (detect1 == LOW) {
tft.fillScreen(WHITE);
tft.drawRect(0, 0, 319, 240, YELLOW); //after the customer put the can tell him to close the door
tft.setCursor(30, 40);
tft.setTextColor(MAGENTA);
tft.setTextSize(3);
tft.print(“Please close the door”);
x = x + 1;
}
}

while (x == 2) {

detect2 = digitalRead(sensor2); // to sense the door is open or close
if (detect2 == LOW) {

tft.fillScreen(WHITE);
tft.setCursor(30, 40); //if the door is close tell the user press start button
tft.setTextColor(BLACK);
tft.setTextSize(4);
tft.print(“Press START button”);

tft.fillRect(50, 130, 210, 40, GREEN);
tft.drawRect(50, 130, 210, 40, BLACK);
tft.setCursor(60, 150);
tft.setTextColor(BLACK);
tft.setTextSize(2);
tft.print(“START”);
x = x + 1;
}
}

TSPoint p = ts.getPoint(); //to read the screen button

while (x == 3 && p.z > ts.pressureThreshhold && detect2 == LOW ) {

p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
p.y = map(p.y, TS_MAXY, TS_MINY, 0, 480);
if (p.x > 80 && p.x < 140 && p.y > 85 && p.y < 192 && buttonEnabled) {
buttonEnabled = false;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);

tft.fillScreen(WHITE);
tft.drawRect(0, 0, 319, 240, YELLOW);
tft.setCursor(30, 40);
tft.setTextColor(BLUE);
tft.setTextSize(4);
tft.print(“Operation in progress…”);
x = x + 1;
}
}

while (x == 4) {

detect3 = digitalRead(sensor3); //this sensor is to sense whether the pusher push the can to the side
if (detect3 == LOW) {
pusherstop();
x = x + 1;
}
detect3 = digitalRead(sensor3); //this sensor is to sense whether the pusher push the can to the side
if (detect3 == HIGH) {
pusherFront();
}

}

while (x == 5) {
detect1 = digitalRead(sensor1); // finish one cycle go back to the front
if (detect1 == LOW) {
x = x - 4;
}
}
}

Please edit your post to add code tags.

After you do that, consider what is wrong with this...

TSPoint p = ts.getPoint();  //to read the screen button

while (x == 3 && p.z > ts.pressureThreshhold && detect2 == LOW ) {   //never update p }