Advertisement
Mouamle

Arduino touchscreen test

Sep 7th, 2018
3,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPFD5408_Adafruit_GFX.h>    // Core graphics library
  2. #include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
  3. #include <SPFD5408_TouchScreen.h>
  4. #include  <Chrono.h>
  5.  
  6. // LCD Pin
  7.  
  8. #define LCD_CS A3 // Chip Select goes to Analog 3
  9. #define LCD_CD A2 // Command/Data goes to Analog 2
  10. #define LCD_WR A1 // LCD Write goes to Analog 1
  11. #define LCD_RD A0 // LCD Read goes to Analog 0
  12.  
  13. #define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
  14.  
  15. #define YP A1
  16. #define XM A2
  17. #define YM 7
  18. #define XP 6
  19.  
  20. // Assign human-readable names to some common 16-bit color values:
  21. #define WHITE   0x0000
  22. #define YELLOW    0x001F
  23. #define CYAN     0xF800
  24. #define MAGENTA   0x07E0
  25. #define RED    0x07FF
  26. #define GREEN 0xF81F
  27. #define BLUE  0xFFE0
  28. #define BLACK   0xFFFF
  29.  
  30. // Calibrates value
  31. #define SENSIBILITY 300
  32. #define MINPRESSURE 10
  33. #define MAXPRESSURE 1000
  34.  
  35. //These are the pins for the shield!
  36. #define YP A1
  37. #define XM A2
  38. #define YM 7
  39. #define XP 6
  40.  
  41. short TS_MINX = 132;
  42. short TS_MINY = 157;
  43. short TS_MAXX = 976;
  44. short TS_MAXY = 860;
  45.  
  46. int trigPin = 11;    //Trig - green Jumper
  47. int echoPin = 10;    //Echo - yellow Jumper
  48. long duration, cm = 0, inches;
  49.  
  50. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  51. TouchScreen ts = TouchScreen(XP, YP, XM, YM, SENSIBILITY);
  52.  
  53. Chrono screenUpdator;
  54.  
  55. void setup() {
  56.   tft.reset();
  57.   tft.begin(0x9341);
  58.   tft.setRotation(0);
  59.   tft.fillScreen(WHITE);
  60.  
  61.   pinMode(trigPin, OUTPUT);
  62.   pinMode(echoPin, INPUT);
  63.  
  64.   tft.setTextColor(BLUE);
  65.   tft.setTextSize(2);
  66.   Serial.begin(9600);
  67.   draw();
  68. }
  69.  
  70. int v = 0 , y = 0, p = 0;
  71. int color = RED;
  72. boolean state = false;
  73. void loop() {
  74.   p = map(v, 5, 80, 0, 100);
  75.   y = 310 - (v * 3);
  76.  
  77.   if (v <= 25)
  78.     color = RED;
  79.   else if (v <= 70)
  80.     color = BLUE;
  81.   else if (v > 70)
  82.     color = GREEN;
  83.  
  84.   // printDistance();
  85.   //  tft.setCursor(85, 50);
  86.   //  tft.print(100 - p);
  87.   //  tft.print(F("%"));
  88.  
  89.   if (screenUpdator.hasPassed(5000)) {
  90.     draw();
  91.   }
  92.  
  93.   TSPoint point = waitOneTouch();
  94.   if (point.x >= 750 && point.x <= 920) {
  95.     if (point.y >= 150 && point.y <= 650) {
  96.       state = !state;
  97.       draw();
  98.     }
  99.   }
  100.  
  101.   if (state) {
  102.     Serial.println(1);
  103.   } else {
  104.     Serial.println(0);
  105.   }
  106.  
  107.   if (state)
  108.     v += 1;
  109.   if (v >= 100)
  110.     v = 0;
  111.   delay(100);
  112. }
  113.  
  114. void draw() {
  115.   screenUpdator.restart();
  116.   tft.fillScreen(BLACK);
  117.   tft.drawRect(10, 10, 50, 300, WHITE);
  118.  
  119.   tft.setTextColor(color);
  120.   tft.fillRect(15, y, 40, 300 - y, color);
  121.  
  122.   // status text
  123.   tft.setTextSize(6);
  124.   tft.setCursor(80, 30);
  125.   tft.print(v);
  126.   tft.print(F("%"));
  127.  
  128.   // On button
  129.   tft.setTextSize(2);
  130.   tft.setCursor(130, 265);
  131.   if (state) {
  132.     tft.setTextColor(GREEN);
  133.     tft.print(F("ON"));
  134.     tft.drawRect(70, 235, 160, 75, GREEN);
  135.   } else {
  136.     tft.setTextColor(RED);
  137.     tft.print(F("OFF"));
  138.     tft.drawRect(70, 235, 160, 75, RED);
  139.   }
  140.  
  141. }
  142.  
  143. TSPoint waitOneTouch() {
  144.  
  145.   // wait 1 touch to exit function
  146.  
  147.   TSPoint p;
  148.  
  149.   //  do {
  150.   p = ts.getPoint();
  151.  
  152.   pinMode(XM, OUTPUT); //Pins configures again for TFT control
  153.   pinMode(YP, OUTPUT);
  154.  
  155.   //  } while ((p.z < MINPRESSURE ) || (p.z > MAXPRESSURE));
  156.  
  157.   return p;
  158. }
  159.  
  160. void printDistance() {
  161.   digitalWrite(trigPin, LOW);
  162.   delayMicroseconds(5);
  163.   digitalWrite(trigPin, HIGH);
  164.   delayMicroseconds(10);
  165.   digitalWrite(trigPin, LOW);
  166.  
  167.   // Read the signal from the sensor: a HIGH pulse whose
  168.   // duration is the time (in microseconds) from the sending
  169.   // of the ping to the reception of its echo off of an object.
  170.   pinMode(echoPin, INPUT);
  171.   duration = pulseIn(echoPin, HIGH);
  172.  
  173.   // convert the time into a distance
  174.   cm = (duration / 2) / 29.1;
  175.   inches = (duration / 2) / 74;
  176.   //tft.print(cm);
  177.   //tft.println(F("cm"));
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement