I could use an outside point of veiw

#include <MCUFRIEND_kbv.h>

MCUFRIEND_kbv tft;
#define BLACK 0x0000
#define RED 0xF800
#define GREEN 0x07E0
#include <stdint.h>
#include "TouchScreen.h"
#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
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

// Button positions and dimensions
const int buttonWidth = 80;
const int buttonHeight = 40;
const int buttonSpacing = 10;

// Button state
bool buttonState[8] = {false, false, false, false, false, false, false, false};

// Digital pin numbers corresponding to the buttons
const int buttonPins[] = {2, 3, 4, 5, 6, 7, 8, 9};

void setup() {
tft.begin(0x9486);
tft.setRotation(1); // Adjust rotation if needed

tft.fillScreen(BLACK);
tft.setTextSize(2);

// Draw on/off buttons
for (int i = 0; i < 8; i++) {
int x = 10 + (i % 4) * (buttonWidth + buttonSpacing);
int y = 10 + (i / 4) * (buttonHeight + buttonSpacing);

tft.fillRect(x, y, buttonWidth, buttonHeight, GREEN);
tft.setTextColor(BLACK);
tft.setCursor(x + 10, y + buttonHeight / 2 - 10);
tft.print("Pin ");
tft.print(buttonPins[i]);
tft.setCursor(x + 10, y + buttonHeight / 2 + 10);
tft.print(buttonState[i] ? "ON" : "OFF");

}
}

void loop() {
// Check if any of the buttons are pressed
for (int i = 0; i < 8; i++) {
int x = 10 + (i % 4) * (buttonWidth + buttonSpacing);
int y = 10 + (i / 4) * (buttonHeight + buttonSpacing);
TSPoint p = ts.getPoint();
if (p.z > ts.pressureThreshhold) {
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\tPressure = "); Serial.println(p.z);{
buttonState[i] = !buttonState[i]; // Toggle the button state
updateButton(i);
}
}
}

void updateButton(int buttonIndex) {
int x = 10 + (buttonIndex % 4) * (buttonWidth + buttonSpacing);
int y = 10 + (buttonIndex / 4) * (buttonHeight + buttonSpacing);

tft.fillRect(x, y, buttonWidth, buttonHeight, buttonState[buttonIndex] ? GREEN : RED);
tft.setTextColor(BLACK);
tft.setCursor(x + 10, y + buttonHeight / 2 - 10);
tft.print("Pin ");
tft.print(buttonPins[buttonIndex]);
tft.setCursor(x + 10, y + buttonHeight / 2 + 10);
tft.print(buttonState[buttonIndex] ? "ON" : "OFF");

// Control the digital pin
digitalWrite(buttonPins[buttonIndex], buttonState[buttonIndex] ? HIGH : LOW);
}

I would expect you to get more and better answers if you follow the forum guidelines and format your code then upload the full code using code tags.

#include <MCUFRIEND_kbv.h>

MCUFRIEND_kbv tft;
#define BLACK 0x0000
#define RED 0xF800
#define GREEN 0x07E0
#include <stdint.h>
#include "TouchScreen.h"
#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
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

// Button positions and dimensions
const int buttonWidth = 80;
const int buttonHeight = 40;
const int buttonSpacing = 10;

// Button state
bool buttonState[8] = {false, false, false, false, false, false, false, false};

// Digital pin numbers corresponding to the buttons
const int buttonPins[] = {2, 3, 4, 5, 6, 7, 8, 9};

void setup() {
  tft.begin(0x9486);
  tft.setRotation(1); // Adjust rotation if needed

  tft.fillScreen(BLACK);
  tft.setTextSize(2);

  // Draw on/off buttons
  for (int i = 0; i < 8; i++) {
    int x = 10 + (i % 4) * (buttonWidth + buttonSpacing);
    int y = 10 + (i / 4) * (buttonHeight + buttonSpacing);
    
    tft.fillRect(x, y, buttonWidth, buttonHeight, GREEN);
    tft.setTextColor(BLACK);
    tft.setCursor(x + 10, y + buttonHeight / 2 - 10);
    tft.print("Pin ");
    tft.print(buttonPins[i]);
    tft.setCursor(x + 10, y + buttonHeight / 2 + 10);
    tft.print(buttonState[i] ? "ON" : "OFF");
  }
}

