简体   繁体   中英

Making a button do something on Arduino TFT touchscreen

so I have an Arduino MEGA2560 and a TFT shield touchscreen. I used one of the examples to make 2 buttons to display on screen, by just using drawRect() . But how do I make these 2 boxes do something when I press them? I know the coordinates for these 2 boxes, so how do I make them "sense" the touch and transist into another screen of display? Maybe a example of code would be great help! thanks.

My current code is below: you could add the necessary parts to it.

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);


void setup(void) {
  Serial.begin(9600);
  progmemPrintln(PSTR("TFT LCD"));

#ifdef USE_ADAFRUIT_SHIELD_PINOUT
  progmemPrintln(PSTR("Using Adafruit 2.8\" TFT Arduino Shield Pinout"));
#else
  progmemPrintln(PSTR("Using Adafruit 2.8\" TFT Breakout Board Pinout"));
#endif

  tft.reset();

  uint16_t identifier = tft.readID();

  if(identifier == 0x9325) {
    progmemPrintln(PSTR("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    progmemPrintln(PSTR("Found ILI9328 LCD driver"));
  } else if(identifier == 0x7575) {
    progmemPrintln(PSTR("Found HX8347G LCD driver"));
  } else {
    progmemPrint(PSTR("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    progmemPrintln(PSTR("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
    progmemPrintln(PSTR("  #define USE_ADAFRUIT_SHIELD_PINOUT"));
    progmemPrintln(PSTR("should appear in the library header (Adafruit_TFT.h)."));
    progmemPrintln(PSTR("If using the breakout board, it should NOT be #defined!"));
    progmemPrintln(PSTR("Also if using the breakout, double-check that all wiring"));
    progmemPrintln(PSTR("matches the tutorial."));
    return;
  }

  tft.begin(identifier);



  progmemPrint(PSTR("Text                     "));
  Serial.println(startText());
  delay(0);



  progmemPrintln(PSTR("Done!"));
}

void loop(void) {

    startText();
    delay(9999999);

}



unsigned long startText() {
  tft.fillScreen(BLACK);
  unsigned long start = micros();
  tft.setCursor(0, 0);
  tft.println();
  tft.println();
  tft.setTextColor(GREEN);    tft.setTextSize(2.8);
  tft.println("Welcome ");
  tft.println();
  tft.setTextColor(WHITE);    tft.setTextSize(2.5);

  tft.println();

  tft.drawRect(5, 150, 110, 110, YELLOW);
  tft.drawRect(130, 150, 110, 110, RED);
  tft.setCursor(155, 170);
  tft.setTextColor(RED);
  tft.println("OFF");
  tft.fillRect(5, 150, 110, 110, YELLOW);
  tft.fillRect(13, 158, 94, 94, BLACK);
  tft.setTextColor(GREEN);
  tft.setCursor(20, 170);
  tft.println("ON");

  return micros() - start;

}

// Copy string from flash to serial port
// Source string MUST be inside a PSTR() declaration!
void progmemPrint(const char *str) {
  char c;
  while(c = pgm_read_byte(str++)) Serial.print(c);
}

// Same as above, with trailing newline
void progmemPrintln(const char *str) {
  progmemPrint(str);
  Serial.println();
}

You need the Touch screen lib

#include <TouchScreen.h>
//inside loop  
    TSPoint p = ts.getPoint();
    // Retrieve a point  
    p = ts.getPoint();
    Serial.print("X = "); Serial.print(p.x);
    Serial.print("\tY = "); Serial.print(p.y);
    Serial.print("\tPressure = "); Serial.println(p.z);

Here is an UART TFT LCD, it supports WYSIWYG editor to build your GUI in PC and download it via USB.

It can be controlled by Arduino via UART, so just use the Serial.print() you can make it display many images. And you can even build Font by yourself and download to the LCD.

See https://www.indiegogo.com/projects/nextion-a-cost-effective-high-performance-tft-hmi/x/4467372

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM