touch button inside while loop

Hi,
I have an issue for coding and I need your help please.
I am using 2.8" TFT LCD MCUfriend
I want to to do a while loop that count up to 50, and if i press the touchscreen button will break the loop

int16_t i = 0;

void loop() 
  {

// Counter up to 50
  while(flag1 == true)
  {
    tft.setCursor(130, 210);
    tft.setTextColor(BLACK, WHITE);
    tft.setTextSize(2);
    tft.print(i);
   
    if((i>50))
    { 
      flag1 == false;
      i=0;
    }
     
    TSPoint p = ts.getPoint();
  
  if (p.z > ts.pressureThreshhold) 
  {
    p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
    p.y = map(p.y, TS_MAXY, TS_MINY, 0, 480);
    Serial.print("X = "); Serial.print(p.x);
    Serial.print("\tY = "); Serial.print(p.y);
    Serial.print("\tPressure = "); Serial.println(p.z);
    
  if(p.x > 305 && p.x < 319 && p.y > 387 && p.y < 470)
  {
    i=0;
    flag1 == false;
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    break;
  }
   
   i++;
   delay(1000);
   
}
 if(p.x > 305 && p.x < 319 && p.y > 387 && p.y < 470)
  {
    i=0;
    flag1 == false;
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    break;
  }

Is that the button test code? Does it not do what you want? What does it do?

I suggest that you post the whole program. Snippets often omit important information. If the code is too large to post it can be attached. Better yet, make a minimal verifiable program that shows the problem.

Karma for code tags on the first post. Most new members fail to read the forum guidelines.

groundFungus:

 if(p.x > 305 && p.x < 319 && p.y > 387 && p.y < 470)

{
   i=0;
   flag1 == false;
   pinMode(XM, OUTPUT);
   pinMode(YP, OUTPUT);
   break;
 }



Is that the button test code? Does it not do what you want? What does it do?

I suggest that you post the whole program. [Snippets often omit important information.](https://snippets-r-us.com/) If the code is too large to post it can be attached. Better yet, make a [minimal verifiable program](https://stackoverflow.com/help/minimal-reproducible-example) that shows the problem.

Karma for code tags on the first post. Most new members fail to read the forum guidelines.
#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include "TouchScreen.h" 
#include <stdint.h>

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

#define TS_MINX 114
#define TS_MINY 122
#define TS_MAXX 940
#define TS_MAXY 915

#define YP A3
#define XM A2
#define YM 9
#define XP 8

#define BLACK        0x0000  /*   0,   0,   0 */
#define BLUE         0x001F  /*   0,   0, 255 */
#define RED          0xF800  /* 255,   0,   0 */
#define GREEN        0x07E0  /*   0, 255,   0 */
#define CYAN         0x07FF  /*   0, 255, 255 */
#define MAGENTA      0xF81F  /* 255,   0, 255 */
#define YELLOW       0xFFE0  /* 255, 255,   0 */
#define WHITE        0xFFFF  /* 255, 255, 255 */
#define NAVY         0x000F  /*   0,   0, 128 */
#define DARKGREEN    0x03E0  /*   0, 128,   0 */
#define DARKCYAN     0x03EF  /*   0, 128, 128 */
#define MAROON       0x7800  /* 128,   0,   0 */
#define PURPLE       0x780F  /* 128,   0, 128 */
#define OLIVE        0x7BE0  /* 128, 128,   0 */
#define LIGHTGREY    0xC618  /* 192, 192, 192 */
#define DARKGREY     0x7BEF  /* 128, 128, 128 */
#define ORANGE       0xFD20  /* 255, 165,   0 */
#define GREENYELLOW  0xAFE5  /* 173, 255,  47 */
#define PINK         0xF81F  /* 255,   0, 255 */

int16_t i = 0;
boolean flag1;

MCUFRIEND_kbv tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);




//******************************************************************************
void setup() 
{
  // put your setup code here, to run once:

  Serial.begin(9600);
  // Restart the LCD
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(3); // Rotation of screen(0,1,2,3)
  tft.fillScreen(WHITE); // color of BackGround

  tft.fillRect(180, 80, 120, 40, NAVY);
  tft.setCursor(210, 92);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Press");
  
  // reset variables
  flag1 = false;
  i = 0;
}
//-----------------------------------
void loop() 
  {
  while(flag1 == true)
  {
   tft.setCursor(130, 210);
   tft.setTextColor(BLACK, WHITE);
   tft.setTextSize(2);
   tft.print(i);
   if((i>10))
   {
    i=0;
    flag1 = false;
   }

  TSPoint p = ts.getPoint();
  if (p.z > ts.pressureThreshhold) 
  {
  p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
  p.y = map(p.y, TS_MAXY, TS_MINY, 0, 480);
  Serial.print("X = "); Serial.print(p.x);
  Serial.print("\tY = "); Serial.print(p.y);
  Serial.print("\tPressure = "); Serial.println(p.z);
  }

    if(p.x > 110 && p.x < 163 && p.y > 280 && p.y < 395)
    {
    i=0;
    flag1 == false;
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    break;
  }
  }

  i++;
  delay(1000);
  
  }

Thank You for the reply

I attach the full code. In fact it should show a counter at the bottom of the screen counting up to 50, and button at top that can stop the counter.
It just show the button, and I don't know why the counter part doesn't work.
The main ideal I need counter that can be stooped by screen touch button

    flag1 == false;

is a comparison, not an assignment

    flag1 = false;

most likely was intended.

didn't check for other errors.

First off. God gave you ctrl-T. It is the best part of the whole Arduino IDE.

Always format code before posting.
Personally, I format code at every opportunity.

You will see how the blocks are structured.
And you will see that pinMode() is only called on a successful Touch.

I suggest that you study the Touch examples that come with MCUFRIEND_kbv.

David.

ZinggJM:

    flag1 == false;

is a comparison, not an assignment

    flag1 = false;

most likely was intended.

didn't check for other errors.

Thank You but still doesn't work

david_prentice:
First off. God gave you ctrl-T. It is the best part of the whole Arduino IDE.

Always format code before posting.
Personally, I format code at every opportunity.

You will see how the blocks are structured.
And you will see that pinMode() is only called on a successful Touch.

I suggest that you study the Touch examples that come with MCUFRIEND_kbv.

David.

#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include "TouchScreen.h"
#include <stdint.h>

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

#define TS_MINX 114
#define TS_MINY 122
#define TS_MAXX 940
#define TS_MAXY 915

#define YP A3
#define XM A2
#define YM 9
#define XP 8

#define BLACK        0x0000  /*   0,   0,   0 */
#define BLUE         0x001F  /*   0,   0, 255 */
#define RED          0xF800  /* 255,   0,   0 */
#define GREEN        0x07E0  /*   0, 255,   0 */
#define CYAN         0x07FF  /*   0, 255, 255 */
#define MAGENTA      0xF81F  /* 255,   0, 255 */
#define YELLOW       0xFFE0  /* 255, 255,   0 */
#define WHITE        0xFFFF  /* 255, 255, 255 */
#define NAVY         0x000F  /*   0,   0, 128 */
#define DARKGREEN    0x03E0  /*   0, 128,   0 */
#define DARKCYAN     0x03EF  /*   0, 128, 128 */
#define MAROON       0x7800  /* 128,   0,   0 */
#define PURPLE       0x780F  /* 128,   0, 128 */
#define OLIVE        0x7BE0  /* 128, 128,   0 */
#define LIGHTGREY    0xC618  /* 192, 192, 192 */
#define DARKGREY     0x7BEF  /* 128, 128, 128 */
#define ORANGE       0xFD20  /* 255, 165,   0 */
#define GREENYELLOW  0xAFE5  /* 173, 255,  47 */
#define PINK         0xF81F  /* 255,   0, 255 */

int16_t i = 0;
boolean flag1;

MCUFRIEND_kbv tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);




//******************************************************************************
void setup()
{
  // put your setup code here, to run once:

  Serial.begin(9600);
  // Restart the LCD
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(3); // Rotation of screen(0,1,2,3)
  tft.fillScreen(WHITE); // color of BackGround

  tft.fillRect(180, 80, 120, 40, NAVY);
  tft.setCursor(210, 92);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Press");

  // reset variables
  flag1 = false;
  i = 0;
}
//-----------------------------------
void loop()
{
  while (flag1 == true)
  {
    tft.setCursor(130, 210);
    tft.setTextColor(BLACK, WHITE);
    tft.setTextSize(2);
    tft.print(i);
    if ((i > 10))
    {
      i = 0;
      flag1 = false;
    }

    TSPoint p = ts.getPoint();
    if (p.z > ts.pressureThreshhold)
    {
      p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
      p.y = map(p.y, TS_MAXY, TS_MINY, 0, 480);
      Serial.print("X = "); Serial.print(p.x);
      Serial.print("\tY = "); Serial.print(p.y);
      Serial.print("\tPressure = "); Serial.println(p.z);
    }

    if (p.x > 110 && p.x < 163 && p.y > 280 && p.y < 395)
    {
      i = 0;
      flag1 = false;
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
      break;
    }
  }

  i++;
  delay(1000);

}

david_prentice:
First off. God gave you ctrl-T. It is the best part of the whole Arduino IDE.

Always format code before posting.
Personally, I format code at every opportunity.

You will see how the blocks are structured.
And you will see that pinMode() is only called on a successful Touch.

I suggest that you study the Touch examples that come with MCUFRIEND_kbv.

David.

Thank You, I really don't know about ctrl-T before "Still New",
I was searching about the samples coming with MCUfriend but unfortunately nothing like this.
I appreciate any help from anubody

OK, haven't figured your problem, but you should not be using "while" loops. What you want here is simply "if", not "while". :grinning:

No, hang on, I have! The problem is that you start by setting the flag to false, and then run a loop which only ever actually executes anything at all (well, that is other than incrementing i each second) if it is true, which it is not. Ergo, never!

Everything else is in the conditional section. That is why the proper formatting is so critical to seeing what is happening.