qwerty key pad for ILI9341 2.8" multi wire & DUE

Members

The code below returns a number instead of a character. The numbers 0 to 9 returns the correct number as its in sequence with the "0", "1" etc.

Anything after that ie "Q","W" returns the number 10 and 11.

How can code be used/added to convert the number into the corresponding key touch?

/*Hard copy printed 25 july
  For a capacitive touch screen.
  Creates a grid of numbered rectangles, each of
  which will trap touches and report its id.
  Any of the four screen rotations may be used.

 ++ Program copied from Arduino forum++

 **and edited for use with DUE and 2.8" UNO shield ILI9341
  multi wire.
*/


#include <Adafruit_GFX.h>
#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

// Default values for UNO shield. Multi wire.
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define YP A2  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM 8   // can be a digital pin
#define XP 9   // can be a digital pin

#define isWithin(x,a,b) ((x>=a) && (x<=b))

#define MINPRESSURE 10
#define MAXPRESSURE 1000
#define TS_MINX 167
#define TS_MINY 228
#define TS_MAXX 880
#define TS_MAXY 860

#define BLACK   0x0000
#define BLUE    0x001F
#define WHITE   0xFFFF

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 365);
int X, Y, Z;
typedef struct {
  int left;
  int top;
  int width;
  int height;
} rectangle;
const byte rotation = 2;
const byte col = 10;
const byte row = 4;

rectangle rect[col * row];
const char *btnTitle[col * row] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "_", "Z", "X", "C", "V", "B", "N", "M", "/", "<", "#"};

void showGrid()
{

  int left, top;
  int l = 5; //LEFT
  int t = 225; //TOP
  int w = 20;  //WIDTH
  int h = 20;  //HEIGHT
  int hgap = 3;
  int vgap = 3;
  byte id = 0;
  for (byte j = 0; j < row; j++)
  {
    for (byte i = 0; i < col; i++)
    {
      left = l + i * (w + vgap);
      top = t + j * (h + hgap);
      rect[id].left = left;
      rect[id].top = top;
      rect[id].width = w;
      rect[id].height = h;
      tft.drawRect( left, top, w, h, BLUE);
      tft.setCursor(left + 4, top + 2);
      tft.print(btnTitle[id]);
      id++;
    }
  }
}

void showZeroZero() {
 // tft.setCursor(0, 0);
 // tft.print("0,0");
}

void setup() {
  Serial.begin(9600);
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(rotation);  //2
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  showZeroZero();
  showGrid();
}

void loop() {
  static uint16_t x, y;

  TSPoint p = ts.getPoint();

  if (p.z > ts.pressureThreshhold) {
    p.y = map(p.y, TS_MINY, TS_MAXY, 0, 240);
    p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320);
    X = tft.height() - map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
    Y = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
    Z = p.z;

    if (rotation == 2) {
      x = p.x;
      y = 240-p.y;
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
    }
    for (byte count = 0; count < (col * row); count++)
    {
      if (isWithin(y, rect[count].left, (rect[count].left + rect[count].width)) && isWithin(x, rect[count].top, (rect[count].top + rect[count].height)))
      {
        showTouchData (x, y, count);

      }
    }
  }
}

void showTouchData(int xCoord, int yCoord, int id) {
  tft.fillRect (10, 30, tft.width() - 10, 20, BLACK);
  tft.setCursor(10, 30);
  tft.print(F(" id=")); tft.print(id);
}

Thanks in advance

/*Hard copy printed 25 july
  For a capacitive touch screen.
  Creates a grid of numbered rectangles, each of
  which will trap touches and report its id.
  Any of the four screen rotations may be used.
  Program copied from Arduino forum
 **edited for use with DUE and 2.8" UNO shield ILI9341
  multi wire.
*/


#include <Adafruit_GFX.h>
#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

// Default values for UNO shield. Multi wire.
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define YP A2  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM 8   // can be a digital pin
#define XP 9   // can be a digital pin

#define isWithin(x,a,b) ((x>=a) && (x<=b))

#define MINPRESSURE 10
#define MAXPRESSURE 1000
#define TS_MINX 167
#define TS_MINY 228
#define TS_MAXX 880
#define TS_MAXY 860

#define BLACK   0x0000
#define BLUE    0x001F
#define WHITE   0xFFFF

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 365);
int X, Y, Z;
typedef struct {
  int left;
  int top;
  int width;
  int height;
} rectangle;
const byte rotation = 2;
const byte col = 10;
const byte row = 4;

rectangle rect[col * row];
const char *btnTitle[col * row] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "_", "Z", "X", "C", "V", "B", "N", "M", "/", "<", "#"};

void showGrid()
{

  int left, top;
  int l = 5; //LEFT
  int t = 210; //TOP
  int w = 20;  //WIDTH
  int h = 25;  //HEIGHT
  int hgap = 3;
  int vgap = 3;
  byte id = 0;
  for (byte j = 0; j < row; j++)
  {
    for (byte i = 0; i < col; i++)
    {
      left = l + i * (w + vgap);
      top = t + j * (h + hgap);
      rect[id].left = left;
      rect[id].top = top;
      rect[id].width = w;
      rect[id].height = h;
      tft.drawRect( left, top, w, h, BLUE);
      tft.setCursor(left + 4, top + 2);
      tft.print(btnTitle[id]);
      id++;
    }
  }
}

void showZeroZero() {
  tft.setCursor(0, 0);
  tft.print("0,0");
}

void setup() {
  Serial.begin(9600);
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(rotation);  //2
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  showZeroZero();
  showGrid();
}

void loop() {
  static uint16_t x, y;

  TSPoint p = ts.getPoint();

  if (p.z > ts.pressureThreshhold) {
    p.y = map(p.y, TS_MINY, TS_MAXY, 0, 240);
    p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320);
    X = tft.height() - map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
    Y = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
    Z = p.z;

    if (rotation == 2) {
      x = p.x - 5;     //calibration for qwerty touch
      y = 240 - p.y; //calibration for qwerty touch and y axis orientation
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
    }
    for (byte count = 0; count < (col * row); count++)
    {
      if (isWithin(y, rect[count].left, (rect[count].left + rect[count].width)) && isWithin(x, rect[count].top, (rect[count].top + rect[count].height)))
      {
        showTouchData (x, y, count);

      }
    }
  }
}

void showTouchData(int xCoord, int yCoord, int id) {
  tft.fillRect (10, 30, tft.width() - 10, 50, BLACK);
  tft.setCursor(10, 30);
  tft.print(F("x=")); tft.print (xCoord);
  tft.print(F("y=")); tft.print (yCoord);
  tft.setCursor(10, 45);
  if (id == 0) {
    tft.print("0");

  }
  if (id == 1) {
    tft.print("1");

  }
  if (id == 2) {
    tft.print("2");

  }

  if (id == 3) {
    tft.print("3");
  }
  if (id == 4) {
    tft.print("4");
  }

  if (id == 5) {
    tft.print("5");
  }
  if (id == 6) {
    tft.print("6");
  }

  if (id == 7) {
    tft.print("7");
  }
  if (id == 8) {
    tft.print("8");
  }

  if (id == 9) {
    tft.print("9");
  }
  if (id == 10) {
    tft.print("Q");
  }

  if (id == 11) {
    tft.print("W");
  }
  if (id == 12) {
    tft.print("E");
  }

  if (id == 13) {
    tft.print("R");
  }
  if (id == 14) {
    tft.print("T");
  }

  if (id == 15) {
    tft.print("Y");
  }
  if (id == 16) {
    tft.print("U");
  }

  if (id == 17) {
    tft.print("I");
  }
  if (id == 18) {
    tft.print("O");
  }

  if (id == 19) {
    tft.print("P");
  }
  if (id == 20) {
    tft.print("A");
  }

  if (id == 21) {
    tft.print("S");
  }
  if (id == 22) {
    tft.print("D");
  }
  if (id == 23) {
    tft.print("F");
  }
  if (id == 24) {
    tft.print("G");
  }

  if (id == 25) {
    tft.print("H");
  }
  if (id == 26) {
    tft.print("J");
  }

  if (id == 27) {
    tft.print("K");
  }
  if (id == 28) {
    tft.print("L");
  }

  if (id == 29) {
    tft.print("_"); //space bar
  }
  if (id == 30) {
    tft.print("Z");
  }

  if (id == 31) {
    tft.print("X");
  }
  if (id == 32) {
    tft.print("C");
  }

  if (id == 33) {
    tft.print("V");
  }
  if (id == 34) {
    tft.print("B");
  }

  if (id == 35) {
    tft.print("N");
  }
  if (id == 36) {
    tft.print("M");
  }
  if (id == 37) {
    tft.print("/");
  }
  if (id == 38) {
    //tft.drawRect(coords should be someting like n,n+6,n-height,height+5); //need to draw a rect with coords of prev number. Then use 
//tft.setCursor(n, 30); to re print.
    tft.print(" "); //This is the delete button '<'
  }
  if (id == 39) {
    tft.print("Save button"); //need to do someting here #
  }

} //END