2.8" TFT Touch Shield for Arduino with Resistive Touch Screen v2

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Post Reply
User avatar
evanlegere
 
Posts: 2
Joined: Fri Feb 09, 2024 4:14 pm

2.8" TFT Touch Shield for Arduino with Resistive Touch Screen v2

Post by evanlegere »

Hey im using the 2.8" TFT Touch Shield for Arduino with Resistive Touch Screen v2 - STEMMA QT / Qwiic and im having difficulties getting my code to work for the screen I found code that I altered online to work for the resistive touch screen and it almost works but my buttons are not changing state they are just staying red went from capacitive code to resistive must be missing some things I can add both original code and altered code if that helps!

User avatar
evanlegere
 
Posts: 2
Joined: Fri Feb 09, 2024 4:14 pm

Re: 2.8" TFT Touch Shield for Arduino with Resistive Touch Screen v2

Post by evanlegere »

/* THIS IS CODE TO GET OUTPUTS OUT OF THE RESISTIVE
* TOUCHSCREEN took me a while but chatGPT helped a lot!
*
* ON/OFF TFT Push Button Grid
* Each button is assigned to a digital output, which
* may be used to illuminate an LED (or other purpose).
* Two additional btns are used to turn all LED on or off.
* Boolean state of each button coincides with HIGH or LOW.
* For use with Adafruit 2.8" TFT LCD - Resistive code edited.
* Any of the four screen rotations may be used.
* LED resistors are 220 ohm.
*/

#include <SPI.h>
#include <Wire.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Adafruit_STMPE610.h"

// Default values for Adafruit shield
#define STMPE_CS 8
#define TFT_DC 9
#define TFT_CS 10

// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF


typedef struct {
int left;
int top;
int width;
int height;
}btnRec;

// LEDs are assigned to digital outputs
const byte LED1 = 2;
const byte LED2 = 3;
const byte LED3 = 5;
const byte LED4 = 6;
const byte LED5 = 7;
const byte LED6 = 8;

const byte row = 4;
const byte col = 2;
#define _numBtns row*col
#define _numLEDs row*col-2

#define _frameColor WHITE
#define _scrnColor BLACK

// Rotations 0,2 = portrait : 0->USB=right,upper : 2->USB=left,lower
// Rotations 1,3 = landscape : 1->USB=left,upper : 3->USB=right,lower
#define _rotation 0 //(0->3)

// Define the touchscreen calibration factors
#define TS_MINX 150
#define TS_MAXX 3800
#define TS_MINY 130
#define TS_MAXY 4000

// Process touch input
void processTouchInput(int rawX, int rawY) {
// Apply touchscreen calibration and scaling factors
int adjustedX = map(rawX, TS_MINX, TS_MAXX, 0, tft.width()); // Map X from touchscreen to TFT width
int adjustedY = map(rawY, TS_MINY, TS_MAXY, 0, tft.height()); // Map Y from touchscreen to TFT height

// Invert Y-axis if needed
// adjustedY = tft.height() - adjustedY;

// Use adjustedX and adjustedY for further processing
}

static btnRec btn[8];
char *titleStr[_numBtns] = {"LED1","LED2","LED3","LED4","LED5","LED6","All ON","All OFF"};
bool state[_numLEDs] = {false, false, false, false, false, false};

uint16_t x, y;
TS_Point p;

void btnGrid() {
int left, top;
int l = 10;
int t = 40;
int w = 100;
int h = 50;
byte hgap = 20;
byte vgap = 20;
byte id = 0;
for(byte j = 0; j < row; j++){
for (byte i = 0; i < col; i++){
btn[id].left = l + i*(w + vgap);
btn[id].top = t + j*(h + hgap);
btn[id].width = w;
btn[id].height = h;

tft.drawRoundRect( btn[id].left, btn[id].top, btn[id].width, btn[id].height, btn[id].height/4, _frameColor);
tft.fillRoundRect( btn[id].left + 2, btn[id].top + 2, btn[id].width-4, btn[id].height-4, btn[id].height/4, RED);
tft.setTextColor(WHITE);
if (id == 6) {
tft.fillRoundRect( btn[id].left + 2, btn[id].top + 2, btn[id].width-4, btn[id].height-4, btn[id].height/4, GREEN);
tft.setTextColor(BLACK);
}
tft.setCursor(btn[id].left + 10, btn[id].top + btn[id].height/2-8);
tft.println(titleStr[id]);
id++;
}
}
}

