Problem clearing touch data. [SOLVED]

This happens on both the DUE R3 & MEGA 2560
Mega uses TFT LCD Mega Shield V2.2
Due uses a Sainsmart knockoff CTE TFT LCD/SD Shield
Both use the Sainsmart 320qvt.
Please refrain from discussing that Sainsmart products are junk, as I have figured that part out already.

void setup()
{
  PaNum = 0;
  myGLCD.setColor(0, 0, 0);
    myGLCD.drawRoundRect(73, 12, 246, 47);
    myGLCD.drawRoundRect(13, 59, 306, 94);
    myGLCD.drawRoundRect(13, 106, 306, 141);
    myGLCD.drawRoundRect(13, 145, 306, 180);
    myGLCD.drawRoundRect(13, 192, 153, 227);
    myGLCD.drawRoundRect(166, 192, 306, 227);
//BUTTON SHADING
  myGLCD.setColor(84, 84, 84);
    myGLCD.drawRect(74, 13, 245, 46);
    myGLCD.drawRect(14, 60, 305, 93);
    myGLCD.drawRect(14, 107, 305, 140);
    myGLCD.drawRect(14, 146, 305, 179);
    myGLCD.drawRect(14, 193, 152, 226);
    myGLCD.drawRect(167, 193, 305, 226);

  myGLCD.setColor(105, 105, 105);
    myGLCD.drawRect(75, 14, 244, 45);                               
    myGLCD.drawRect(15, 61, 304, 92);
    myGLCD.drawRect(15, 108, 304, 139);
    myGLCD.drawRect(15, 147, 304, 178);
    myGLCD.drawRect(15, 194, 151, 225);
    myGLCD.drawRect(168, 194, 304, 225);
  
  myGLCD.setColor(126, 126, 126);
    myGLCD.drawRect(76, 15, 243, 44);                              
    myGLCD.drawRect(16, 62, 303, 91);
    myGLCD.drawRect(16, 109, 303, 138);
    myGLCD.drawRect(16, 148, 303, 177);
    myGLCD.drawRect(16, 195, 150, 224);
    myGLCD.drawRect(169, 195, 303, 224);
  
  myGLCD.setColor(147, 147, 147);
    myGLCD.drawRect(77, 16, 242, 43);                               
    myGLCD.drawRect(17, 63, 302, 90);
    myGLCD.drawRect(17, 110, 302, 137);
    myGLCD.drawRect(17, 149, 302, 176);
    myGLCD.drawRect(17, 196, 149, 223);
    myGLCD.drawRect(170, 196, 302, 223);
  
  myGLCD.setColor(168, 168, 168);
    myGLCD.drawRect(78, 17, 241, 42);                               
    myGLCD.drawRect(18, 64, 301, 89);
    myGLCD.drawRect(18, 111, 301, 136);
    myGLCD.drawRect(18, 150, 301, 175);
    myGLCD.drawRect(18, 197, 148, 222);
    myGLCD.drawRect(171, 197, 301, 222);
  
  myGLCD.setColor(189, 189, 189);
    myGLCD.drawRect(79, 18, 240, 41);                               
    myGLCD.drawRect(19, 65, 300, 88);                             //2
    myGLCD.drawRect(19, 112, 300, 135);                            //3
    myGLCD.drawRect(19, 151, 300, 174);                            //5
    myGLCD.drawRect(19, 198, 147, 221);                           //7
    myGLCD.drawRect(172, 198, 300, 221);                          //8

  myGLCD.setColor(210, 210, 210);
    myGLCD.drawRect(80, 19, 239, 40);                               
    myGLCD.drawRect(20, 66, 299, 87);                             //2
    myGLCD.drawRect(20, 113, 299, 134);                            //3
    myGLCD.drawRect(20, 152, 299, 173);                            //5
    myGLCD.drawRect(20, 199, 146, 220);                           //7
    myGLCD.drawRect(173, 199, 299, 220);                          //8

  myGLCD.setColor(231, 231, 231);
    myGLCD.drawRect(81, 20, 238, 39);                               
    myGLCD.drawRect(21, 67, 298, 86);                             //2
    myGLCD.drawRect(21, 114, 298, 133);                            //3
    myGLCD.drawRect(21, 153, 298, 172);                            //5
    myGLCD.drawRect(21, 200, 145, 219);                           //7
    myGLCD.drawRect(174, 200, 298, 219);                          //8
//OUTLINING
  myGLCD.setColor(0, 0, 0);                                     
    myGLCD.drawRect(22, 115, 297, 132);                          //3
    myGLCD.drawRect(22, 154, 297, 171);                          //5
    myGLCD.drawRect(22, 201, 144, 218);                         //7
    myGLCD.drawRect(175, 201, 297, 218);                        //8

//4
    myGLCD.drawLine(14, 107, 21, 114);                          //4
    myGLCD.drawLine(14, 140, 21, 133);
    myGLCD.drawLine(305, 140, 298, 133);
    myGLCD.drawLine(305, 107, 298, 114);                        //4
//6
    myGLCD.drawLine(14, 146, 21, 153);                          //6
    myGLCD.drawLine(14, 179, 21, 172);
    myGLCD.drawLine(305, 179, 298, 172);
    myGLCD.drawLine(305, 146, 298, 153);                        //6
//7
    myGLCD.drawLine(14, 193, 21, 200);                          //7
    myGLCD.drawLine(14, 226, 21, 219);
    myGLCD.drawLine(152, 226, 145, 219);
    myGLCD.drawLine(152, 193, 145, 200);                        //7
//8
    myGLCD.drawLine(167, 193, 174, 200);                        //8
    myGLCD.drawLine(167, 226, 174, 219);
    myGLCD.drawLine(305, 226, 298, 219);
    myGLCD.drawLine(305, 193, 298, 200);                        //8
//TEXT BACKGROUND
  myGLCD.setColor(252, 252, 252);
    myGLCD.fillRect(81, 20, 238, 38);
    myGLCD.fillRect(22, 68, 297, 85);
    myGLCD.fillRect(23, 116, 296, 131);
    myGLCD.fillRect(23, 155, 296, 170);
    myGLCD.fillRect(23, 202, 143, 217);
    myGLCD.fillRect(176, 202, 296, 217);
}

