Auto-Manual Mode Slection Blynk app

Dear all.

I am trying to do simple time switch app using Blynk app. I wanted to add simple feature manual and automode

In manual mode i can select any Button as per manual choice.
In auto mode if timeperiod is between Starttime and stop button it should enable equivalent button

I have 4 button for different time Zone.

Timezone1,TimeZone2, timezone3, timezone4.

my RTC time Working 12:00, In auto mode It Directly going to Timezone 4

If i use condition

if (hour() == Wk_Emd_SThour && hour() == Wk_Emd_SPhour) instead of if (hour() == Wk_Emd_SThour || hour() == Wk_Emd_SPhour)

code will not work. Thats reason i am using Or condition

TimeZone1 STTime: 12:01 STPTime :12:02

TimeZone2 STTime: 12:03 STPTime :12:04

TimeZone3 STTime: 12:05 STPTime :12:06

TimeZone4 STTime: 12:07 STPTime :12:08

BLYNK_WRITE(V3)// TimeZone1 TimeInput Store in below variable
{

  TimeInputParam t(param);
  SThour = t.getStartHour();
  STmin = t.getStartMinute();
  STsec = t.getStartSecond();
  SPhour = t.getStopHour();
  SPmin = t.getStopMinute();
  SPsec = t.getStopSecond();

}


BLYNK_WRITE(V4)// TimeZone2 TimeInput Store in below variable
{


  TimeInputParam t(param);
  Wk_Emd_SThour = t.getStartHour();
  Wk_Emd_STmin = t.getStartMinute();
  Wk_Emd_STsec = t.getStartSecond();
  Wk_Emd_SPhour = t.getStopHour();
  Wk_Emd_SPmin = t.getStopMinute();
  Wk_Emd_SPsec = t.getStopSecond();

}


BLYNK_WRITE(V5)// TimeZone3 TimeInput Store in below variable
{

  TimeInputParam t(param);
  Al_SThour = t.getStartHour();
  Al_STmin = t.getStartMinute();
  Al_STsec = t.getStartSecond();
  Al_SPhour = t.getStopHour();
  Al_SPmin = t.getStopMinute();
  Al_SPsec = t.getStopSecond();

}



BLYNK_WRITE(V6)// TimeZone4 TimeInput Store in below variable
{

  TimeInputParam t(param);
  TimeZ4__SThour = t.getStartHour();
  TimeZ4__STmin = t.getStartMinute();
  TimeZ4__STsec = t.getStartSecond();
  TimeZ4__SPhour = t.getStopHour();
  TimeZ4__SPmin = t.getStopMinute();
  TimeZ4__SPsec = t.getStopSecond();

}



BLYNK_WRITE(V50)  // Manual/Auto selection
{
  if (param.asInt() == 0)

  {
    manual = 1;
    Serial.println("Manual mode is selected ");
    // restorelastbuttonpressed ();



  } else
  {
    manual = 0;
    Serial.println("AUTO mode is selected ");
    AutoMode_Function();
    // checklastbuttonpressed();
  }
}



void AutoMode_Function()
{
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
  Serial.print("Current Time: ");
  Serial.println(currentTime);

  if (hour() >= SThour ||  hour() <= SPhour)
  {
    TimeZone1 = 1;
    Blynk.virtualWrite(V51, 1);
    Blynk.virtualWrite(V52, 0);
    Blynk.virtualWrite(V53, 0);
    Blynk.virtualWrite(V54, 0);

  }else
  if (hour() == Wk_Emd_SThour || hour() == Wk_Emd_SPhour)
  {
    TimeZone2 = 1;
    Blynk.virtualWrite(V51, 0);
    Blynk.virtualWrite(V52, 1);
    Blynk.virtualWrite(V53, 0);
    Blynk.virtualWrite(V54, 0);
  }else
  if (hour() >= Al_SThour || hour() <= Al_SPhour)
  {
    TimeZone3 = 1;
    Blynk.virtualWrite(V51, 0);
    Blynk.virtualWrite(V52, 0);
    Blynk.virtualWrite(V53, 1);
    Blynk.virtualWrite(V54, 0);
  }else
  if (hour() >= TimeZ4__SThour || hour() <= TimeZ4__SPhour)
  {
    TimeZone4 = 1;
    Blynk.virtualWrite(V51, 0);
    Blynk.virtualWrite(V52, 0);
    Blynk.virtualWrite(V53, 0);
    Blynk.virtualWrite(V54, 1);
  }


}

With Below code i could able to switch between timing. How can i Keep track of old Status of automode

Whenever i validate button then only it change to other zone else remain in previous stage

BLYNK_WRITE(V50)  // Manual/Auto selection
{
  if (param.asInt() == 0)

  {
    manual = 1;
    Serial.println("Manual mode is selected ");
    // restorelastbuttonpressed ();



  } else
  {
    manual = 0;
    Serial.println("AUTO mode is selected ");
    AutoMode_Function();
    // checklastbuttonpressed();
  }
}



