Hello,
I am building a calendar to display on a tft screen, and am wondering how to get the loop to enter in to the relevant statement based on current time measured when the Arduino is powered off and on again. Is this a logic problem or something more complicated? Perhaps someone can point out where my issue lays.
Currently, the program waits for the criteria of the first if statement to be met regardless of the current time (even if the current time meets the criteria of a statement further down), but I would like the program to enter in to the relevant statement based on the current time.
I initially thought that void loop ran through the entire loop and ignored statements whose criteria are not yet met instead of looping up to the first statement until the criteria of that first statement is met.
My thoughts are that as I am dealing with time, perhaps the problem has something to do with the fact that the criteria for the first statement will always be met eventually because the time figures recur daily, and that is why the loop keeps waiting for it in the event that the Arduino is powered off and on again, even if the current time measured meets the criteria of a statement further down.
void loop (){
DateTime now = rtc.now();
int CurrentTime = (now.hour() * 100) + now.minute();
//------------------- Task 1 -------------------//
if (CurrentTime >= a && CurrentTime < b && changebackgroundcolour == 1) {
changebackgroundcolour = 2;
String Task1 = "TASK 1 TEXT";
unsigned int lastStringLength = Task1.length();
tft.setTextColor(WHITE, BLACK);
tft.setTextSize(3);
tft.fillRect(1, 1, 479, 319, BLACK);
tft.setCursor(tft.width() / 2 - (Task1.length() * 17) / 2, tft.height() / 2 - 10);
tft.print(Task1);
tDown.setCounter(00, 00, 55, tDown.COUNT_DOWN, tDownComplete); //Hours, Minutes, Seconds
tDown.setInterval(CountDownTimer, 1000);
}
//------------------- Task 2 -------------------//
else if (CurrentTime >= b && CurrentTime < c && changebackgroundcolour == 2) {
num = 2;
changebackgroundcolour = 3;
String Task2 = "TASK 2 TEXT";
unsigned int lastStringLength = Task2.length();
tft.setTextColor(WHITE, BLUE);
tft.setTextSize(3);
tft.fillRect(1, 1, 479, 319, BLUE);
tft.setCursor(tft.width() / 2 - (Task2.length() * 17) / 2, tft.height() / 2 - 10);
tft.print(Task2);
tDown.setCounter(00, 00, 55, tDown.COUNT_DOWN, tDownComplete); //Hours, Minutes, Seconds
tDown.setInterval(CountDownTimer, 1000);
}
//------------------- Task 3 -------------------//
else if (CurrentTime >= c && CurrentTime < d && changebackgroundcolour == 3) {
num = 3;
changebackgroundcolour = 4;
String Task3 = "TASK 3 TEXT";
unsigned int lastStringLength = Task3.length();
tft.setTextColor(BLACK, RED);
tft.setTextSize(3);
tft.fillRect(1, 1, 479, 319, RED);
tft.setCursor(tft.width() / 2 - (Task3.length() * 17) / 2, tft.height() / 2 - 10);
tft.print(Task3);
tDown.setCounter(00, 00, 55, tDown.COUNT_DOWN, tDownComplete); //Hours, Minutes, Seconds
tDown.setInterval(CountDownTimer, 1000);
}
//------------------- Task 4 -------------------//
else if (CurrentTime >= d && CurrentTime < e && changebackgroundcolour == 4) {
num = 4;
changebackgroundcolour = 5;
String Task4 = "TASK 4 TEXT";
unsigned int lastStringLength = Task4.length();
tft.setTextColor(BLACK, ORANGE);
tft.setTextSize(3);
tft.fillRect(1, 1, 479, 319, ORANGE);
tft.setCursor(tft.width() / 2 - (Task4.length() * 17) / 2, tft.height() / 2 - 10);
tft.print(Task4);
tDown.setCounter(00, 00, 55, tDown.COUNT_DOWN, tDownComplete); //Hours, Minutes, Seconds
tDown.setInterval(CountDownTimer, 1000);
}
//------------------- Task 5 -------------------//
else if (CurrentTime >= e && CurrentTime < f && changebackgroundcolour == 5) {
num = 5;
changebackgroundcolour = 6;
String Task5 = "TASK 5 TEXT";
unsigned int lastStringLength = Task5.length();
tft.setTextColor(BLACK, CYAN);
tft.setTextSize(3);
tft.fillRect(1, 1, 479, 319, CYAN);
tft.setCursor(tft.width() / 2 - (Task5.length() * 17) / 2, tft.height() / 2 - 10);
tft.print(Task5);
tDown.setCounter(00, 00, 55, tDown.COUNT_DOWN, tDownComplete); //Hours, Minutes, Seconds
tDown.setInterval(CountDownTimer, 1000);
}
}