Touch screen not displaying labels as expected

Status
Not open for further replies.

narco

Member
Hi guys and gals

This is part of my first project.

I'm trying to get my tft touchscreen to work, and it isn't drawing as expected. I'm expecting with the following code, to get 8 rectangles with the labels from the array "lengths" on them. But there seem to be a number of labels appearing that should not be there, including some that shouldn't be anywhere (like "40" which has a zero in it for instance), and some dots of the beginnings of something on the edge of the screen..


Code:
#include <XPT2046_Touchscreen.h>

#include <ILI9341_t3.h>

// This is calibration data for the raw touch data to the screen coordinates
// Warning, These are
#define TS_MINX 337
#define TS_MINY 529
#define TS_MAXX 3729
#define TS_MAXY 3711

#define TFT_DC  9
#define TFT_CS 10
#define TFT_RST 7
#define TFT_SCK 13
#define TFT_MISO 12
#define TFT_MOSI 11

#define TOUCH_CS  8

XPT2046_Touchscreen ts(TOUCH_CS);
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCK, TFT_MISO);


void drawButtons()  {
  // Define some variables.
  int numRows = 4;
  int numColumns = 2;
  float rowHeight = tft.height() / numRows;
  float columnWidth = tft.width() / numColumns;
  int color;
  int lengths[] = {1, 2, 4, 8, 16, 32, 48, 64};


  for (int i = 0; i <= numRows; i++) {
    for (int j = 0; j <= numColumns; j++) {
      int cellNum = i * numColumns + j;
      
      if ((j + i) % 2 == 0) {
        color = ILI9341_RED;
        tft.setTextColor(ILI9341_WHITE);
      } else {
        color = ILI9341_WHITE;
        tft.setTextColor(ILI9341_RED);
      }

      tft.fillRect(j * columnWidth, i * rowHeight, columnWidth, rowHeight, color);
      tft.setCursor(j * columnWidth + columnWidth/2.5 ,  i * rowHeight + (rowHeight / 2.5));
      tft.setTextSize(2);
      
      String label = String(lengths[cellNum]);
      tft.println(label);
    }
  }
}

yet what I'm getting is:
 

Attachments

  • fullsizeoutput_19fd.jpeg
    fullsizeoutput_19fd.jpeg
    23.9 KB · Views: 81
LOL!

How embarrassing.

I copied the syntax for a for loop from another place without checking. Doh!

thanks :)
 
Status
Not open for further replies.
Back
Top