title: Making a button do something on Arduino TFT touchscreen
body: <p>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 <code>drawRect()</code>. 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.</p>
  
  <p>My current code is below: you could add the necessary parts to it.</p>
  
  <pre><code>#include &lt;Adafruit_GFX.h&gt;    // Core graphics library
  #include &lt;Adafruit_TFTLCD.h&gt; // 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();
  }
  </code></pre>