본문 바로가기
아두이노

터치스크린 소스

by Channel_B2 2020. 2. 24.

 아두이노 터치스크린 터치 그리고 Display

<소스파일> Final_Touchscreen.ino

<결과물> https://youtu.be/bY6el40VmZI



기타 

<한글 출력 방법> https://blog.naver.com/kwy1052aa/221763898675

<사용방법> https://orasman.tistory.com/276

TFT라이브러리.zip




  1. #include <Adafruit_GFX.h>    // Core graphics library
  2. #include <Adafruit_TFTLCD.h> // Hardware-specific library
  3. /////////////////////////////////////////////////////////////
  4. #include <TouchScreen.h>


  5. #define YP A2  // must be an analog pin, use "An" notation!
  6. #define XM A3  // must be an analog pin, use "An" notation!
  7. #define YM 8   // can be a digital pin
  8. #define XP 9  // can be a digital pin


  9. #define TS_MINX 350
  10. #define TS_MINY -230
  11. #define TS_MAXX 1000
  12. #define TS_MAXY 960

  13. #define MINPRESSURE 10
  14. #define MAXPRESSURE 1000

  15. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  16. ////////////////////////////////////////////////////////////////////


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

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

  22. #define  BLACK   0x0000
  23. #define BLUE    0x001F
  24. #define RED     0xF800
  25. #define GREEN   0x07E0
  26. #define CYAN    0x07FF
  27. #define MAGENTA 0xF81F
  28. #define YELLOW  0xFFE0
  29. #define WHITE   0xFFFF

  30. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  31. // If using the shield, all control and data lines are fixed, and
  32. // a simpler declaration can optionally be used:
  33. // Adafruit_TFTLCD tft;

  34. int a;

  35. void setup(void) {
  36.  
  37.   Serial.begin(9600);
  38.   Serial.println(F("TFT LCD test"));

  39. #ifdef USE_ADAFRUIT_SHIELD_PINOUT
  40.   Serial.println(F("Using Adafruit 2.8\" TFT Arduino Shield Pinout"));
  41. #else
  42.   Serial.println(F("Using Adafruit 2.8\" TFT Breakout Board Pinout"));
  43. #endif

  44.   Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());

  45.   tft.reset();

  46.   uint16_t identifier = 0x9327;//tft.readID();

  47.   if(identifier == 0x9325) {
  48.     Serial.println(F("Found ILI9325 LCD driver"));
  49.   } else if(identifier == 0x9327) {
  50.     Serial.println(F("Found ILI9327 LCD driver"));
  51.   } else if(identifier == 0x9328) {
  52.     Serial.println(F("Found ILI9328 LCD driver"));
  53.   } else if(identifier == 0x7575) {
  54.     Serial.println(F("Found HX8347G LCD driver"));
  55.   } else if(identifier == 0x9341) {
  56.     Serial.println(F("Found ILI9341 LCD driver"));
  57.   } else if(identifier == 0x8357) {
  58.     Serial.println(F("Found HX8357D LCD driver"));
  59.   } else if(identifier == 0x0154) {
  60.     Serial.println(F("Found S6D0154 LCD driver"));
  61.   } else {
  62.     Serial.print(F("Unknown LCD driver chip: "));
  63.     Serial.println(identifier, HEX);
  64.     Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
  65.     Serial.println(F("  #define USE_ADAFRUIT_SHIELD_PINOUT"));
  66.     Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
  67.     Serial.println(F("If using the breakout board, it should NOT be #defined!"));
  68.     Serial.println(F("Also if using the breakout, double-check that all wiring"));
  69.     Serial.println(F("matches the tutorial."));
  70.     return;
  71.   }
  72.   
  73.   #define LCDROTATION 3

  74. tft.begin(identifier);

  75.   Serial.print(F("Text                     "));
  76.   delay(1000);
  77.  tft.fillScreen(BLACK); //배경 까망색
  78.   tft.drawRect(0,0,130,120,WHITE); // 네모 테두리 있는 상자 넣기
  79. }



  80. void loop(void) {

  81.   
  82.  

  83.   TSPoint p = ts.getPoint();
  84.   pinMode(XM, OUTPUT);
  85.   pinMode(YP, OUTPUT);

  86.   if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {

  87.     p.x=map(p.x,TS_MINX, TS_MAXX, tft.width(),0);
  88.     p.y=map(p.y,TS_MINY, TS_MAXY, tft.height(),0);

  89.     
  90.    if( p.x>0 && p.x<160){  //터치스크린 X좌표 0~160
  91.     if(p.y>0 && p.y<160){  //터치스크린 Y좌표 0~160 두조건 만족하면 아래 조건 성립
  92.  
  93.    testText();
  94.    delay(500);}}
  95.   
  96.     
  97.    
  98.          
  99.          }
  100.       }
  101.     

  102. unsigned long testText() {
  103.       tft.setRotation(3);

  104.   unsigned long start = micros();
  105.   tft.fillScreen(BLACK);  
  106.   tft.setCursor(0, 0);
  107.   tft.setTextColor(WHITE);  tft.setTextSize(2);
  108.   tft.println("Hello BAK CHAN YEOP");

  109.   tft.fillRect(180,30,130,120,RED);
  110.   tft.setTextColor(WHITE);  tft.setTextSize(3);
  111.   tft.setCursor(20, 40);
  112.   tft.println("<TEMP>");
  113.   tft.setCursor(30, 70);
  114.   tft.setTextColor(YELLOW); tft.setTextSize(2);
  115.   tft.println(a);

  116.   tft.setTextColor(WHITE);  tft.setTextSize(3);
  117.   tft.setCursor(180, 40);
  118.   tft.println("<Count>");
  119.   tft.setCursor(190, 70);
  120.   tft.setTextColor(YELLOW); tft.setTextSize(2);
  121.   tft.println(a);


  122.  return micros() - start;
  123. }






아래 링크로 쓸수 있는 디자인들

https://www.youtube.com/watch?v=_h4LrQCqj1Y