Changing Rectangle Colors on a TFT

Hi All. I’m fairly new to arduino, but have been working on a project that I’m pretty happy with so far. I have a TFT screen with essentially a “button matrix” programed to it. When you hit the “buttons” it triggers a relay. It’s all working great, except for one thing. I would like the “button” (fillRect) to change colors when triggered, say from light very to yellow & then back again. When I put the code into the if statement, the TFT ceases to operate the way it has, & one of the relays pops on & off as though it were running a blink sketch. Hoping someone here can help. Sketch attached. Thanks very much.

T

Work_Light_Sketch.ino (5.53 KB)

@tcrlighting

Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum. Other general help and troubleshooting advice can be found here. It will help you get the best out of the forum in the future.

More members will see your code if you post your code as described in the forum guidelines.

I have used a KBV style. Search for kbv with ctrl-F. Please compare the changes. Ask questions if you do not understand.

I strongly advise you to study the button examples that come with MCUFRIEND_kbv e.g. button_simple.ino, Touch_shield_new.ino

If you are interested, I will show how buttons would make your project easier.

#define USE_KBV 1
//Adafruit GFX & MCDFRIEND Libraries
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

//Touch Screen Library
#include <TouchScreen.h>

//Add Fonts
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
#include <FreeDefaultFonts.h>

//Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define NAVY 0x000F
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define MAROON 0x7800
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define LIGHTGREY 0xC618
#define DARKGREY 0x7BEF
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define PINK 0xF81F

//Set up the touch screen element of the TFT screen
#if USE_KBV
// copy-paste the Calibration result from Calibration example sketch
const int XP=6,XM=A2,YP=A1,YM=7; //320x480 ID=0x9488
const int TS_LEFT=866,TS_RT=141,TS_TOP=945,TS_BOT=96;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#else
#define YP A3
#define XM A2
#define YM 9
#define XP 8
#define SENSITIVITY 300
#define MINPRESSURE 10
#define MAXPRESSURE 1000
const int TS_LEFT = 0, TS_RT = 320, TS_TOP = 0, TS_BOT = 240;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, SENSITIVITY);

//Define MIN/MAX X/Y Coordinates
#define TS_MINX 204
#define TS_MINY 195
#define TS_MAXX 948
#define TS_MAXY 910
#endif

//Define Button Names
int WL1;
int WL2;
int WL3;
int WL4;
int ALLON;
int ALLOFF;
int GHOSTLIGHT;

//Define Relay Pins
int relay1 = 23;
int relay2 = 25;
int relay3 = 27;
int relay4 = 29;

//Define Relay States
int relay1State = 1;
int relay2State = 1;
int relay3State = 1;
int relay4State = 1;

void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    //Set relays High to start(no current flowing)
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    //Set up relay Pin Modes
    pinMode(relay1, OUTPUT);
    pinMode(relay2, OUTPUT);
    pinMode(relay3, OUTPUT);
    pinMode(relay4, OUTPUT);
    tft.reset();
    uint16_t ID = tft.readID();
    Serial.print("TFT ID=0x");
    Serial.println(ID, HEX);
    tft.begin(ID);
    tft.setRotation(1);
    tft.fillScreen(BLACK);
    //Draw button matrix
    tft.drawRect(49, 59, 172, 42, WHITE);
    tft.fillRect(50, 60, 170, 40, LIGHTGREY); //WL1
    tft.drawRect(49, 119, 172, 42, WHITE);
    tft.fillRect(50, 120, 170, 40, LIGHTGREY); //WL3
    tft.drawRect(49, 179, 172, 42, WHITE);
    tft.fillRect(50, 180, 170, 40, LIGHTGREY); //ON
    tft.drawRect(99, 239, 302, 42, WHITE);
    tft.fillRect(100, 240, 300, 40, LIGHTGREY); //GHOST
    tft.drawRect(274, 59, 172, 42, WHITE);
    tft.fillRect(275, 60, 170, 40, LIGHTGREY); //WL2
    tft.drawRect(274, 119, 172, 42, WHITE);
    tft.fillRect(275, 120, 170, 40, LIGHTGREY); //WL4
    tft.drawRect(274, 179, 172, 42, WHITE);
    tft.fillRect(275, 180, 170, 40, LIGHTGREY); //OFF
    tft.setCursor(120, 10);
    tft.setTextSize(4);
    tft.setTextColor(WHITE);
    tft.print("WORK LIGHTS");
    tft.drawFastHLine(70, 40, 350, RED);
    tft.setCursor(82, 65);
    tft.setTextColor(BLACK);
    tft.print("WL 1");
    tft.setCursor(82, 125);
    tft.print("WL 3");
    tft.setCursor(65, 185);
    tft.print("ALL ON");
    tft.setCursor(305, 65);
    tft.print("WL 2");
    tft.setCursor(305, 125);
    tft.print("WL 4");
    tft.setCursor(280, 185);
    tft.print("ALL OFF");
    tft.setCursor(120, 245);
    tft.print("GHOST LIGHT");
}

void loop() {
    // put your main code here, to run repeatedly:
    int pixel_x = 0, pixel_y = 0;  //.kbv use pixel coordinates
    TSPoint p = ts.getPoint();
    pinMode(XM, OUTPUT);  //.kbv must always restore XM, YP to OUTPUT
    pinMode(YP, OUTPUT);
    if (p.z > ts.pressureThreshhold) {
#if USE_KBV
        pixel_x = map(p.y, TS_TOP, TS_BOT, 0, 480); //landscape coordinates from Portrait calibration
        pixel_y = map(p.x, TS_RT, TS_LEFT, 0, 320);
#else

        Serial.print("X = "); Serial.print(p.x);
        Serial.print("\tY = "); Serial.print(p.y);
        Serial.print("\n");

        p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
        p.y = map(p.y, TS_MAXY, TS_MINY, 0, 240);
#endif
    }
    //if (p.x > 650 && p.x < 750 && p.y > 560 && p.y < 880) {
    if (pixel_x > 50 && pixel_x < 50+170 && pixel_y > 60 && pixel_y < 60+40) { //WL1
        if (relay1State == 1) {
            Serial.println("WL 1 ON!!!");
            digitalWrite(relay1, LOW);
            relay1State = 0;
            delay(100);
        }
        else {
            digitalWrite(relay1, HIGH);
            Serial.println("WL 1 OFF!!!");
            relay1State = 1;
            delay(100);
        }
    }
    //if (p.x > 650 && p.x < 750 && p.y > 150 && p.y < 450) {
    if (pixel_x > 275 && pixel_x < 275+170 && pixel_y > 60 && pixel_y < 60+40) { //WL2
        if (relay2State == 1) {
            Serial.println("WL 2 ON!!!");
            digitalWrite(relay2, LOW);
            relay2State = 0;
            delay(100);
        }
        else {
            digitalWrite(relay2, HIGH);
            Serial.print("WL 2 OFF!!!");
            relay2State = 1;
            delay(100);

        }
    }
    //if (p.x > 500 && p.x < 600 && p.y > 560 && p.y < 880) {
    if (pixel_x > 50 && pixel_x < 50+170 && pixel_y > 120 && pixel_y < 120+40) { //WL3
        if (relay3State == 1) {
            Serial.println("WL 3 ON!!!");
            digitalWrite(relay3, LOW);
            relay3State = 0;
            delay(100);
        }
        else {
            digitalWrite(relay3, HIGH);
            Serial.println("WL 3 OFF!!!");
            relay3State = 1;
            delay(100);
        }
    }
    //if (p.x > 500 && p.x < 600 && p.y > 150 && p.y < 450) {
    if (pixel_x > 275 && pixel_x < 275+170 && pixel_y > 120 && pixel_y < 120+40) { //WL4
        if (relay4State == 1) {
            Serial.println("WL 4 ON!!!");
            digitalWrite(relay4, LOW);
            relay4State = 0;
            delay(100);
        }
        else {
            digitalWrite(relay4, HIGH);
            Serial.println("WL 4 OFF!!!");
            relay4State = 1;
            delay(100);
        }
    }
    //if (p.x > 350 && p.x < 450 && p.y > 560 && p.y < 880) {
    if (pixel_x > 50 && pixel_x < 50+170 && pixel_y > 180 && pixel_y < 180+40) { //ON
        Serial.println("All Works On!!!");
        digitalWrite(relay1, LOW);
        digitalWrite(relay2, LOW);
        digitalWrite(relay3, LOW);
        digitalWrite(relay4, LOW);
        delay(1000);
    }
    //if (p.x > 350 && p.x < 450 && p.y > 150 && p.y < 450) {
    if (pixel_x > 275 && pixel_x < 275+170 && pixel_y > 180 && pixel_y < 180+40) { //OFF
        Serial.println("All Works Off!!!");
        digitalWrite(relay1, HIGH);
        digitalWrite(relay2, HIGH);
        digitalWrite(relay3, HIGH);
        digitalWrite(relay4, HIGH);
        delay(1000);
    }
    //if (p.x > 200 && p.x < 300 && p.y > 230 && p.y < 780) {
    if (pixel_x > 100 && pixel_x < 100+300 && pixel_y > 240 && pixel_y < 240+40) { //GHOST
        if (relay1State == 1 && relay2State == 1 && relay3State == 1 && relay4State == 1) {
            Serial.println("GHOST LIGHT ON!!!");
            digitalWrite(relay1, LOW);
            digitalWrite(relay2, HIGH);
            digitalWrite(relay3, LOW);
            digitalWrite(relay4, HIGH);
            relay1State = 0;
            relay2State = 0;
            delay(100);
        }
        else {
            Serial.println("GHOST LIGHT OFF!!!");
            digitalWrite(relay1, HIGH);
            digitalWrite(relay2, HIGH);
            digitalWrite(relay3, HIGH);
            digitalWrite(relay4, HIGH);
            relay1State = 1;
            relay2State = 1;
            relay3State = 1;
            relay4State = 1;
            delay(100);
        }
    }
}

David.