void loop() {
  // Check if any of the buttons are pressed
  for (int i = 0; i < 8; i++) {
    int x = 10 + (i % 4) * (buttonWidth + buttonSpacing);
    int y = 10 + (i / 4) * (buttonHeight + buttonSpacing);
     TSPoint p = ts.getPoint();
    if (p.z > ts.pressureThreshhold) {
     Serial.print("X = "); Serial.print(p.x);
     Serial.print("\tY = "); Serial.print(p.y);
     Serial.print("\tPressure = "); Serial.println(p.z);{
      buttonState[i] = !buttonState[i]; // Toggle the button state
      updateButton(i);
    }
  }
}

void updateButton(int buttonIndex) {
  int x = 10 + (buttonIndex % 4) * (buttonWidth + buttonSpacing);
  int y = 10 + (buttonIndex / 4) * (buttonHeight + buttonSpacing);
  
  tft.fillRect(x, y, buttonWidth, buttonHeight, buttonState[buttonIndex] ? GREEN : RED);
  tft.setTextColor(BLACK);
  tft.setCursor(x + 10, y + buttonHeight / 2 - 10);
  tft.print("Pin ");
  tft.print(buttonPins[buttonIndex]);
  tft.setCursor(x + 10, y + buttonHeight / 2 + 10);
  tft.print(buttonState[buttonIndex] ? "ON" : "OFF");

  // Control the digital pin
  digitalWrite(buttonPins[buttonIndex], buttonState[buttonIndex] ? HIGH : LOW);
}

What is the question?

what am i missing to make this work properly

From How to get the best out of this forum:

It is very important to be clear about what is expected from code and what happens instead. Code ALWAYS works; that is the nature of code. Whether it does what you expect is a different thing altogether. We need to know what you expected the code to do and what happened instead.
More about posting code and common code problems.

You haven't told us what you expect the code to do or what it's doing instead. And none of us are mind readers.

sorry for that, what im looking to do is display 8 buttons on the tft screen that can be toggled on/off by touch, in the end those 8 buttons would control 8 relays

This is like pulling teeth: okay, and what is the problem?

the 'updateButton' was not declared in scope error is coming up, but it is declared.
im not sure what to do here. sorry its been a long couple days.

need one more } to end the loop()..

here's a demo touch sim i did a while back..
maybe it gives you some ideas, look at how i checked button touches..
touch screen demo..

good luck.. ~q

You have a stray { in your loop() function.

Need to have available:

#include <Adafruit_GFX.h>
#include <Adafruit_TouchScreen.h>

Do not need:

// #include <stdint.h>

Need to add, in setup()

Serial.begin(9600);

... because you have data printing to the Serial Monitor.

X = 492	Y = 570	Pressure = 26
X = 581	Y = 629	Pressure = 27
X = 532	Y = 624	Pressure = 27
1 Like

Thank you for the help, i will try it in the morning. Thank you for not being sarcastic

I am having trouble with adafruit mcufriend seems to be working better for me

This forum? Sarcastic? : ) (sometimes you must wait for the happy ending)

Any idea why adafruit wont work with this display. I tried a bunch of examples from ide and the only ones that work are mcufriend. The display says to use urtouch but that wont work either. Am i mising something

Your TFT might have a controller Adafruit have not written a library. Can you describe what you do and what you see, including code (which will show the Adafruit library you use) and errors?

This could be "updateButton" is not in the "scope" where the error is declared. The error report will show the line number where the error occurred.

// Setup for Uno and TouchScreen Sheild ELEGOO UNO R3 2.8" 
//TFT with small white CD bought on Amazon in Nov 6,2021
//Not sure who I got this from, so I assume it's copyrighted
//Use only for personal use until I can find who to give proper credit to.
//I'm using an UNO, an ELEGOO UNO R3 2.8 Inches TFT Touch Screen
//This will display a few on off buttons which can be enabled to turn on relays or led's or....?

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000

unsigned long previousMillis = 0;
const long interval = 1000;   

const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x9341
const int TS_LEFT=190,TS_RT=950,TS_TOP=968,TS_BOT=189;

  int pixel_x, pixel_y;     //Touch_getXY() updates global vars
  int texty[] = {15, 65, 115, 165, 215};// words down
  int buttony[] = {20, 70, 120, 170, 220};// buttons down
  int rect[] = {10,60,110,160,210};// controll square down
  char* labels[]={"START","STOP","THRTL","CLUTCH","LIGHT"};
      bool firstrun = 1;
    
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

Adafruit_GFX_Button on_btn, off_btn, up_btn;
//Adafruit_INA219 sensor219; // Declare and instance of INA219

bool Touch_getXY(void)
{
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());   // Determine where touch is X axis
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());  // Determine where touch is Y axis
    }
    else {pixel_x=0; pixel_y=0;
}
    return pressed;
}

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
 
///////////////////////////  CHECK FOR TOUCH and ACT
  int TouchCheck(bool pressed, int line)
{
    bool down = Touch_getXY();

    tft.setCursor(45, texty[line]);
    tft.setTextColor(YELLOW); tft.setTextSize(2);
    tft.println(labels[line]);

  //   X, Y, W, H, OUTLINE, FILL, TEXTCOLOR, LABEL_TXT, TEXT_SIZE    
     on_btn.initButton(&tft, 140, buttony[line], 50, 40, WHITE, CYAN, BLACK, "ON", 2);   //Position of buttons
    off_btn.initButton(&tft, 200, buttony[line], 50, 40, WHITE, CYAN, BLACK, "OFF", 2);   //Position of buttons

    if(firstrun)  { tft.fillRect(10, rect[line], 30, 30, RED);
    on_btn.drawButton(false);
   off_btn.drawButton(false); } 

    on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
   off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
    
    Serial.print(pixel_x);
        Serial.print(",");
    Serial.println(pixel_y);  
      
    if (on_btn.justReleased())
        on_btn.drawButton();
        
    if (off_btn.justReleased())
        off_btn.drawButton();
        
    if (on_btn.justPressed()) {
        on_btn.drawButton(false);
        tft.fillRect(10, rect[line], 30, 30, GREEN);
        digitalWrite(31+(line*2),LOW);
    }
    if (off_btn.justPressed()) {
        off_btn.drawButton(false);
        tft.fillRect(10, rect[line], 30, 30, RED);
        digitalWrite(31+(line*2),HIGH);
    }    
    }


void setup(void)
{
    Serial.begin(9600);

//    sensor219.begin();  // INA219
    
      pinMode(31, OUTPUT); //  Head Light
      pinMode(33, OUTPUT); //  Tail Light
      pinMode(35, OUTPUT); //  Cops Light
      pinMode(37, OUTPUT); //  12V Bus
      pinMode(39, OUTPUT); //  5V USB Power  all but Arduino
      digitalWrite(31,HIGH);
      digitalWrite(33,HIGH);
      digitalWrite(35,HIGH);
      digitalWrite(37,HIGH);
      digitalWrite(39,HIGH);   
                     
    uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    Serial.println("Calibrate for your Touch Panel");
    
    if (ID == 0xD3D3) ID = 0x9488; // write-only shield
    tft.begin(ID);
    tft.setRotation(0);            //PORTRAIT
    tft.fillScreen(BLACK);
} // =========== SETUP ==============



void loop(void)
{
  unsigned long currentMillis = millis();
  int line;  bool pressed;
  for (line = 0; line < 5; line++)  TouchCheck(pressed,line);
  firstrun = 0;
     
  float busVoltage = 0;
  float current = 0; // Measure in milli amps
  float power = 0;

//  busVoltage = sensor219.getBusVoltage_V();
//  current = sensor219.getCurrent_mA();
//  power = busVoltage * (current/1000); // Calculate the Power
  
  tft.setCursor(50, 262);
  tft.setTextColor(GREEN); tft.setTextSize(3);
  tft.print("BLOWER");
  tft.setCursor(50, 300);
  tft.setTextColor(RED); tft.setTextSize(4);
  tft.print("CONTROLER");
//  tft.print("V ");
//  tft.print(current,1);
//  tft.println("A");
//  tft.setCursor(60, 365);
//  tft.setTextColor(WHITE); tft.setTextSize(3);
//  power = busVoltage * current;
//  tft.print(power,2);
//  tft.println("W");
  
  /*
  Serial.print("Bus Voltage:   "); 
  Serial.print(busVoltage); 
  Serial.println(" V");  
  
  Serial.print("Current:       "); 
  Serial.print(current); 
  Serial.println(" mA");
  
  Serial.print("Power:         "); 
  Serial.print(power); 
  Serial.println(" W");  
  
  Serial.println("");  
*/ 

  if (currentMillis - previousMillis >= 2000) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
 tft.fillRect(0, 290, 400, 30, BLACK);
  tft.fillRect(0, 290, busVoltage * 6, 30, BLUE);
   tft.fillRect(0, 325, 400, 30, BLACK);
  tft.fillRect(0, 325, power * -6, 30, WHITE);
  }
  } // =========== LOOP ==============
  // TFT Touch Screen (Amazon)
  //  https://www.amazon.com/gp/product/B07D2FR5MJ/ref=ox_sc_act_title_1?smid=A1EJT7E0QSYEZM&psc=1
  //
  //  DB-25      Panel to Guts Box:
  //  Pins
  //
  //1, 2, 3       GND
  //4, 5, 6       Vcc (+5V from USB adapter)
  //7  HEAD       RELAY 1     BRN  I/O 31
  //8  TAIL       RELAY 2     ORG  I/O 33
  //9  COPS       RELAY 3     YEL  I/O 35
  //10 +12V       RELAY 4     GRN  I/O 37
  //11 +5V        RELAY 5     BLU  I/O 39

ok this works but i just need to the button pressed to toggle on while pressed and the toggle to off when released, can you help with that? i have been reading and studying as im going along but this has me off a bit

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.