Adafruit TFT ILI9341

Status
Not open for further replies.

jdevlami

Member
I am trying to use the following code just to test my screen and continue on my project. Adds a grid to what it should look like which is a grid of colored buttons, but also seems stretched. Currently have the MOSI connected to 11 on teensy, and 10 to CS. MISO to 12, 6 to D/C
Code:
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"

#define TFT_CS 11
#define TFT_DC 6
#define TFT_RST 0 // RST can be set to -1 if you tie it to Arduino's reset

#define XMAX 320
#define YMAX 480
#define BWIDTH 110
#define BHIGHT 90

uint16_t clr = HX8357_GREEN;

// Use hardware SPI
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);

void setup() {
  //tft.reset();
  tft.begin();
  tft.fillScreen(HX8357_BLACK);

}

void loop() {
  static bool pressed;

  tft.fillRoundRect(30, 30, BWIDTH, BHIGHT, 5, 0xFFFF - clr);
  tft.fillRoundRect(XMAX - 30 - BWIDTH, 30, BWIDTH, BHIGHT, 5, clr);
  tft.fillRoundRect(XMAX - 30 - BWIDTH, YMAX/2 - BHIGHT/2, BWIDTH, BHIGHT, 5, clr);
  tft.fillRoundRect(30, YMAX/2 - BHIGHT/2, BWIDTH, BHIGHT, 5, 0xFFFF - clr);
  tft.fillRoundRect(XMAX - 30 - BWIDTH, YMAX - 30 - BHIGHT, BWIDTH, BHIGHT, 5, clr);
  tft.fillRoundRect(30, YMAX - 30 - BHIGHT, BWIDTH, BHIGHT, 5, 0xFFFF - clr);

  uint16_t button = digitalRead(35);

  if (button)
  {
    if (!pressed)
    {
      clr = random(0xFFFF);
    }
    pressed = true;
  }
  else
  {
    pressed = false;
  }
  delay(20);
}
 
Looks like you are ysing the wrong library?
Code:
#include "Adafruit_HX8357.h"

Should be:
Code:
#include "Adafruit_ILI9341.h"
The HX8357 is a larger display with more pixels...
 
Update:
Currently none of my buttons register a push (tried using a serial plotter to show it registering the touch of my finger). Can anyone point out what is going wrong?
Code:
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "TouchScreen.h"
#include "Buttons.h"
#include <SPI.h>
#include <stdint.h>

#define BACK_CLR ILI9341_BLACK
#define numButt 4
Button b[numButt];

Button bSingle[numButt];           // single screen
int bSize = sizeof(Button);
#define TFT_CS_0 10
#define TFT_DC_0 6
#define TFT_RST 0
#define numScr 1
#define XP 3
#define YP 1
#define XM 2
#define YM 4
Adafruit_ILI9341 tft = { Adafruit_ILI9341(TFT_CS_0, TFT_DC_0, TFT_RST)};    // screen
// measured 282 ohms accross the X plate
TouchScreen ts = { TouchScreen(XP, YP, XM, YM, 298)};      // touch screen

void setup() {
  Serial.begin(9600);
  
  tft.begin();
  tft.fillScreen(BACK_CLR);
  // initialise buttons on screen
  int xOffset = 30;                                 // offset from x edge
  int yOffset = 30;                                 // offset from y edge
  int numBX = 2;                                    // number of buttons along the x axis per screen
  int numBY = 3;                                    // number of buttons along the y axis per screen
  int xDiff = (tft.width()) / numBX;             // distance between buttons on x axis per screen
  int yDiff = (tft.height()) / numBY;            // distance between buttons on y axis per screen
  for (int i = 0; i < numBX; i++) {
    for (int j = 0; j < numBY; j++) {
      b[j+numBY * i] = B_Init_Def(b[j + numBY * i],xOffset + i * xDiff,yOffset + j * yDiff);
      
    }
  }
  for (int i = 0; i < numButt; i++) {
    tft.fillRoundRect(b[i].x, b[i].y, b[i].width, b[i].height, b[i].r, b[i].clr);
  }
  
}

