cecell

Control_Bandsaw

Jul 15th, 2024
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino_GigaDisplay_GFX.h>
  2. #include <Arduino_GigaDisplayTouch.h>
  3.  
  4. // Define pins for the touch screen
  5. #define YP A3  // must be an analog pin, use "An" notation!
  6. #define XM A2  // must be an analog pin, use "An" notation!
  7. #define YM 9   // can be a digital pin
  8. #define XP 8   // can be a digital pin
  9.  
  10. #define TS_MINX 100
  11. #define TS_MINY 120
  12. #define TS_MAXX 940
  13. #define TS_MAXY 900
  14.  
  15. GigaDisplay_GFX Display; // Initialize the display
  16. Arduino_GigaDisplayTouch ts; // Initialize the touch screen
  17.  
  18. // Relay pins
  19. const int relay1 = 2;
  20. const int relay2 = 3;
  21. const int relay3 = 4;
  22.  
  23. int touch_x;   // x location where screen was touched
  24. int touch_y;   // y location where screen was touched
  25.  
  26. // Button coordinates
  27. const int button1_x = 20;
  28. const int button1_y = 50;
  29. const int button2_x = 140;
  30. const int button2_y = 50;
  31. const int button3_x = 260;
  32. const int button3_y = 50;
  33. const int button_w = 100;
  34. const int button_h = 50;
  35.  
  36. // Button modes
  37. const bool momentary = true;
  38. const bool toggle = false;
  39. const bool buttonModes[] = {momentary, momentary, toggle};
  40.  
  41. bool relayStates[] = {false, false, false};
  42.  
  43. // Relay pin array for easier handling
  44. const int relayPins[] = {relay1, relay2, relay3};
  45.  
  46. // Define custom colors
  47. #define GD_BLACK 0x0000
  48. #define GD_WHITE 0xFFFF
  49. #define GD_RED   0xF800
  50. #define GD_GREEN 0x07E0
  51. #define GD_BLUE  0x001F
  52. #define GD_ORANGE 0xFD20
  53. #define GD_YELLOW 0xFFE0
  54. #define GD_CYAN   0x07FF
  55.  
  56. void setup() {
  57.   Serial.begin(9600);
  58.   delay(1000); // Wait for the Serial Monitor to initialize
  59.   Serial.println("Initializing display...");
  60.  
  61.   Display.begin();
  62.   ts.begin();
  63.   Display.setRotation(1);
  64.   Display.fillScreen(GD_BLACK);
  65.   Serial.println("Display initialized.");
  66.  
  67.   pinMode(relay1, OUTPUT);
  68.   pinMode(relay2, OUTPUT);
  69.   pinMode(relay3, OUTPUT);
  70.  
  71.   drawButtons();
  72. }
  73.  
  74. void loop() {
  75.   uint8_t contact;
  76.   GDTpoint_t myPoint[1];
  77.   contact = ts.getTouchPoints(myPoint);
  78.  
  79.   if (contact > 0) {
  80.     touch_x = myPoint[0].y;
  81.     touch_y = 480 - myPoint[0].x;
  82.     Serial.println("touch_x = " + String(touch_x));
  83.     Serial.println("touch_y = " + String(touch_y));
  84.     Serial.println("myPoint[0].y = " + String(myPoint[0].y));
  85.     Serial.println("myPoint[0].x = " + String(myPoint[0].x));
  86.    
  87.     if (myPoint[0].y > button1_y && myPoint[0].y < button1_y + button_h) {
  88.       if (touch_x > button3_x && touch_x < button3_x + button_w) {
  89.         handleButtonPress(3);
  90.         Serial.println("Button 3 pressed!");
  91.       } else if (touch_x > button2_x && touch_x < button2_x + button_w) {
  92.         handleButtonPress(2);
  93.         Serial.println("Button 2 pressed!");
  94.       } else if (touch_x > button1_x && touch_x < button1_x + button_w) {
  95.         handleButtonPress(1);
  96.         Serial.println("Button 1 pressed!");
  97.       }
  98.     }
  99.   }
  100. }
  101.  
  102. void drawButtons() {
  103.   Display.fillRect(button1_x, button1_y, button_w, button_h, GD_RED);
  104.   Display.fillRect(button2_x, button2_y, button_w, button_h, GD_GREEN);
  105.   Display.fillRect(button3_x, button3_y, button_w, button_h, GD_BLUE);
  106.  
  107.   Display.setCursor(button1_x + 25, button1_y + 20);
  108.   Display.setTextColor(GD_WHITE);
  109.   Display.setTextSize(2);
  110.   Display.print("Relay 1");
  111.  
  112.   Display.setCursor(button2_x + 25, button2_y + 20);
  113.   Display.setTextColor(GD_WHITE);
  114.   Display.setTextSize(2);
  115.   Display.print("Relay 2");
  116.  
  117.   Display.setCursor(button3_x + 25, button3_y + 20);
  118.   Display.setTextColor(GD_WHITE);
  119.   Display.setTextSize(2);
  120.   Display.print("Relay 3");
  121. }
  122.  
  123. void handleButtonPress(int button) {
  124.   pressButton(button);
  125.   delay(200);
  126.  
  127.   if (buttonModes[button - 1] == momentary) {
  128.     digitalWrite(relayPins[button - 1], HIGH);
  129.     releaseButton(button);
  130.     delay(200);
  131.     digitalWrite(relayPins[button - 1], LOW);
  132.   } else if (buttonModes[button - 1] == toggle) {
  133.     relayStates[button - 1] = !relayStates[button - 1];
  134.     digitalWrite(relayPins[button - 1], relayStates[button - 1]);
  135.     releaseButton(button);
  136.   }
  137. }
  138.  
  139. void pressButton(int button) {
  140.   switch (button) {
  141.     case 1:
  142.       Display.fillRect(button1_x, button1_y, button_w, button_h, GD_ORANGE);
  143.       Display.setCursor(button1_x + 25, button1_y + 20);
  144.       Display.setTextColor(GD_WHITE);
  145.       Display.setTextSize(2);
  146.       Display.print("Relay 1");
  147.       break;
  148.     case 2:
  149.       Display.fillRect(button2_x, button2_y, button_w, button_h, GD_YELLOW);
  150.       Display.setCursor(button2_x + 25, button2_y + 20);
  151.       Display.setTextColor(GD_WHITE);
  152.       Display.setTextSize(2);
  153.       Display.print("Relay 2");
  154.       break;
  155.     case 3:
  156.       Display.fillRect(button3_x, button3_y, button_w, button_h, GD_CYAN);
  157.       Display.setCursor(button3_x + 25, button3_y + 20);
  158.       Display.setTextColor(GD_WHITE);
  159.       Display.setTextSize(2);
  160.       Display.print("Relay 3");
  161.       break;
  162.   }
  163. }
  164.  
  165. void releaseButton(int button) {
  166.   switch (button) {
  167.     case 1:
  168.       Display.fillRect(button1_x, button1_y, button_w, button_h, GD_RED);
  169.       Display.setCursor(button1_x + 25, button1_y + 20);
  170.       Display.setTextColor(GD_WHITE);
  171.       Display.setTextSize(2);
  172.       Display.print("Relay 1");
  173.       break;
  174.     case 2:
  175.       Display.fillRect(button2_x, button2_y, button_w, button_h, GD_GREEN);
  176.       Display.setCursor(button2_x + 25, button2_y + 20);
  177.       Display.setTextColor(GD_WHITE);
  178.       Display.setTextSize(2);
  179.       Display.print("Relay 2");
  180.       break;
  181.     case 3:
  182.       Display.fillRect(button3_x, button3_y, button_w, button_h, GD_BLUE);
  183.       Display.setCursor(button3_x + 25, button3_y + 20);
  184.       Display.setTextColor(GD_WHITE);
  185.       Display.setTextSize(2);
  186.       Display.print("Relay 3");
  187.       break;
  188.   }
  189. }
  190.  
Tags: Arduino
Advertisement