microcontroller - Making a button do something on Arduino TFT touchscreen -
so have arduino mega2560 , tft shield touchscreen. used 1 of examples create 2 buttons display on screen, using drawrect()
. how create these 2 boxes when press them? know coordinates these 2 boxes, how create them "sense" touch , transist screen of display? maybe illustration of code great help! thanks.
my current code below: add together necessary parts it.
#include <adafruit_gfx.h> // core graphics library #include <adafruit_tftlcd.h> // hardware-specific library #define lcd_cs a3 // chip select goes analog 3 #define lcd_cd a2 // command/data goes analog 2 #define lcd_wr a1 // lcd write goes analog 1 #define lcd_rd a0 // lcd read goes analog 0 #define lcd_reset a4 // can alternately connect arduino's reset pin #define black 0x0000 #define bluish 0x001f #define reddish 0xf800 #define greenish 0x07e0 #define cyan 0x07ff #define magenta 0xf81f #define yellowish 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 adafruit 2.8\" tft arduino shield, line:")); progmemprintln(pstr(" #define use_adafruit_shield_pinout")); progmemprintln(pstr("should appear in library header (adafruit_tft.h).")); progmemprintln(pstr("if using breakout board, should not #defined!")); progmemprintln(pstr("also if using breakout, double-check wiring")); progmemprintln(pstr("matches 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"); homecoming micros() - start; } // re-create string flash serial port // source string must within pstr() declaration! void progmemprint(const char *str) { char c; while(c = pgm_read_byte(str++)) serial.print(c); } // same above, trailing newline void progmemprintln(const char *str) { progmemprint(str); serial.println(); }
you need touch screen lib
includeinside loop tspoint p = ts.getpoint(); // retrieve 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);
hope helps
arduino microcontroller
No comments:
Post a Comment