MyTouch Coordinates Not Displaying Correctly on Screen

Hi, I’m having a lot of trouble with something that should be pretty simple I would think.

I’m pulling the coordinates of a 5" touch screen and trying to display the on the screen.

When I use
myGLCD.printNumI(positionY, 35, 30);
it always displays 3 digits once 3 digits have been reached. For example if I scroll from Y=0 down to Y=250 it shows the correct numbers, then if I go from Y=250 up to Y=0 it won’t drop the last 2 digits, they remain as they were unless I reset the arduino then it displays correctly.

For instance if I scrolled from Y=300 up to Y=34 it will show something like 342, the 2 will always stay like that until I go back into the hundreds and it needs to change. So with last touch at Y=34 and display showing 342 I reset and when it comes on it shows 34 as it should.

Its good until I hit 3 digits again.

When I use
myGLCD.printNumF(positionY, 1, 35, 30);
it displays correctly but shows up to 3 decimal points when in the single digit numbers like 3.000 or 13.00 or 130.0 Of course I don’t want that.

Here’s my sketch.

#include <UTFT.h>
#include <UTouch.h>
#include <EEPROM.h>  // used to store and retrieve settings from memory

extern uint8_t BigFont[];

int positionY = 0;

UTFT myGLCD(CTE50,38,39,40,41); // start up an instance of the TFT screen
UTouch myTouch(6,5,4,3,2);

void setup()
{
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);

  myTouch.InitTouch(PORTRAIT);
  myTouch.setPrecision(PREC_HI);

  myGLCD.setColor(255,255,255);
  
  Serial.begin(9600);  
}

void loop()
{
  processMyTouch();  
}

void processMyTouch()
{
  int x, y;
  while (myTouch.dataAvailable() == true)
  {
  myTouch.read();
  x = myTouch.getX();
  y = myTouch.getY();

  positionY = myTouch.getY();
    
  EEPROM.write(1, positionY); // 0 // last feeding data saved (0 for no, 1 for yes)
  
  myGLCD.printNumF(positionY, 1, 35, 30);
  myGLCD.printNumI(positionY, 35, 60);
  }
}

Any help would be appreciated.

it won't drop the last 2 digits, they remain as they were unless I reset the arduino then it displays correctly.

You could check the width of the number before you print it, and pad with spaces.

it always displays 3 digits once 3 digits have been reached. For example if I scroll from Y=0 down to Y=250 it shows the correct numbers, then if I go from Y=250 up to Y=0 it won't drop the last 2 digits, they remain as they were unless I reset the arduino then it displays correctly.

I think I understand your problem even though you are expressing it in an odd way.

If I understand correctly, when you display numbers as they increase from say 1 to 250 they display OK but if you display a 2 digit number after displaying a 3 digit one the last digit of the 3 digit number is still on the screen. If that is what is happening then it should be no surprise because you have not written over the third digit. So, how to fix it ? One way is to test the size of the number and if it is below 100 print a space after it to get rid of any possible third digit. A smarter way would be to spot that you had just gone from 100 to 99 or less and only print the space then.

The same principle applies when going below 10 of course.

If I have misunderstood the problem can you please explain it more clearly, perhaps take a picture or two and post them.

Thanks guys that worked perfectly!

myGLCD.print(" ",165,190);
myGLCD.printNumI(boost, 160, 190)

I interpret that as printing a space at 165, 190 then printing the value of boost at 160, 190, ie before the space. Is that what it does and if so is that what you want to do ?

UKHeliBob:

myGLCD.print(" ",165,190);

myGLCD.printNumI(boost, 160, 190)



I interpret that as printing a space at 165, 190 then printing the value of boost at 160, 190, ie before the space. Is that what it does and if so is that what you want to do ?

I think I got it now. Is there a way to optimize this?

Basically what I’m trying to do is display -28 to 45 without decimals and showing single digits as well. Right now what I’m doing is just covering up the already printed characters with spaces.

Here’s the code (it’s almost working, just need to adjust x,y)

  if (boost*2.036021 <=-10)
       {
      boost = boost*2.036021;
     
      myGLCD.printNumI(boost, 160, 190);
      
}
else if (boost*2.036021 < 0)
{
  boost = boost*2.036021;
  myGLCD.printNumI(boost, 160, 190);
}

 else if (boost >= 10)
      {
        myGLCD.print("  ", 200, 190);
       myGLCD.printNumI(boost, 160, 190);
      }
        else if (boost > 0)
      {
        myGLCD.print(" ", 160, 190);
        myGLCD.print("  ", 190, 190);
        myGLCD.printNumI(boost, 160, 190);
      }

The only other thing you can do really, is this.

As long as your text background is not transparent, this should work with no problems.

char value[4] = {0}; // make an array large enough to hold your variable. 3 digits + 1 null terminator
sprintf(value, "%03d", boost); // convert the number to an ascii string
myGLCD.print(value, CENTER, 190); // print the string

%03d will make everything as 000, 001, 002 … 999

If you dont want to see the preceding zeros then simply change it to just %3d

HazardsMind: The only other thing you can do really, is this.

As long as your text background is not transparent, this should work with no problems.

char value[4] = {0}; // make an array large enough to hold your variable. 3 digits + 1 null terminator
sprintf(value, "%03d", boost); // convert the number to an ascii string
myGLCD.print(value, CENTER, 190); // print the string

%03d will make everything as 000, 001, 002 ... 999

If you dont want to see the preceding zeros then simply change it to just %3d

Exactly what I needed!

Thanks for the info. I was trying all sorts for a couple hours now lol. Even skimmed thru the sprintf, but ignored it thinking it wasn't gona work since it wasn't in the UTFT library.

thinking it wasn’t gona work since it wasn’t in the UTFT library.

Nor are any of the other standard C functions but you can use any of them that are relevant to the Arduino environment with some limitations such as lack of support for formatting of floats in sprintf(), for instance.