If statement/else if statement logic question

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);
  }
}

ShooterMcGavin:
I initially thought that void loop ran through the entire loop and ignored statements whose criteria are not yet met

You thought correctly.

Please post your FULL sketch.

A friend of mine pointed out that that "changebackgroundcolour" was not allowing entry in to the relevant statement for the time because its value wasn't set to the criteria to enter any subsequent statement until it had already run through the prior statement (essentially meaning that it needed to run through the first loop to enter the second, and the second to enter the third etc...)

The below modifications have made it work, although strangely it it temperamental. I have to upload the sketch multiple times before it actually works.

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

#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define CYAN            0x07FF
#define WHITE           0xFFFF
#define ORANGE          0xFD20

RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char dateBuffer[26];
int a, b, c, d, e, f;
Countimer tDown;
int num = 1;
int changeBackgroundColour = 1;

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();
}

void loop () {
  DateTime now = rtc.now();
  int currentTime = ((now.hour() * 100) + now.minute());
  tDown.run();
  tDown.start();

  if (num == 1) {
    tft.setTextColor(WHITE, BLACK);
  }

  if (num == 2) {
    tft.setTextColor(WHITE, BLUE);
  }

  if (num == 3) {
    tft.setTextColor(BLACK, RED);
  }

  if (num == 4) {
    tft.setTextColor(BLACK, ORANGE);
  }

  if (num == 5) {
    tft.setTextColor(BLACK, CYAN);
  }

  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);

  // -- For testing purposes -- //
  a = 0207;  //02:08am
  b = 0208;
  c = b + 1;
  d = c + 1;
  e = d + 1;
  f = e + 1;

  //---------  Task 1 ---------//
  if (currentTime >= a && currentTime < b && changeBackgroundColour != 2) {
      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 != 3) {
      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 != 4) {
      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 != 5) {
      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 != 6) {
      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);
  }


  //---------  Tasks End ---------//
}

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

  if (num == 2) {
    tft.setTextColor(WHITE, BLUE);
  }

  if (num == 3) {
    tft.setTextColor(BLACK, RED);
  }

  if (num == 4) {
    tft.setTextColor(BLACK, ORANGE);
  }

  if (num == 5) {
    tft.setTextColor(BLACK, CYAN);
  }

  tft.setTextSize(3);
  tft.setCursor(170, 275);
  tft.print(tDown.getCurrentTime());
}

void tDownComplete() {
}