I do not have RGB leds but your program seems to work ok.
You have probably noticed that update gets a little messy when you have a LOT of buttons.
I put the buttons in a list and use helper functions e.g.
extern bool update_button(Adafruit_GFX_Button *b, bool down);
extern bool update_button_list(Adafruit_GFX_Button **pb);
extern void draw_button_list(Adafruit_GFX_Button **pb);
These helper functions use pointers. You might find pointers difficult to understand.
But they certainly make your program logic easier to maintain.
I suggest that you try buttons with different shape and size.
#if 1
// INCLUDE FILES
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <XPT2046_Touchscreen.h>
#define XPT_CS 3
#define XPT_IRQ 255 //ignore interrupt pin
//const int TS_BOT=3679,TS_TOP=347,TS_LEFT=258,TS_RT=3635; //old 2.4"
const int TS_BOT = 374, TS_TOP = 3944, TS_LEFT = 3714, TS_RT = 251; //new 2.4"
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define redled 5
#define greenled 6
#define blueled 7
int flag = 0, screen = 0;
// GLOBAL VARIABLES, CONSTRUCTORS
const int LED = A5; //which pin for LED
Adafruit_ILI9341 tft(10, 9, 8);
XPT2046_Touchscreen ts(XPT_CS, XPT_IRQ);
Adafruit_GFX_Button on_btn, off_btn, flickeron_btn, flickeroff_btn, led_btn, RGB_btn, red_btn, green_btn, blue_btn, yellow_btn, purple_btn, aqua_btn, back_btn;
// easier to handle a list of button pointers
Adafruit_GFX_Button *screen0[] = {&led_btn, &RGB_btn, NULL };
Adafruit_GFX_Button *screen1[] = {&on_btn, &off_btn, &flickeron_btn, &flickeroff_btn, &back_btn, NULL };
Adafruit_GFX_Button *screen2[] = {&red_btn, &green_btn, &blue_btn, &yellow_btn, &purple_btn, &aqua_btn, &back_btn, NULL };
// TOUCH MADE AS SEPARATE GLOBAL FUNCTION
int pixel_x, pixel_y; //make global
bool Touch_getXY(void)
{
bool down = ts.touched();
if (down) { //XPT2046_Touchscreen can use hardware
TS_Point p = ts.getPoint(); //XPT_2046_touchscreen wired for LANDSCAPE
pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
// tft.drawPixel(pixel_x, pixel_y, WHITE);
}
return down;
}
// FORWARD DECLARATIONS because IDE sometimes misses them
extern void setColor(int red, int green, int blue);
extern bool update_button(Adafruit_GFX_Button *b, bool down);
extern bool update_button_list(Adafruit_GFX_Button **pb);
extern void draw_button_list(Adafruit_GFX_Button **pb);
// INITIALISE GPIO, TOUCH, TFT, BUTTONS
void setup(void)
{
pinMode(A4, OUTPUT);
digitalWrite(A4, LOW); //so that an LED fits between A4, A5
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(blueled, OUTPUT);
ts.begin();
tft.begin();
tft.setRotation(1); //LANDSCAPE
on_btn.initButton(&tft, 80, 40, 120, 40, WHITE, GREEN, BLACK, "ON", 2);
off_btn.initButton(&tft, 240, 40, 120, 40, WHITE, RED, BLACK, "OFF", 2);
flickeron_btn.initButton(&tft, 80, 160, 120, 40, WHITE, GREEN, BLACK, "flickerON", 2);
flickeroff_btn.initButton(&tft, 240, 160, 120, 40, WHITE, RED, BLACK, "flickerOFF", 2);
led_btn.initButton(&tft, 80, 120, 120, 40, BLACK, WHITE, BLACK, "LED", 2);
RGB_btn.initButton(&tft, 240, 120, 120, 40, BLACK, WHITE, BLACK, "RGB", 2);
red_btn.initButton(&tft, 80, 40, 120, 40, WHITE, RED, BLACK, "RED", 2);
green_btn.initButton(&tft, 240, 40, 120, 40, WHITE, GREEN, BLACK, "GREEN", 2);
blue_btn.initButton(&tft, 80, 100, 120, 40, WHITE, BLUE, BLACK, "BLUE", 2);
yellow_btn.initButton(&tft, 240, 100, 120, 40, WHITE, YELLOW, BLACK, "YELLOW", 2);
purple_btn.initButton(&tft, 80, 160, 120, 40, WHITE, MAGENTA, BLACK, "PURPLE", 2);
aqua_btn.initButton(&tft, 240, 160, 120, 40, WHITE, CYAN, BLACK, "AQUA", 2);
back_btn.initButton(&tft, 160, 220, 120, 40, BLACK, WHITE, BLACK, "BACK", 2);
tft.fillScreen(BLACK);
led_btn.drawButton(false);
RGB_btn.drawButton(false);
}
void loop(void)
{
// READ TOUCH
if (screen == 0) {
update_button_list(screen0); //process all buttons
if (led_btn.justPressed()) {
screen = 1;
draw_button_list(screen1);
}
if (RGB_btn.justPressed()) {
screen = 2;
draw_button_list(screen2);
}
}
if (screen == 1) {
update_button_list(screen1); //process all buttons
if (on_btn.justPressed()) {
digitalWrite(LED, HIGH);
flag = 0;
}
if (off_btn.justPressed()) {
digitalWrite(LED, LOW);
flag = 0;
}
if (flickeron_btn.justPressed()) {
flag = 1;
}
if (flickeroff_btn.justPressed()) {
flag = 0;
}
if (flag == 1) {
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
}
if (back_btn.justPressed()) {
screen = 0;
draw_button_list(screen0);
digitalWrite(LED, LOW);
}
}
if (screen == 2) {
update_button_list(screen2); //process all buttons
if (red_btn.justPressed()) {
setColor(255, 0, 0);
}
if (green_btn.justPressed()) {
setColor(0, 255, 0);
}
if (blue_btn.justPressed()) {
setColor(0, 0, 255);
}
if (yellow_btn.justPressed()) {
setColor(255, 255, 0);
}
if (purple_btn.justPressed()) {
setColor(128, 0, 128);
}
if (aqua_btn.justPressed()) {
setColor(0, 255, 255);
}
if (back_btn.justPressed()) {
screen = 0;
draw_button_list(screen0);
setColor(0, 0, 0);
}
}
}
void setColor(int red, int green, int blue)
{
analogWrite(redled, red);
analogWrite(greenled, green);
analogWrite(blueled, blue);
}
/* update the state of a button and redraw as reqd
*
* main program can use isPressed(), justPressed() etc
*/
bool update_button(Adafruit_GFX_Button *b, bool down)
{
b->press(down && b->contains(pixel_x, pixel_y));
if (b->justReleased())
b->drawButton(false);
if (b->justPressed())
b->drawButton(true);
return down;
}
/* most screens have different sets of buttons
* life is easier if you process whole list in one go
*/
bool update_button_list(Adafruit_GFX_Button **pb)
{
bool down = Touch_getXY();
for (int i = 0 ; pb[i] != NULL; i++) {
update_button(pb[i], down);
}
return down;
}
void draw_button_list(Adafruit_GFX_Button **pb)
{
tft.fillScreen(BLACK);
for (int i = 0 ; pb[i] != NULL; i++) {
pb[i]->drawButton(false);
}
}
#endif