void AutoMode_Function()
{
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
  Serial.print("Current Time: ");
  Serial.println(currentTime);
  
  if (hour() >= SThour &&  hour() <= SPhour)
  {
    if (minute() >= STmin && minute() <= SPmin)
    {
      TimeZone1 = 1;
      Blynk.virtualWrite(V51, 1);
      Blynk.virtualWrite(V52, 0);
      Blynk.virtualWrite(V53, 0);
      Blynk.virtualWrite(V54, 0);
    }

  }

  if (hour() >= Wk_Emd_SThour && hour() <= Wk_Emd_SPhour)
  {
    if (minute() >= Wk_Emd_STmin && minute() <= Wk_Emd_SPmin)
    {
      TimeZone2 = 1;
      Blynk.virtualWrite(V51, 0);
      Blynk.virtualWrite(V52, 1);
      Blynk.virtualWrite(V53, 0);
      Blynk.virtualWrite(V54, 0);
    }
  }
  if (hour() >= Al_SThour && hour() <= Al_SPhour)
  {
    if (minute() >= Al_STmin && minute() <= Al_SPmin)
    {
      TimeZone3 = 1;
      Blynk.virtualWrite(V51, 0);
      Blynk.virtualWrite(V52, 0);
      Blynk.virtualWrite(V53, 1);
      Blynk.virtualWrite(V54, 0);
    }
  }
  if (hour() >= TimeZ4__SThour && hour() <= TimeZ4__SPhour)
  {
    if (minute() >= TimeZ4__STmin && minute() <= TimeZ4__SPmin)
    {
      TimeZone4 = 1;
      Blynk.virtualWrite(V51, 0);
      Blynk.virtualWrite(V52, 0);
      Blynk.virtualWrite(V53, 0);
      Blynk.virtualWrite(V54, 1);
    }
  } else
  {
    Blynk.virtualWrite(V51, 0);
    Blynk.virtualWrite(V52, 0);
    Blynk.virtualWrite(V53, 0);
    Blynk.virtualWrite(V54, 0);
  }


}

Here is modified code

void TimeZone1_Function()
{

  if (TimeZone1 == 1)
  {
    // call with timer every 30 seconds or so
    // Get RTC time
    sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
    Serial.print("Current Time: ");
    Serial.println(currentTime);

    // Get Time Input Widget time
    sprintf(startTime, "%02d:%02d:%02d", SThour, STmin, STsec);
    Serial.print("Start Time: ");
    Serial.println(startTime);

    sprintf(stopTime, "%02d:%02d:%02d", SPhour, SPmin, SPsec);
    Serial.print("Stop Time: ");
    Serial.println(stopTime);

    if (hour() >= SThour &&  hour() <= SPhour)
    {
      if (minute() >= STmin && minute() <= SPmin)
      {
        Serial.println("RELAY IS ON");
        Serial.println("..........................");
        digitalWrite(D5, HIGH); // Turn ON built-in LED
        led22.on();
      }
      else
      {
        Serial.println("RELAY IS OFF"); Serial.println("..........................");
        digitalWrite(D5, LOW); // Turn ON built-in LED
        led22.off();
        
      }
    }

    else
    {
      led22.off(); digitalWrite(D5, LOW); 
        Blynk.virtualWrite(V51, 0);
    }

  }

}

Serial output

The code will work fine if time between 1 hour range if its beyond 1 hours . The code wont work
Here is example.

Time Zone1 is selected
Current Time: 09:02:55
Start Time: 08:40:00
Stop Time: 10:40:00
RELAY IS OFF

AUTO mode is selected
Current Time: 09:03:06
Current Time: 09:03:07
Current Time: 09:03:08
Current Time: 09:03:09
Current Time: 09:03:10
Current Time: 09:03:11
Current Time: 09:03:12
Current Time: 09:03:13
Current Time: 09:03:14
Current Time: 09:03:15
Current Time: 09:03:16
Current Time: 09:03:17
Current Time: 09:03:18
Current Time: 09:03:19
Current Time: 09:03:20
Current Time: 09:03:21
Current Time: 09:03:22
Current Time: 09:03:23
Current Time: 09:03:24
Current Time: 09:03:25
Current Time: 09:03:26
Current Time: 09:03:27
Current Time: 09:03:28
Current Time: 09:03:29
Current Time: 09:03:30
Current Time: 09:03:31
Current Time: 09:03:32
Current Time: 09:03:33
Current Time: 09:03:34
Current Time: 09:03:35
Current Time: 09:03:36
Current Time: 09:03:37
Current Time: 09:03:38
Current Time: 09:03:39
Current Time: 09:03:40
Current Time: 09:03:41
Current Time: 09:03:42
Current Time: 09:03:43
Current Time: 09:03:44
Current Time: 09:03:45
Start Time: 09:03:00
Stop Time: 09:59:00
RELAY IS ON