Hi
I have a TFT screen which i have coded to help with testing trailer lighting while im working on the in my workshop (not possible to get a car to them all the time) so using a Mega and a 8-Way relay block i came up with this.
But having used it briefly, i decided that the "Side lights" needed to be toggled, so i can easily check the brake lights. idea being i can have the side lights turned on, and then press the button on TFT to put the Brake lights on for a set amount of time set in the "ON" delay setting.
I have tried i cane seem to work this out... code is below..
#include <TouchScreen.h>
#include <Adafruit_GFX.h> // Core graphic library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <Wire.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
const int relay1 = 53; // output for relay - LEFT SIDE LIGHTS
const int relay2 = 51; // output for relay - RIGHT SIDE LIGHT
const int relay3 = 49; // output for relay - LEFT INDICATOR
const int relay4 = 47; // output for relay - RIGHT INDICATOR
const int relay5 = 45; // output for relay - BRAKE LIGHTS
const int relay6 = 43; // output for relay - FOG LIGHT
const int relay7 = 41; // output for relay - SPARE...
const int relay8 = 39; // output for relay - SPARE...
#define FLASH_ON 600 // delay between FLASHES
#define FLASH_OFF 500 // delay between FLASHES
#define NUM_FLASH 8 // Number of Flashes for Indicators
#define ON 5000 // test time delay
const int XP = 8, XM = A2, YP = A3, YM = 9; //320x480 ID=0x9486
const int TS_LEFT = 129, TS_RT = 887, TS_TOP = 949, TS_BOT = 97;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button SIDE_btn, SIDEON_btn, BRAKE_btn, LIND_btn, RIND_btn, FOG_btn, RSIDE_btn, LSIDE_btn;
int pixel_x, pixel_y; //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
return pressed;
}
// R G B
#define BLACK 0x0000 // 0, 0, 0
#define RED 0xF800 // 255, 0, 0
#define WHITE 0xFFFF // 255, 255, 255
#define ORANGE 0xFD20 // 255, 165, 0
#define GREEN 0x0F00 // 0, 255, 0
void setup(void)
{
Wire.begin();
pinMode(relay1, OUTPUT); // SET RELAY AS OUTPUTS
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(relay5, OUTPUT);
pinMode(relay6, OUTPUT);
pinMode(relay7, OUTPUT);
pinMode(relay8, OUTPUT);
digitalWrite(relay1, HIGH); // SET AS HIGH AT POWER UP (OFF)
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(relay5, HIGH);
digitalWrite(relay6, HIGH);
digitalWrite(relay7, HIGH);
digitalWrite(relay8, HIGH);
uint16_t ID = tft.readID();
if (ID == 0xD3D3) ID = 0x9481; // write-only shield
tft.begin(ID);
tft.setRotation(0); //PORTRAIT
tft.fillScreen(BLACK); //CLEAR SCREEN
//----------------- BUTTON INFO -----------------
// POSITION INFO, border, background, writing colour, "text", size
SIDEON_btn.initButton(&tft, 160, 45, 240, 60, RED, WHITE, RED, "*SIDE ON*", 4);
BRAKE_btn.initButton(&tft, 160, 110, 240, 60, RED, WHITE, RED, "* BRAKE *", 4);
LIND_btn.initButton(&tft, 160, 175, 240, 60, WHITE, ORANGE, RED, "* IND ", 4);
RIND_btn.initButton(&tft, 160, 240, 240, 60, WHITE, ORANGE, RED, " IND *", 4);
LSIDE_btn.initButton(&tft, 160, 305, 240, 60, WHITE, RED, WHITE, "* SIDE ", 4);
RSIDE_btn.initButton(&tft, 160, 370, 240, 60, WHITE, RED, WHITE, " SIDE *", 4);
FOG_btn.initButton(&tft, 160, 435, 240, 60, GREEN, RED, GREEN, " FOG *", 4);
/*
SIDE_btn.initButton(&tft, 95, 45, 115, 60, RED, WHITE, RED, "* SIDE *", 4);
SIDE1_btn.initButton(&tft, 220, 45, 115, 60, RED, WHITE, RED, "* SIDE *", 4);
BRAKE_btn.initButton(&tft, 160, 110, 240, 60, RED, WHITE, RED, "* BRAKE *", 4);
LIND_btn.initButton(&tft, 160, 175, 240, 60, WHITE, ORANGE, RED, "* IND ", 4);
RIND_btn.initButton(&tft, 160, 240, 240, 60, WHITE, ORANGE, RED, " IND *", 4);
LSIDE_btn.initButton(&tft, 160, 305, 240, 60, WHITE, RED, WHITE, "* SIDE ", 4);
RSIDE_btn.initButton(&tft, 160, 370, 240, 60, WHITE, RED, WHITE, " SIDE *", 4);
FOG_btn.initButton(&tft, 160, 435, 240, 60, GREEN, RED, GREEN, " FOG *", 4);
*/
//SIDE_btn.drawButton(false);
SIDEON_btn.drawButton(false);
BRAKE_btn.drawButton(false);
LIND_btn.drawButton(false);
RIND_btn.drawButton(false);
RSIDE_btn.drawButton(false);
LSIDE_btn.drawButton(false);
FOG_btn.drawButton(false);
}
void loop() {
//----------------- BUTTON INFO -----------------
bool down = Touch_getXY();
// SIDE_btn.press(down && SIDE_btn.contains(pixel_x, pixel_y));
SIDEON_btn.press(down && SIDE_btn.contains(pixel_x, pixel_y));
BRAKE_btn.press(down && BRAKE_btn.contains(pixel_x, pixel_y));
LIND_btn.press(down && LIND_btn.contains(pixel_x, pixel_y));
RIND_btn.press(down && RIND_btn.contains(pixel_x, pixel_y));
RSIDE_btn.press(down && RSIDE_btn.contains(pixel_x, pixel_y));
LSIDE_btn.press(down && LSIDE_btn.contains(pixel_x, pixel_y));
FOG_btn.press(down && FOG_btn.contains(pixel_x, pixel_y));
// if (SIDE_btn.justReleased())
// SIDE_btn.drawButton();
if (SIDEON_btn.justReleased())
SIDEON_btn.drawButton();
if (BRAKE_btn.justReleased())
BRAKE_btn.drawButton();
if (LIND_btn.justReleased())
LIND_btn.drawButton();
if (RIND_btn.justReleased())
RIND_btn.drawButton();
if (RSIDE_btn.justReleased())
RSIDE_btn.drawButton();
if (LSIDE_btn.justReleased())
LSIDE_btn.drawButton();
if (FOG_btn.justReleased())
FOG_btn.drawButton();
if (SIDEON_btn.justPressed()) {
SIDEON_btn.drawButton(true);
// ??????????????????? CODE TO PUT HERE (AND AT THE START) TO TOGGLE THIS OUTPUT -- relay1 WHEN THE
// ??????????????????? SIDE_btn AREA IS PRESSED ON tft SCREEN......
}
if (BRAKE_btn.justPressed()) {
BRAKE_btn.drawButton(true);
digitalWrite(relay5, LOW);
delay(ON);
digitalWrite(relay5, HIGH);
}
if (LSIDE_btn.justPressed()) {
LSIDE_btn.drawButton(true);
digitalWrite(relay1, LOW);
delay(ON);
digitalWrite(relay1, HIGH);
}
if (RSIDE_btn.justPressed()) {
RSIDE_btn.drawButton(true);
digitalWrite(relay2, LOW);
delay(ON);
digitalWrite(relay2, HIGH);
}
if (FOG_btn.justPressed()) {
FOG_btn.drawButton(true);
digitalWrite(relay6, LOW);
delay(ON);
digitalWrite(relay6, HIGH);
}
if (LIND_btn.justPressed()) {
LIND_btn.drawButton(true);
for (int i = 0; i < NUM_FLASH; i++) {
digitalWrite(relay3, LOW);
delay(FLASH_ON);
digitalWrite(relay3, HIGH);
delay(FLASH_OFF);
}
}
if (RIND_btn.justPressed()) {
RIND_btn.drawButton(true);
for (int i = 0; i < NUM_FLASH; i++) {
digitalWrite(relay4, LOW);
delay(FLASH_ON);
digitalWrite(relay4, HIGH);
delay(FLASH_OFF);
}
}
}```