I have a 3.5 inch tft display for my arduino mega that I want to use for my airsoft bomb. So I have to display the time it has before exploding, but I can't write over the old value of min and sec without writing over, thus making it unreadable. What I tried was to make a box over the time to make it the same colour as the background and then print the values, but this way the time flickers making it a bit hard to read in the heat of the moment. This code is to set the time for the bomb before the game even starts and store it to the EEPROM using a key switch.
This is the code I am using:
#include <EEPROM.h>
#include <Keypad.h>
#include <LCDWIKI_GUI.h>
#include <LCDWIKI_KBV.h>
LCDWIKI_KBV mylcd(ILI9486, 40, 38, 39, -1, 41);
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
const byte ROWS = 4;
const byte COLS = 3;
char Keys[ROWS][COLS] = {
{ '3', '2', '1' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ '*', '0', '#' }
};
byte rowPins[ROWS] = { 3, 8, 7, 5 };
byte colPins[COLS] = { 4, 2, 6 };
Keypad keypad = Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
int min, sec, newMin, newSec, i = 0, numKey = 0, numKey2 = 0;
String inputStr;
void setup() {
Serial.begin(9600);
mylcd.Init_LCD();
mylcd.Fill_Screen(BLACK);
pinMode(13, INPUT_PULLUP);
sec = EEPROM.read(0);
min = EEPROM.read(1);
}
void loop() {
char keypressed = keypad.getKey();
int but = digitalRead(13);
mylcd.Set_Rotation(1);
mylcd.Set_Text_Mode(1);
//mylcd.Set_Text_Back_colour(BLACK);
if (but == HIGH) {
if (keypressed != NO_KEY) {
inputStr += keypressed;
numKey++;
if (i == 0) {
if (numKey == 2) {
newSec = inputStr.toInt();
i = 1;
inputStr = "";
numKey = 0;
}
} else if (i == 1) {
numKey2++;
if (numKey == 2) {
newMin = inputStr.toInt();
min = newMin;
sec = newSec;
if (sec > 59) {
sec = sec - 60;
min++;
numKey2 = 0;
}
EEPROM.write(0, sec);
EEPROM.write(1, min);
}
}
}
showmod();
} else if (but == LOW) {
showtime();
mylcd.Fill_Rect(175, 65, 125, 35, BLACK);
}
}
void showtime() {
mylcd.Set_Text_colour(BLUE);
mylcd.Set_Text_Back_colour(BLACK);
mylcd.Set_Text_Size(5);
if (min > 10) {
mylcd.Print_Number_Int(min, 175, 25, 1, 0, 10);
} else if (min < 10) {
mylcd.Print_Number_Int(0, 175, 25, 1, 0, 10);
mylcd.Print_Number_Int(min, 205, 25, 1, 0, 10);
}
mylcd.Print(":", 225, 25);
if (sec > 10) {
mylcd.Print_Number_Int(sec, 245, 25, 1, 0, 10);
} else if (sec < 10) {
mylcd.Print_Number_Int(0, 245, 25, 1, 0, 10);
mylcd.Print_Number_Int(sec, 275, 25, 1, 0, 10);
}
}
void showmod() {
mylcd.Set_Text_colour(BLUE);
mylcd.Set_Text_Back_colour(BLACK);
mylcd.Set_Text_Size(5);
if (min > 10) {
mylcd.Print_Number_Int(min, 175, 25, 1, 0, 10);
} else if (min < 10) {
mylcd.Print_Number_Int(0, 175, 25, 1, 0, 10);
mylcd.Print_Number_Int(min, 205, 25, 1, 0, 10);
}
mylcd.Print(":", 225, 25);
if (sec > 10) {
mylcd.Print_Number_Int(sec, 245, 25, 1, 0, 10);
} else if (sec < 10) {
mylcd.Print_Number_Int(0, 245, 25, 1, 0, 10);
mylcd.Print_Number_Int(sec, 275, 25, 1, 0, 10);
}
mylcd.Set_Text_colour(RED);
mylcd.Set_Text_Size(5);
if (newMin > 10) {
mylcd.Print_Number_Int(newMin, 175, 65, 1, 0, 10);
} else if (newMin < 10) {
mylcd.Print_Number_Int(0, 175, 65, 1, 0, 10);
mylcd.Print_Number_Int(newMin, 205, 65, 1, 0, 10);
}
mylcd.Print(":", 225, 65);
if (newSec > 10) {
mylcd.Print_Number_Int(newSec, 245, 65, 1, 0, 10);
} else if (newSec < 10) {
mylcd.Print_Number_Int(0, 245, 65, 1, 0, 10);
mylcd.Print_Number_Int(newSec, 275, 65, 1, 0, 10);
}
}