#include <Arduino_GigaDisplay_GFX.h>
#include <Arduino_GigaDisplayTouch.h>
// Define pins for the touch screen
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define TS_MINX 100
#define TS_MINY 120
#define TS_MAXX 940
#define TS_MAXY 900
GigaDisplay_GFX Display; // Initialize the display
Arduino_GigaDisplayTouch ts; // Initialize the touch screen
// Relay pins
const int relay1 = 2;
const int relay2 = 3;
const int relay3 = 4;
int touch_x; // x location where screen was touched
int touch_y; // y location where screen was touched
// Button coordinates
const int button1_x = 20;
const int button1_y = 50;
const int button2_x = 140;
const int button2_y = 50;
const int button3_x = 260;
const int button3_y = 50;
const int button_w = 100;
const int button_h = 50;
// Button modes
const bool momentary = true;
const bool toggle = false;
const bool buttonModes[] = {momentary, momentary, toggle};
bool relayStates[] = {false, false, false};
// Relay pin array for easier handling
const int relayPins[] = {relay1, relay2, relay3};
// Define custom colors
#define GD_BLACK 0x0000
#define GD_WHITE 0xFFFF
#define GD_RED 0xF800
#define GD_GREEN 0x07E0
#define GD_BLUE 0x001F
#define GD_ORANGE 0xFD20
#define GD_YELLOW 0xFFE0
#define GD_CYAN 0x07FF
void setup() {
Serial.begin(9600);
delay(1000); // Wait for the Serial Monitor to initialize
Serial.println("Initializing display...");
Display.begin();
ts.begin();
Display.setRotation(1);
Display.fillScreen(GD_BLACK);
Serial.println("Display initialized.");
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
drawButtons();
}
void loop() {
uint8_t contact;
GDTpoint_t myPoint[1];
contact = ts.getTouchPoints(myPoint);
if (contact > 0) {
touch_x = myPoint[0].y;
touch_y = 480 - myPoint[0].x;
Serial.println("touch_x = " + String(touch_x));
Serial.println("touch_y = " + String(touch_y));
Serial.println("myPoint[0].y = " + String(myPoint[0].y));
Serial.println("myPoint[0].x = " + String(myPoint[0].x));
if (myPoint[0].y > button1_y && myPoint[0].y < button1_y + button_h) {
if (touch_x > button3_x && touch_x < button3_x + button_w) {
handleButtonPress(3);
Serial.println("Button 3 pressed!");
} else if (touch_x > button2_x && touch_x < button2_x + button_w) {
handleButtonPress(2);
Serial.println("Button 2 pressed!");
} else if (touch_x > button1_x && touch_x < button1_x + button_w) {
handleButtonPress(1);
Serial.println("Button 1 pressed!");
}
}
}
}
void drawButtons() {
Display.fillRect(button1_x, button1_y, button_w, button_h, GD_RED);
Display.fillRect(button2_x, button2_y, button_w, button_h, GD_GREEN);
Display.fillRect(button3_x, button3_y, button_w, button_h, GD_BLUE);
Display.setCursor(button1_x + 25, button1_y + 20);
Display.setTextColor(GD_WHITE);
Display.setTextSize(2);
Display.print("Relay 1");
Display.setCursor(button2_x + 25, button2_y + 20);
Display.setTextColor(GD_WHITE);
Display.setTextSize(2);
Display.print("Relay 2");
Display.setCursor(button3_x + 25, button3_y + 20);
Display.setTextColor(GD_WHITE);
Display.setTextSize(2);
Display.print("Relay 3");
}
void handleButtonPress(int button) {
pressButton(button);
delay(200);
if (buttonModes[button - 1] == momentary) {
digitalWrite(relayPins[button - 1], HIGH);
releaseButton(button);
delay(200);
digitalWrite(relayPins[button - 1], LOW);
} else if (buttonModes[button - 1] == toggle) {
relayStates[button - 1] = !relayStates[button - 1];
digitalWrite(relayPins[button - 1], relayStates[button - 1]);
releaseButton(button);
}
}
void pressButton(int button) {
switch (button) {
case 1:
Display.fillRect(button1_x, button1_y, button_w, button_h, GD_ORANGE);
Display.setCursor(button1_x + 25, button1_y + 20);
Display.setTextColor(GD_WHITE);
Display.setTextSize(2);
Display.print("Relay 1");
break;
case 2:
Display.fillRect(button2_x, button2_y, button_w, button_h, GD_YELLOW);
Display.setCursor(button2_x + 25, button2_y + 20);
Display.setTextColor(GD_WHITE);
Display.setTextSize(2);
Display.print("Relay 2");
break;
case 3:
Display.fillRect(button3_x, button3_y, button_w, button_h, GD_CYAN);
Display.setCursor(button3_x + 25, button3_y + 20);
Display.setTextColor(GD_WHITE);
Display.setTextSize(2);
Display.print("Relay 3");
break;
}
}
void releaseButton(int button) {
switch (button) {
case 1:
Display.fillRect(button1_x, button1_y, button_w, button_h, GD_RED);
Display.setCursor(button1_x + 25, button1_y + 20);
Display.setTextColor(GD_WHITE);
Display.setTextSize(2);
Display.print("Relay 1");
break;
case 2:
Display.fillRect(button2_x, button2_y, button_w, button_h, GD_GREEN);
Display.setCursor(button2_x + 25, button2_y + 20);
Display.setTextColor(GD_WHITE);
Display.setTextSize(2);
Display.print("Relay 2");
break;
case 3:
Display.fillRect(button3_x, button3_y, button_w, button_h, GD_BLUE);
Display.setCursor(button3_x + 25, button3_y + 20);
Display.setTextColor(GD_WHITE);
Display.setTextSize(2);
Display.print("Relay 3");
break;
}
}
Comments
0 B
|👍
/👎