void loop()
{
  TouchScreenLoop();
}

void TouchScreenLoop()
{
  short x, y;
  while (true)
    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x = myTouch.getX();
      y = myTouch.getY();
      if ((x!=-1) and (y!=-1))
      {
        if ((x>=14) && (x<=305) && (y>=106) && (y<=141) && (PaNum == 0))
        {
          myGLCD.clrScr();
          drawpage1();
        }
        if ((x>=0) && (x<=319) && (y>=0) &&(y<=47) && (PaNum >= 1))
        {
          myGLCD.clrScr();
          drawHome();
        }
        if ((x>=13) && (x<=306) && (y>=106) && (y<=141) && (PaNum == 1) && (Select1 = 0))
        {
          Select1 = 1;
          drawSelect1();
        }
        if ((x>=13) && (x<=306) && (y>=106) && (y<=141) && (PaNum == 1) && (Select1 = 1))
        {
          Select1 = 0;
          drawDeselect1();
        }
        if ((x>=13) && (x<=306) && (y>=145) && (y<=180) && (PaNum == 1) && (Select2 = 0))
        {
          Select2 = 1;
          drawSelect2();
        }
        if ((x>=13) && (x<=306) && (y>=145) && (y<=180) && (PaNum == 1) && (Select2 = 1))
        {
          Select2 = 0;
          drawDeselect2();
        }
        if ((x>=34) && (x<=285) && (y>=59) && (y<=94) && (PaNum == 1) && ((Select1 = 0) || (Select2 = 0)))
        {
          Select1 = 1;
          Select2 = 1;
          drawSelect1();
          drawSelect2();
        }
      }
    }
}

The basics here are:
When I touch the only variable located on PaNum 0, touch data is still available after drawpage1(); is executed. Likewise when I touch the location to execute drawSelect1(); it decides that touch data should be used again and thus executes drawDeelect1();

Be that as it may, it continues to use data and executes an undesired secondary function.
Is there something I can do like switch, case, break or a clear data function I can use that will stop the Arduino from continuing to use the UTouch data after I lift my finger off the screen.

I have tried using delays in multiple places with no avail.
When I searched on google, it brought me to a calibration fix that seemed to work for many, however, both of my 320qvt’s have no calibration issues.

Weman:
I FIXED IT on my Sainsmart board.

Me too has found the joy in arduino and bought a cheap sainsmart Mega 2560 from Asia, with shield and Touch-TFT.
The touch did not work at all, not possible to calibrate. I studied the data waves with oscilloscope and found that the signals from touch IC XPT2046, in to the mega was ugly due to the 10K resistor. The resistor should not be needed as the touch IC signal is 3,3V but it has over and undershoot pikes, so it probably needs some filtering. When I used the oscilloscope probe without damping resistor in probe it suddenly worked. The oscilloscope adds a load of 1M ohm. So I started experimenting. After replacing the 10K resistor at D_OUT with 50K and connecting 10K between touch IC side of the 50K and to ground, it works and after calibration everything is perfekt.
Strange though, as this still makes the data signal look bad with curved flanks. I wonder if the problem is in the TFT, sheild or in the mega board (all from sainsmart), but clearly the data signal needs some attention, it has nothing to do with the software.

Thanks Henning for the librarys.
/ Weman

This did not fix it either.
I have tried setting x=0; y=0; directly after the if statements to clear the data.
I started out with if ((x!=-1) and (y!=-1)) not included, and honestly, this is the only line of my entire code I haven’t the slightest clue of its meaning.

SOLVED:

I changed all of my subsequent if statements to be else if statements. I also made sure all of my comparisons inside said if statements were comparisons and not definitions, example == rather than =.
I dropped some of the code out that was not needed and some delays to improve functionality.
On a side note, it helps a ton to pull out that Arduino Programming text and brush up on a chapter or two. :grin:

void TouchScreenLoop()
{
if (myTouch.dataAvailable() ==true){
      myTouch.read();
      x = myTouch.getX(); // X coordinate where the screen has been pressed
      y = myTouch.getY(); // Y coordinates where the screen has been pressed  
      delay(50);
      if ((x>=14) && (x<=305) && (y>=106) && (y<=141) && (PaNum == 0))
        {
          drawpage1();
        }
        else if ((x>=0) && (x<=319) && (y>=0) &&(y<=47) && (PaNum >= 1))
        {
          drawHome();
        }
        else if ((x>=13) && (x<=306) && (y>=106) && (y<=141) && (PaNum == 1) && (Select1 = 0))
        {
          Select1 = 1;
          drawSelect1();
          delay(50);
        }
        else if ((x>=13) && (x<=306) && (y>=106) && (y<=141) && (PaNum == 1) && (Select1 = 1))
        {
          Select1 = 0;
          drawDeselect1();
          delay(50);
        }
        else if ((x>=13) && (x<=306) && (y>=145) && (y<=180) && (PaNum == 1) && (Select2 = 0))
        {
          Select2 = 1;
          drawSelect2();
          delay(50);
        }
        else if ((x>=13) && (x<=306) && (y>=145) && (y<=180) && (PaNum == 1) && (Select2 = 1))
        {
          Select2 = 0;
          drawDeselect2();
          delay(50);
        }
        else if ((x>=34) && (x<=285) && (y>=59) && (y<=94) && (PaNum == 1) && ((Select1 = 0) || (Select2 = 0)))
        {
          Select1 = 1;
          Select2 = 1;
          drawSelect1();
          drawSelect2();
          delay(50);
        }
      }
    }

ACE27:
I started out with if ((x!=-1) and (y!=-1)) not included, and honestly, this is the only line of my entire code I haven't the slightest clue of its meaning.

if (( x not equal -1) && (y not equal -1)) will only run what follows this statement when both x and y are not -1.

Pleased you got it sorted.

G

ghlawrence2000:
if (( x not equal -1) && (y not equal -1)) will only run what follows this statement when both x and y are not -1.

Pleased you got it sorted.

G

Awesome, it makes sense now. Thank you!

It’s a sad sad day though. :frowning: I think I fried my chip. Or at least pins 44-51. I accidentally shorted my V-in to ground for like a split second, and now my display is a bit wonky.
My 16x2 LCD display works as it should but

Anything on the screen with a value of (x>~275) shows up decent but not perfect.
Everything on the screen with a value of (x<~275) is just a gray screen.
I Created a sketch, latched pin 1-53 in the HIGH state.

44-51 all have an output of <=1.5V
Only 3 of the other pins are actually hitting 3V
All the rest are ((V>=2.5) && (V<=2.8))
Voltage does not fluctuate on any given pin.

Searching the webs to see if I can somehow reroute 44-51 to 14-21 by editing the .h files or some other method, but I doubt it’s even worth the try.

I think it’s off to amazon to buy a new board. I would try to change out the 3.3V regulator, but seeing as there is no consistency in voltage between all the pins like there should be, I’m chalking this up as a fried chip.

The moral to this story? Don't piss about with Vin when there is a perfectly good barrel jack receptacle..... :frowning:

Regards,

Graham

ghlawrence2000:
The moral to this story? Don't piss about with Vin when there is a perfectly good barrel jack receptacle..... :frowning:

Regards,

Graham

:o
I read your post, but chose to ignore it. I apologize, but I had an absolute goal in mind. As we all know, Arduino is a prototyping platform and I was and am in need of utilizing only one barrel connector for my entire project. The Neopixels I am using require as much as 1.5Amps alone. Powering them through the DUE's 5V rail has never been or will ever be an option as that is too much for any Arduino to handle let alone the DUE. So, using 2 power supplies, one for the breadboard and one for the DUE would have been the route I needed to go. Except I knew there had to be an easier way.

Que my dilemma, "How can I power the Arduino with let's say 9V but also supply my breadboard components with a 5V?"

Before I started my project I had a few of these on hand. Breadboard Power Supply I know right, I'm an idiot. What was I thinking trying to draw 1500 mA through a 700mA regulator.

Long story short I built my own power supply capable of my needs. Seeing as V-in bypasses the diode I made sure my connection came after the diodes, the fuses, and tied to a 100uF cap.

I lied, I didn't ignore your post. I headed your warning. I shrink wrapped my connections and vowed to never touch a connection unless I power down first.

;D

Sounds like a positive outcome was achieved in the end , well done, looks good by the way.

Regards,

Graham