void LEDHandler(byte btnHit, byte value) {
switch (btnHit) {
case 0: digitalWrite(LED1, value);
break;
case 1: digitalWrite(LED2, value);
break;
case 2: digitalWrite(LED3, value);
break;
case 3: digitalWrite(LED4, value);
break;
case 4: digitalWrite(LED5, value);
break;
case 5: digitalWrite(LED6, value);
break;
}
}

void redBtnFill(byte num) {
tft.fillRoundRect( btn[num].left + 2, btn[num].top + 2, btn[num].width-4, btn[num].height-4, btn[num].height/4, RED);
tft.setCursor(btn[num].left + 10, btn[num].top + btn[num].height/2-8);
tft.setTextColor(WHITE);
tft.println(titleStr[num]);
state[num] = false;
LEDHandler(num,LOW);
}

void greenBtnFill(byte num) {
tft.fillRoundRect( btn[num].left + 2, btn[num].top + 2, btn[num].width - 4, btn[num].height - 4, btn[num].height/4, GREEN);
tft.setCursor(btn[num].left + 10, btn[num].top + btn[num].height/2-8);
tft.setTextColor(BLACK);
tft.println(titleStr[num]);
state[num] = true;
LEDHandler(num,HIGH);
}

void showIndividualState(byte btnNum ) {
// Erase old data
tft.fillRect( 10, 10, 220, 20, _scrnColor );
tft.setCursor(10, 10);
tft.setTextColor(WHITE);
tft.print(titleStr[btnNum]);
if(state[btnNum]) {tft.print(" HIGH");} else {tft.print(" LOW");}
}

void showCollectiveState(char *stateStr ) {
// Erase old data
tft.fillRect( 10, 10, 220, 20, _scrnColor );
tft.setCursor(10, 10);
tft.setTextColor(WHITE);
tft.print("LED1-LED6 "); tft.print(stateStr);
}

void LEDsOn(boolean illuminated) {
for (byte z = 0; z < _numLEDs; z++) {
if (illuminated) {
greenBtnFill(z);
state[z] = true;
} else {
redBtnFill(z);
state[z] = false;
}
}
}

void setPinModes(){
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
}

void setup() {
tft.begin();
if (! ts.begin()) { // may pass in 'sensitivity' coefficient
Serial.println("Couldn't start touchscreen controller");
while (1);
}
tft.setRotation(_rotation);
tft.fillScreen(BLACK);
tft.setTextSize(2);
btnGrid();
setPinModes();
}

void loop() {
if (ts.touched()) {
delay(50);
TS_Point p = ts.getPoint(); // Get touchscreen coordinates
if (p.z > ts.touched()) { // Check if touch pressure is above the threshold
// Apply touchscreen calibration and scaling factors
int adjustedX = map(p.x, TS_MINX, TS_MAXX, 0, tft.width()); // Map X from touchscreen to TFT width
int adjustedY = map(p.y, TS_MINY, TS_MAXY, 0, tft.height()); // Map Y from touchscreen to TFT height

// Invert Y-axis if needed
// adjustedY = tft.height() - adjustedY;

// Check if the touch falls within any button boundaries
for (byte z = 0; z < _numLEDs; z++) {
if ((adjustedX > btn[z].left) && (adjustedX < (btn[z].left + btn[z].width)) &&
(adjustedY > btn[z].top) && (adjustedY <= (btn[z].top + btn[z].height))) {
delay(100);
// Button z is pressed
if (state[z]) {
redBtnFill(z); // Turn LED off
showIndividualState(z);
delay(100);
} else {
greenBtnFill(z); // Turn LED on
showIndividualState(z);
delay(100);

}
}
}

// Check if the touch falls within the "All ON" button boundaries
if ((adjustedX > btn[6].left) && (adjustedX < (btn[6].left + btn[6].width)) &&
(adjustedY > btn[6].top) && (adjustedY <= (btn[6].top + btn[6].height))) {
LEDsOn(true); // Turn all LEDs on
showCollectiveState("HIGH");
}

// Check if the touch falls within the "All OFF" button boundaries
if ((adjustedX > btn[7].left) && (adjustedX < (btn[7].left + btn[7].width)) &&
(adjustedY > btn[7].top) && (adjustedY <= (btn[7].top + btn[7].height))) {
LEDsOn(false); // Turn all LEDs off
showCollectiveState("LOW");
}
}
}
}

Post Reply
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”