void loop() {
 static boolean set = false;
 if (!set) {
   set = true;
   setupSingle();
 }
 singleScreen();

}
 void setupSingle() {
  digitalWrite(LED_BUILTIN,1);

  // fill other screens with grey, this may not be possible
  for (int i=0; i < numScr; i++)
    if (i != 1)
      tft.fillScreen(0x8410);
  tft.fillScreen(BACK_CLR);
  
  // initialise buttons on left screen
  int bHeight = DEF_B_HEIGHT - 20;                   // height of buttons
  int bWidth = DEF_B_WIDTH - 42;                     // width of buttons
  int xOffset = 20;                                 // offset from x edge
  int yOffset = 30;                                 // offset from y edge
  int numBX = 2;                                    // number of buttons along the x axis per screen
  int numBY = 3;                                    // number of buttons along the y axis per screen
  int xDiff = (tft.width()) / numBX;    // distance between buttons on x axis per screen
  int yDiff = (tft.height()) / numBY;   // distance between buttons on y axis per screen

  // place buttons in an even grid formation
  bSingle[0] = B_Init(bSingle[0], xOffset + 1 *xDiff, yOffset + 1 * yDiff, bHeight, bWidth, 5, 0x001F, 1);
  bSingle[1] = B_Init(bSingle[1], xOffset + 0 *xDiff, yOffset + 0 * yDiff, bHeight, bWidth, 5, DEF_B_CLR, 1);
  bSingle[2] = B_Init(bSingle[2], xOffset + 0 *xDiff, yOffset + 1 * yDiff, bHeight, bWidth, 5, 0x07FE, 1);
  bSingle[3] = B_Init(bSingle[3], xOffset + 0 *xDiff, yOffset + 2 * yDiff, bHeight, bWidth, 5, DEF_B_CLR, 1);
  
  
  // Draw to screens
  for (int i = 0; i < numButt; i++) {
    tft.fillRoundRect(bSingle[i].x, bSingle[i].y, bSingle[i].width, bSingle[i].height, bSingle[i].r, bSingle[i].clr);
  }
 }

 void singleScreen() {
  static int led = 1;
  static int cnt = 0;
  
  if (cnt++ > 200) {
    digitalWrite(LED_BUILTIN, led ^= 1);
    cnt = 0;
  }
  
  // If touchscreen pressed
  static int fCnt = 0;
  static int lastX = 0, lastY = 0;


  // Read from touchscreens
  TSPoint p = ts.getPoint();
  
  // When touch detected    
  if (p.z > ts.pressureThreshhold) {
    
    // scale from 0->1023 to tft.width
    p.x = map(p.x, 920, 120, tft.width(), 0);
    p.y = map(p.y, 935, 75, tft.height(), 0);

    // button press to change color
    if (!digitalRead(5))
    {
      // only when pressed, not continuously
      if (fCnt > 30) {
        for (int i = 0; i < numButt; i++) {
          if (isPressed(bSingle[i], p.x, p.y)) {

            // if cyan, change to green
            if (bSingle[i].clr == 0xF81F) {
              bSingle[i].clr = ILI9341_GREEN;
              Serial.print("Channel "); Serial.print(i, DEC); Serial.println(" connected");

              // all other green buttons change magenta
              for (int j = 0; j  < numButt; j++) {
                if (j != i) {
                  if (bSingle[j].clr == ILI9341_GREEN) {
                    bSingle[j].clr = 0xF81F;
                    tft.fillRoundRect(bSingle[j].x, bSingle[j].y, bSingle[j].width, bSingle[j].height, bSingle[j].r, bSingle[j].clr);
                    Serial.print("Channel "); Serial.print(j, DEC); Serial.println(" disconnected");
                  }
                }
              }
            }
            else if (bSingle[i].clr == ILI9341_GREEN) {
              // change to magenta if green
              bSingle[i].clr = 0xF81F;//random(0xFFFF);
              Serial.print("Channel "); Serial.print(i, DEC); Serial.println(" disconnected");
              for (int j = 0; j  < numButt; j++) {
                if (j != i) {
                  if (bSingle[j].clr == ILI9341_GREEN) {
                    bSingle[j].clr = 0xF81F;
                    tft.fillRoundRect(bSingle[j].x, bSingle[j].y, bSingle[j].width, bSingle[j].height, bSingle[j].r, bSingle[j].clr);
                    Serial.print("Channel "); Serial.print(j, DEC); Serial.println(" disconnected");
                  } 
                  else if (bSingle[j].clr == 0x07FF) {
                    bSingle[j].clr = 0x001F;
                    tft.fillRoundRect(bSingle[j].x, bSingle[j].y, bSingle[j].width, bSingle[j].height, bSingle[j].r, bSingle[j].clr);
                    Serial.print("Channel "); Serial.print(j, DEC); Serial.println(" disconnected");
                  }
                }
              }
            }
            else if (bSingle[i].clr == 0x001F) {
              bSingle[i].clr = 0x07FF;
              Serial.print("Channel "); Serial.print(i, DEC); Serial.println(" disconnected");
            }
            else if (bSingle[i].clr == 0x07FF) {
              bSingle[i].clr = 0x001F;
              Serial.print("Channel "); Serial.print(i, DEC); Serial.println(" disconnected");
            }

            // redraw pressed button
            tft.fillRoundRect(bSingle[i].x, bSingle[i].y, bSingle[i].width, bSingle[i].height, bSingle[i].r, bSingle[i].clr);
          }
        }
      }
    } 
    fCnt = 0;
  } else {
    fCnt++;
  }
}
 
On a recent project, I had the same problem of getting the location touched to correspond to the button on the screen. These are the general steps I used to get the touch mapped to the button.

1. Ensure you can print text to the display screen in the orientation you intend to use it (portrait or landscape).
2. Get a simple sketch working to show the coordinates of where you are touching the screen and print the results to the screen.
3. Touch each corner to get the xmin, xmax, ymin, ymax coordinates of the touch screen. Use those values in the map() function to map the touch screen to the display screen.
4. Load up your application to display the buttons in the locations you want for the user. Take a piece of plastic wrap and place it over the display. Using a marker, draw on the plastic wrap the location of each button.
5. Go back to your simple touch screen sketch. record the coordinates of the touch as you touch the location of each button on the plastic wrap.
6.Go back to the application sketch, modify the parameters of the map function to those found in step 3. Modify the location you are checking for each button to the location found in step 5. Allow for a +/- variation in both the x and y coordinates for each button check.

If things have went well, the application should now respond to each button touch with the action you programmed it to do.

Good luck.
 
Status
Not open for further replies.
Back
Top