Stop screen colour redrawing (only draw once per loop)

Hello,

I am wondering if there is a way to set the background colour of a tft screen one time without having that background redrawn every loop. Perhaps there is a simple answer. "tft.fillRect(1, 1, 479, 319, GREEN);", and "tft.fillScreen(GREEN);" cause flickering due to the redraw every loop.

My requirement is that if the time is between two certain hours (say 13:00 and 14:00), then the screen should turn red and a message should display. From 14:00 to 15:00, the screen turns green and a different message displays. The below sketch works, although because of the redraw every loop it's not visually appealing (and aside from the countdown timer not working for task 1 yet, but that's a different issue)

Here is the full sketch if that is useful:

#include "Countimer.h"
#include <RTClib.h>
#include <Wire.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

#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 GREY            0x7BEF
#define NAVY            0x000F
#define DARKGREEN       0x03E0
#define DARKCYAN        0x03EF
#define MAROON          0x7800
#define PURPLE          0x780F 
#define OLIVE           0x7BE0
#define LIGHTGREY       0xC618
#define ORANGE          0xFD20
#define GREENYELLOW     0xAFE5
#define PINK            0xF81F

RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char dateBuffer[26];
int x;
Countimer tDown;

void setup (){
  rtc.begin();  
  uint16_t ID = tft.readID();
  if (ID == 0xD3D3) ID = 0x9486;
  tft.begin(ID);
  tft.setRotation(1);
  tft.fillScreen(BLACK);    
  Wire.begin();     
  tDown.run();  
  x = 1;
}

void loop (){   
  DateTime now = rtc.now();
  tDown.run();
  tDown.start();

  if (x == 1){
  tft.setTextColor(GREY, BLACK);
  }

  if (x == 2){
  tft.setTextColor(GREY, RED);
  }

  if (x == 3){
  tft.setTextColor(GREY, GREEN);
  }
  
  tft.setTextSize(2);
  tft.setCursor(10, 17);
  tft.print(daysOfTheWeek[now.dayOfTheWeek()]);
  sprintf(dateBuffer,"  %02u/%02u/%04u  %02u:%02u:%02u ",now.day(),now.month(),now.year(),now.hour(),now.minute(),now.second());
  tft.setCursor(205, 17);
  tft.print(dateBuffer); 
  delay(1000); 
  
  if ((now.hour()*100) + now.minute()>= 430 && ((now.hour()*100) + now.minute()) < 440){
    Task1();
  }

  if ((now.hour()*100) + now.minute()>= 440 && ((now.hour()*100) + now.minute()) < 441){
    Task2();
  }
  
  if ((now.hour()*100) + now.minute()>= 441 && ((now.hour()*100) + now.minute()) < 442){
    Task3();
  } 
  
  if ((now.hour()*100) + now.minute()>= 442 && ((now.hour()*100) + now.minute()) < 443){
    Task4();
  }
  
  if ((now.hour()*100) + now.minute()>= 443 && ((now.hour()*100) + now.minute()) < 444){
    Task4();
  }

}
  
void Task1(){
  String Task1 = "EXERCISE";
  unsigned int lastStringLength = Task1.length();
  tft.setTextColor(WHITE, BLACK);
  tft.setTextSize(3);
  myGLCD.fillRect(1,15,478,304);
//  tft.fillRect(1, 1, 479, 319, RED);  //Coloured bar
  tft.setCursor(tft.width()/2 - (Task1.length()*17)/2,tft.height()/2 - 10);
  tft.print(Task1);
  tDown.setCounter(00, 00, 10, tDown.COUNT_DOWN, tDownComplete); //Hours, Minutes, Seconds
  tDown.setInterval(CountDownTimer, 1000);
  return;
}

void Task2(){
  x = 2;
  String Task1 = "WORK OVER";
  unsigned int lastStringLength = Task2.length();
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(3);
  tft.fillRect(1, 1, 479, 319, BLUE);  //Coloured bar
  tft.setCursor(tft.width()/2 - (Task2.length()*17)/2,tft.height()/2 - 10);
  tft.print(Task2);
  tDown.setCounter(00, 00, 10, tDown.COUNT_DOWN, tDownComplete); //Hours, Minutes, Seconds
  tDown.setInterval(CountDownTimer, 1000);
  return;
}

void Task3(){
  x = 3;
  String Task3 = "BED";
  unsigned int lastStringLength = Task3.length();
  tft.setTextColor(WHITE, RED);
  tft.setTextSize(3);
  tft.fillRect(1, 1, 479, 319, RED);  //Coloured bar
  tft.setCursor(tft.width()/2 - (Task3.length()*17)/2,tft.height()/2 - 10);
  tft.print(Task3);
  tDown.setCounter(00, 00, 10, tDown.COUNT_DOWN, tDownComplete); //Hours, Minutes, Seconds
  tDown.setInterval(CountDownTimer, 1000);
  return;
}

void Task4(){
  x = 4;
  String Task3 = "JOG";
  unsigned int lastStringLength = Task4.length();
  tft.setTextColor(BLACK, GREEN);
  tft.setTextSize(3);
  tft.fillRect(1, 1, 479, 319, GREEN);  //Coloured bar
  tft.setCursor(tft.width()/2 - (Task4.length()*17)/2,tft.height()/2 - 10);
  tft.print(Task4);
  tDown.setCounter(00, 00, 10, tDown.COUNT_DOWN, tDownComplete); //Hours, Minutes, Seconds
  tDown.setInterval(CountDownTimer, 1000);
  return;
}

void Task5(){
  
}

void CountDownTimer(){
  if (x == 1){
  tft.setTextColor(WHITE, BLACK);
  }

  if (x == 2){
  tft.setTextColor(WHITE, RED);
  }

  if (x == 3){
  tft.setTextColor(BLACK, GREEN);
  }
  
  tft.setTextSize(3);
  tft.setCursor(170, 275);
  tft.print(tDown.getCurrentTime());
}

void tDownComplete(){
}

boolean bTurnGreen = True;

if (now >= 1pm and now <= 3pm and bTurnGreen == True)
{
//turn the background color green

bTurnGreen = False;
}

That worked when I changed it to:

int bTurnGreen = 1;


if (now >= 1pm and now <= 3pm and bTurnGreen == 1) 
{
   //turn the background color green

   bTurnGreen = 2;
}

I kept getting the error "'True' was not declared in this scope" even with the 'boolean' specified as you outlined.

Now I will have to change the numbers for every separate colour I want the screen to change, but it's still a good solution! Thank you!

boolean bTurnGreen = True;

should be:

boolean bTurnGreen = true;   // true and false are lower-case

Please use ctrl-T to format your code. It makes it readable by human people.

You will also note that keywords like

true

or

false

appear in a funny BLUE colour in the IDE.