Countdown timer, if minute changed I want to refresh LCd

I have made a countdown timer basically. Its a keypad lock that opens with a passcode and then locks for 24 hours. I have a waveshare 1.28 inch lcd that shows how much time is left. I need to do a command to clear the lcd screen whenever the minute is changed - otherwise it keeps printing the new characthers on top of the old. So I tried storing the value of the current minute in a integer and to compare the current minute to the old minute in a function (countdown_check) - and if they dont match up - I do the clear screen command (gfx->fillScreen(BLACK); But it doesnt work. For some reason I dont even get any output in the monitor from the function countdown_check();

I am quite stuck and dont know how to solve it. I appreciate any help.

#include <Arduino_GFX_Library.h>
#include <Keypad.h>

Arduino_DataBus *bus = new Arduino_SWSPI(8 /* DC */, 9 /* CS */, 13 /* SCK */, 11 /* MOSI */, -1 /* MISO */);
//Arduino_GFX *gfx = create_default_Arduino_GFX();
//Arduino_GFX *gfx = new Arduino_ILI9341(bus, 7 /* RST */, 0 /* rotation */, true /* IPS */);
Arduino_GFX *gfx = new Arduino_GC9A01(bus, 7 /* RST */, 0 /* rotation */, true /* IPS */);
#define relay 2
#define red1 3
#define green1 4
#define Password_Length 8

long hour = 23, minute = 59, second = 59;
long countdown_time = (hour * 3600) + (minute * 60) + second;
int currentMinute = 0;
int oldMinute = 0;

bool unlocked = true;

char Data[Password_Length];
char Master[Password_Length] = "4444444";
byte data_count = 0, master_count = 0;
bool passwordValid;
char customKey;

const byte ROWS = 4;
const byte COLS = 3;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {A4, A2, 5, A1};
byte colPins[COLS] = {A5, A0, A3};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() {
  // put your setup code here, to run once:
  gfx->begin();
  gfx->fillScreen(BLACK);

#ifdef TFT_BL
  pinMode(TFT_BL, OUTPUT);
  digitalWrite(TFT_BL, HIGH);
#endif
  Serial.begin(9600);
  pinMode(relay, OUTPUT);
  pinMode(red1, OUTPUT);
  pinMode(green1, OUTPUT);
  digitalWrite(red1, 0);
  digitalWrite(green1, 0);
  digitalWrite(relay, 0); 
  delay(1000); // 1 seconds
  gfx->setCursor(70, 90);
  gfx->setTextColor(RED);
  gfx->setTextSize(2);
  gfx->println("Welcome!");
  gfx->fillScreen(BLACK);
  delay(2000);
}

void getpwd()
{
  while (data_count < 7)
  {
    customKey = customKeypad.getKey();
    if (customKey) {
      Data[data_count] = customKey;
      Serial.print(Data[data_count]);
      data_count++;
    }
  }
  if (data_count == Password_Length - 1) {

    if (!strcmp(Data, Master)) {
      Serial.println("\nCorrect");
      clearData();

      digitalWrite(relay, 1);
      digitalWrite(green1, 1);
      digitalWrite(red1, 0);
      gfx->print("Correct"); 
      delay(5000);
      digitalWrite(relay, 0);
      digitalWrite(green1, 0);
      digitalWrite(red1, 0);
      unlocked = false;
            gfx->fillScreen(BLACK);

    }

    else {
      Serial.println("\nIncorrect");

      digitalWrite(relay, 0);
      digitalWrite(green1, 0);
      digitalWrite(red1, 1);
      clearData();
      delay(5000);
      digitalWrite(relay, 0);
      digitalWrite(green1, 0);
      digitalWrite(red1, 0);

    }

  }
}


void clearData()
{
  while (data_count != 0) {
    Data[data_count--] = 0;
  }
  return;
}


// resetTimer will make the timer reset
void resetTimer() {
  countdown_time = (hour * 3600) + (minute * 60) + second;
}

void loop() {
  // put your main code here, to run repeatedly:
  gfx->setCursor(70, 90);
  gfx->setTextColor(RED);

  if (unlocked) {
    Serial.println("Waiting on user input");
    gfx->println("Enter!");
    // Wait on Keypad to be entered
    // and set unlocked to false if correct
    getpwd();
  } else {
    // Block until CountDown timer restarts
    resetTimer();
    Serial.println("Blocking 24 hours");
    lock24Hours();
  }




}
// lock24Hours locks the code execution for 24 hours
void lock24Hours() {
  while (!unlocked) {
    String timeStr = timeLeft();

    // Check if we get the exit code here
    if (timeStr == "exit") {
      // set unlocked to true to escape
      unlocked = true;
    }
    Serial.println(timeStr);
    gfx->setCursor(70, 90);
    gfx->println(timeStr);
    delay(1000);
  }

}

// timeLeft builds the timer string
String timeLeft() {
  long countdowntime_seconds = countdown_time - (millis() / 1000);

  if (countdowntime_seconds <= 1) {
  }
  // If Countdown time is less 0 or 0 , return a exit code
  if (countdowntime_seconds <= 0) {
    return "exit";
  }
  String result = "";
  if (countdowntime_seconds >= 0) {
    long countdown_hour = countdowntime_seconds / 3600;
    long countdown_minute = ((countdowntime_seconds / 60) % 60);
    long countdown_sec = countdowntime_seconds % 60;

    // If Hours is less than 10, then append 0 infront
    if (countdown_hour < 10) {
      result.concat("0");
    }
    result.concat(countdown_hour);
    result.concat(":");
    // If Min is less than 10, append 0
    if (countdown_minute < 10) {
      result.concat("0");
    }
    result.concat(countdown_minute);
    currentMinute = countdown_minute; 
    //   result.concat(":");
    countdown_check ();

    // If Sec is less than 10, append 0
    if (countdown_sec < 10) {
      // result.concat("0");
    }
    // result.concat(countdown_sec);
    Serial.println(result);
    return result;
  }
}

void countdown_check () {
  
  if (currentMinute != oldMinute) {
    Serial.println(oldMinute); 
    Serial.println(currentMinute); 
    gfx->fillScreen(BLACK);
    currentMinute = oldMinute; 
    Serial.println(currentMinute); 
    Serial.println(oldMinute); 
  }    else {
      Serial.println("Wait for it"); 
    }
}

Hello theswedishmaker
All variables using results from the millis() function have to be declared as unsigned long.
Have a nice day and enjoy coding in C++.

ah! of course! I´ll try that. Thanks @paulpaulson

I was thinking: "Why is this a problem?" Then I realized that, the way that the (stupidly written) graphics library works, writing a new character does not get rid of the old character.

Why is that graphics library so stupidly written? Why does it not, in addition to a foreground color, accept a background color for text (or at least default to, for example, a black background), thus preventing this exact problem?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.