Trying to add a physical push button for ON/OFF

Hello !

I am trying to give a light tweak to this project by adding a physical push button for ON/OFF which is
V2 virtual pin in the code below. I am using the blynk sync state push button code and trying to integrate with this. But am not able to get it work. I clearly understand this is not Blynk problem or no one is here to help anyone. But i wanted to know whether my idea will even work or am going on the right path. Please guide me or please give a piece of code which i can inject and get it working…:neutral_face:


BLYNK_WRITE(V2)  // ON-OFF Manual 
{
    if (param.asInt()==1) {  // boton encendido  
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("Manual MODE is ON");
    terminal.println("Press ON/OFF button if required");
    terminal.println("Device is ON");
    terminal.println();
    terminal.println();
    terminal.flush();
    
        if (manual==0){  //estĂĄ en modo automĂĄtico     
         checklastbuttonpressed ();
         manual=1;
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED, HIGH); // set LED ON 
         Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget
         
         } else {             //estĂĄ en modo manual 
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED, HIGH); // set LED ON 
         Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget
         }                           
    }else {
      
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("Manual MODE is ON");
    terminal.println("Press ON/OFF button if required");
    terminal.println("Device is OFF");
    terminal.println();
    terminal.println();
    terminal.flush();

        if (manual==0){      //modo automĂĄtico
         checklastbuttonpressed ();
         manual=1;
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED, LOW); // set LED OFF
         Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
         } else {  
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;   
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED, LOW); // set LED OFF
         Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
         }  
         }
}


That doesn’t make much sense!
If you’re using a physical button as opposed to a widget in the app then that physical button needs to be connected to one of the GPIO pins on your MCU.
The other side of the physical button will be connected to either Ground or 3.3/5v (Vcc) depending on your device, and your preference as to whether you want to pull the pin LOW or HIGH when the button is pressed.

You’ll need to declare that GPIO as an input with a pinmode statement and either monitor it for a change of state or attach an interrupt to it.
I’d also suggest using some denounce code on the physical button, as most switches don’t give a clean transition from off to on, you frequently get ‘noise’ that needs to be cleaned-up to get a consistent switch operation.

Also, posting snippets of code, and no details of the hardware that you’re using makes it difficult to help point you in the right direction.

Pete.

Make yourself a timed function to detect a physical button state 0/1
http://docs.blynk.cc/#blynk-firmware-blynktimer
https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/

Following the debouncing suggestion as mentioned by @PeteKnight
https://www.google.com/search?q=arduino+button+debounce

Either use a switch or add in some latching code to toggle between 0 & 1 each press - I will leave that for you to Google. Then use it to send a matching 1/0 state to V2
http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynkvirtualwritevpin-value

And follow that with a sync command to act as if the App button was pressed instead
http://docs.blynk.cc/#blynk-firmware-blynktimer-blynksyncvirtualvpin

PS, I changed your title to something a bit more meaningful :stuck_out_tongue:

sorry for the incomplete info.
Yes i have attached a Push Button to Pin D1 and the relay out to pin D2 , And am using node mcu and also defined them in the code

const int TacSwitch1 = D1;
const int TestLED11 = D2;
void checkPhysicalButton();
int ledState = LOW;
int btnState = HIGH;
void setup()
{
  pinMode(TestLED1, OUTPUT);          // Pin D2 P5 on board
  pinMode(TacSwitch1, INPUT_PULLUP);  // Pin D1 P1 on board
  
  digitalWrite(TestLED1, LOW); // set LED OFF
  digitalWrite(TacSwitch1, ledState);
 

this is just a few lines of the code so state that push button is defined.
later what i have done is in the setup part,

void checkPhysicalButton()
{
  if (digitalRead(TestLED1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(TacSwitch1, ledState);

      // Update Button Widget
      Blynk.virtualWrite(V2, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

BLYNK_WRITE(V2)  // ON-OFF Manual 
{
    if (ledState = param.asInt()) {              //// here am missing out 
       
    //(param.asInt()==1)   // boton encendido   //////// this was how it was previously done
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("Manual MODE is ON");
    terminal.println("Press ON/OFF button if required");
    terminal.println("Device is ON");
    terminal.println();
    terminal.println();
    terminal.flush();
    digitalWrite(TacSwitch1, ledState);
    
        if (manual==0){  //estĂĄ en modo automĂĄtico     
         checklastbuttonpressed ();
         manual=1;
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED1, HIGH); // set LED ON 
         Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget
         
         } else {             //estĂĄ en modo manual 
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED1, HIGH); // set LED ON 
         Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget
         }                           
    }else {
      
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("Manual MODE is ON");
    terminal.println("Press ON/OFF button if required");
    terminal.println("Device is OFF");
    terminal.println();
    terminal.println();
    terminal.flush();

        if (manual==0){      //modo automĂĄtico
         checklastbuttonpressed ();
         manual=1;
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED1, LOW); // set LED OFF
         Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
         } else {  
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;   
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED1, LOW); // set LED OFF
         Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
         }  
         }
}

hardware part is completely working fine. The only problem is the code part.

and posting the complete edited code is not a problem. but it goes way too long. above i have mentioned the bits of code that i have injected to the @psoro code.
And below i will post the whole code .

/**************************************************************
 * timeinput.ino Demonstrate interaction of Time library with
 * Blynk's TimeInput widget.
 * App project setup:
 * RTC widget (no pin required!!!)
 * V1 : Manual/Auto button
 * V2: On-off button
 * Terminal on V3 // Label will be the clock + wifi signal!!!!
 * 
 * Time Input widget on V4 (Monday-Friday)
 * Button selection for Time Input (Monday-Friday) on V5
 * 
 * Time Input widget on V6 (Saturday-Sunday)
 * Button selection for Time Input (Saturday-Sunday on V7
 * 
 * Time Input widget on V8 (All days)
 * Button selection for Time Input (All days) on V9
 * 
 * Time Input widget on V10 (Up to you)
 * Button selection for Time Input (Up to you) on V11
 * 
 **************************************************************/

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>


SimpleTimer timer;

WidgetRTC rtc;
WidgetTerminal terminal(V3);

#define server "192.168.1.xxx"   // or "blynk.cloud-com" for Blynk's cloud server
#define TestLED1 4                 //  pin assignment  D2
char Date[16];
char Time[16];
char auth[] = "xxxxxxxxxxxxxx";
char ssid[] = "xxx";
char pass[] = "xxx";
long startsecondswd;            // weekday start time in seconds
long stopsecondswd;             // weekday stop  time in seconds
long nowseconds;                // time now in seconds
bool isFirstConnect = true;


String displaycurrenttimepluswifi;
int wifisignal;
int manual=0;
int oldstatus;

int mondayfriday;
int saturdaysunday;
int alldays;
int uptoyou;

const int TacSwitch1 = D1; //////////
const int TestLED11 = D2;   ////////

void checkPhysicalButton();

int ledState = LOW;
int btnState = HIGH;



void setup()
{
  pinMode(TestLED1, OUTPUT);          // Pin D2 P5
  pinMode(TacSwitch1, INPUT_PULLUP);  // Pin D1 P1 
  
  digitalWrite(TestLED1, LOW); // set LED OFF
  digitalWrite(TacSwitch1, ledState);

  Serial.begin(115200);
  Serial.println("\Starting");
 // Blynk.begin(auth, ssid, pass, server);
  Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,xxx), 8080);
  timer.setInterval(100L, checkPhysicalButton);
  int mytimeout = millis() / 1000;
  while (Blynk.connect() == false) { // try to connect to server for 10 seconds
    if((millis() / 1000) > mytimeout + 8){ // try local server if not connected within 9 seconds
       break;
    }
  }
  rtc.begin();
  timer.setInterval(10000L, activetoday);  // check every 10 SECONDS if schedule should run today 
  timer.setInterval(30000L, reconnectBlynk);  // check every 30s if still connected to server 
  timer.setInterval(5000L, clockvalue);  // check value for time
  timer.setInterval(5000L, sendWifi);    // Wi-Fi singal
}


BLYNK_CONNECTED() {
if (isFirstConnect) {
  Blynk.syncAll();
  Blynk.notify("TIMER STARTING!!!!");
isFirstConnect = false;
}
}


void sendWifi() {
  wifisignal = map(WiFi.RSSI(), -105, -40, 0, 100);
}

void clockvalue() // Digital clock display of the time
{

 int gmthour = hour();
  if (gmthour == 24){
     gmthour = 0;
  }
  String displayhour =   String(gmthour, DEC);
  int hourdigits = displayhour.length();
  if(hourdigits == 1){
    displayhour = "0" + displayhour;
  }
  String displayminute = String(minute(), DEC);
  int minutedigits = displayminute.length();  
  if(minutedigits == 1){
    displayminute = "0" + displayminute;
  }  

  displaycurrenttimepluswifi = "                                          Clock:  " + displayhour + ":" + displayminute + "               Signal:  " + wifisignal +" %";
  Blynk.setProperty(V3, "label", displaycurrenttimepluswifi);
  
}



void activetoday(){        // check if schedule should run today
  if(year() != 1970){

   if (mondayfriday==1) {  
    Blynk.syncVirtual(V4); // sync timeinput widget  
   }
   if (saturdaysunday==1) { 
    Blynk.syncVirtual(V6); // sync timeinput widget  
   }
   if (alldays==1) { 
    Blynk.syncVirtual(V8); // sync timeinput widget  
   }
   if (uptoyou==1) { 
    Blynk.syncVirtual(V10); // sync timeinput widget  
   }
  }
}

void checklastbuttonpressed (){
    if((mondayfriday==1)&&(saturdaysunday==0)){ oldstatus=1; }
    if((mondayfriday==0)&&(saturdaysunday==1)){ oldstatus=2; }
    if((mondayfriday==1)&&(saturdaysunday==1)){ oldstatus=3; }
    if(alldays==1){ oldstatus=4; }
    if(uptoyou==1){ oldstatus=5; }
    if((mondayfriday==0)&&(saturdaysunday==0)&&(alldays==0)&&(uptoyou==0)){ oldstatus=6; }  
}


void restorelastbuttonpressed (){
    if(oldstatus==1){ mondayfriday=1; Blynk.virtualWrite(V5, 1); }
    if(oldstatus==2){ saturdaysunday=1 ; Blynk.virtualWrite(V7, 1); }
    if(oldstatus==3){ saturdaysunday=1; mondayfriday=1;Blynk.virtualWrite(V5, 1);Blynk.virtualWrite(V7, 1); }
    if(oldstatus==4){ alldays=1; Blynk.virtualWrite(V9, 1);}
    if(oldstatus==5){ uptoyou=1; Blynk.virtualWrite(V11, 1);}   
    if(oldstatus==6){ 
      mondayfriday=0; 
      saturdaysunday=0;
      alldays=0;
      uptoyou=0;
      Blynk.virtualWrite(V5, 0);
      Blynk.virtualWrite(V7, 0);
      Blynk.virtualWrite(V9, 0);
      Blynk.virtualWrite(V11, 0);
      }
 }


BLYNK_WRITE(V1)  // Manual/Auto selection
{
  if (param.asInt()==1) {
    manual=1;
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("Manual MODE is ON");
    terminal.println("Press ON/OFF button if required");
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.flush();

    checklastbuttonpressed ();
  
    alldays=0;
    uptoyou=0;
    mondayfriday=0;
    saturdaysunday=0;
    Blynk.virtualWrite(V5, 0);
    Blynk.virtualWrite(V7, 0);
    Blynk.virtualWrite(V9, 0);
    Blynk.virtualWrite(V11, 0);
    
  } else {
    restorelastbuttonpressed ();
    manual=0;
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("Manual MODE is OFF");
    terminal.println("Auto MODE restored from last status");
    terminal.println("Wait for update (10 seconds as maximum)");
    terminal.println();
    terminal.println();
    terminal.flush();
}
}

void resetTerminal()
{
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("New MODE has been selected");
    terminal.println("Wait for update (10 seconds as maximum)");
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.flush();
}

void resetManual()
{
Blynk.virtualWrite(V1, 0);   //Turn OFF Manual Mode Widget
Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget Device
digitalWrite(TestLED1, LOW); // set LED OFF
}

void checkPhysicalButton()
{
  if (digitalRead(TestLED1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(TacSwitch1, ledState);

      // Update Button Widget
      Blynk.virtualWrite(V2, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}


BLYNK_WRITE(V2)  // ON-OFF Manual 
{
    if (ledState = param.asInt()) {
       
    //(param.asInt()==1)   // boton encendido  
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("Manual MODE is ON");
    terminal.println("Press ON/OFF button if required");
    terminal.println("Device is ON");
    terminal.println();
    terminal.println();
    terminal.flush();
    digitalWrite(TacSwitch1, ledState);
    
        if (manual==0){  //estĂĄ en modo automĂĄtico     
         checklastbuttonpressed ();
         manual=1;
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED1, HIGH); // set LED ON 
         Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget
         
         } else {             //estĂĄ en modo manual 
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED1, HIGH); // set LED ON 
         Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget
         }                           
    }else {
      
    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("Manual MODE is ON");
    terminal.println("Press ON/OFF button if required");
    terminal.println("Device is OFF");
    terminal.println();
    terminal.println();
    terminal.flush();

        if (manual==0){      //modo automĂĄtico
         checklastbuttonpressed ();
         manual=1;
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED1, LOW); // set LED OFF
         Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
         } else {  
         mondayfriday=0;
         saturdaysunday=0;
         alldays=0;
         uptoyou=0;   
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V5, 0);
         Blynk.virtualWrite(V7, 0);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED1, LOW); // set LED OFF
         Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
         }  
         }
}

BLYNK_WRITE(V5)  // Monday-Friday selected
{
  if (param.asInt()==1 && (V1==1)) {
    timer.setTimeout(50, resetTerminal);
    timer.setTimeout(50, resetManual);
    timer.setTimeout(50, checklastbuttonpressed);
    mondayfriday=1;
    alldays=0;
    uptoyou=0;
    Blynk.virtualWrite(V9, 0);
    Blynk.virtualWrite(V11, 0);
  } else {
    mondayfriday=0;
}
}


BLYNK_WRITE(V7)  // Saturday-Sunday selected
{
  if (param.asInt()==1) {
    timer.setTimeout(50, resetTerminal);
    timer.setTimeout(50, resetManual);
    timer.setTimeout(50, checklastbuttonpressed);
    saturdaysunday=1;
    alldays=0;
    uptoyou=0;
    Blynk.virtualWrite(V9, 0);
    Blynk.virtualWrite(V11, 0);
  } else {
    saturdaysunday=0;
}
}

BLYNK_WRITE(V9)  // All days selected
{
  if (param.asInt()==1) {
    timer.setTimeout(50, resetTerminal);
    timer.setTimeout(50, resetManual);
    timer.setTimeout(50, checklastbuttonpressed);
    alldays=1;
    mondayfriday=0;
    saturdaysunday=0;
    uptoyou=0; 
    Blynk.virtualWrite(V5, 0);
    Blynk.virtualWrite(V7, 0);
    Blynk.virtualWrite(V11, 0);
  } else {
    alldays=0;
}
}

BLYNK_WRITE(V11)  // Up to you selected
{
  if (param.asInt()==1) {
    timer.setTimeout(50, resetTerminal);
    timer.setTimeout(50, resetManual);
    timer.setTimeout(50, checklastbuttonpressed);
    uptoyou=1;
    mondayfriday=0;
    saturdaysunday=0;
    alldays=0;
    Blynk.virtualWrite(V5, 0);
    Blynk.virtualWrite(V7, 0);
    Blynk.virtualWrite(V9, 0);
  } else {
    uptoyou=0;
}
}

BLYNK_WRITE(V4)//Monday-Friday
{  
  if (mondayfriday==1) {         
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  
    TimeInputParam t(param);
  
    terminal.print("M-F Checked schedule at: ");
    terminal.println(Time);
    terminal.flush();
    int dayadjustment = -1;  
    if(weekday() == 1){
      dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }
    if(t.isWeekdaySelected(weekday() + dayadjustment)){ //Time library starts week on Sunday, Blynk on Monday
    terminal.println("Monday-Friday ACTIVE today");
    terminal.flush();
    if (t.hasStartTime()) // Process start time
    {
      terminal.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
    }
    if (t.hasStopTime()) // Process stop time
    {
      terminal.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
    }
    // Display timezone details, for information purposes only 
    terminal.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time 
  //  terminal.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
    terminal.flush();
  
     for (int i = 1; i <= 7; i++) {  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
        if (t.isWeekdaySelected(i)) {
        terminal.println(String("Day ") + i + " is selected");
        terminal.flush();
        }
      } 
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    //Serial.println(startsecondswd);  // used for debugging
    if(nowseconds >= startsecondswd){    
      terminal.print("Monday-Friday STARTED at");
      terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
      if(nowseconds <= startsecondswd + 90){    // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED1, HIGH); // set LED ON
        Blynk.virtualWrite(V2, 1);
        // code here to switch the relay ON
      }      
    }
    else{
      terminal.println("Monday-Friday Device NOT STARTED today");
      terminal.flush();
   
    }
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    //Serial.println(stopsecondswd);  // used for debugging
    if(nowseconds >= stopsecondswd){
      digitalWrite(TestLED1, LOW); // set LED OFF
      Blynk.virtualWrite(V2, 0);
      terminal.print("Monday-Friday STOPPED at");
      terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
      if(nowseconds <= stopsecondswd + 90){   // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED1, LOW); // set LED OFF
        Blynk.virtualWrite(V2, 0);
        // code here to switch the relay OFF
      }              
    }
    else{
      if(nowseconds >= startsecondswd){  
        digitalWrite(TestLED1, HIGH); // set LED ON    test
        Blynk.virtualWrite(V2, 1);
        terminal.println("Monday-Friday is ON");
        terminal.flush();
      
      }          
    }
  }
  else{
    terminal.println("Monday-Friday INACTIVE today");
    terminal.flush();
    // nothing to do today, check again in 30 SECONDS time    
  }
  terminal.println();
}
}

 BLYNK_WRITE(V6) //Saturday-Sunday
 {  
  if (saturdaysunday==1) { 
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  
    TimeInputParam t(param);
  
    terminal.print("S-S Checked schedule at: ");
    terminal.println(Time);
    terminal.flush();
    int dayadjustment = -1;  
    if(weekday() == 1){
    dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
   }
    if(t.isWeekdaySelected(weekday() + dayadjustment)){ //Time library starts week on Sunday, Blynk on Monday
    terminal.println("Saturday-Sunday ACTIVE today");
    terminal.flush();
    if (t.hasStartTime()) // Process start time
    {
      terminal.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
    }
    if (t.hasStopTime()) // Process stop time
    {
      terminal.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
    }
    // Display timezone details, for information purposes only 
    terminal.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time 
   // terminal.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
    terminal.flush();
  
     for (int i = 1; i <= 7; i++) {  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
        if (t.isWeekdaySelected(i)) {
        terminal.println(String("Day ") + i + " is selected");
        terminal.flush();
        }
      } 
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    //Serial.println(startsecondswd);  // used for debugging
    if(nowseconds >= startsecondswd){    
      terminal.print("Saturday-Sunday STARTED at");
      terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
      if(nowseconds <= startsecondswd + 90){    // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED1, HIGH); // set LED ON
        Blynk.virtualWrite(V2, 1);
        // code here to switch the relay ON
      }      
    }
    else{
      terminal.println("Saturday-Sunday NOT STARTED today");
      terminal.flush();
     
    }
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    //Serial.println(stopsecondswd);  // used for debugging
    if(nowseconds >= stopsecondswd){
      digitalWrite(TestLED1, LOW); // set LED OFF
      Blynk.virtualWrite(V2, 0);
      terminal.print("Saturday-Sunday STOPPED at");
      terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
      if(nowseconds <= stopsecondswd + 90){   // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED1, LOW); // set LED OFF
        Blynk.virtualWrite(V2, 0);
        // code here to switch the relay OFF
      }              
    }
    else{
      if(nowseconds >= startsecondswd){  
        digitalWrite(TestLED1, HIGH); // set LED ON  TEST
        Blynk.virtualWrite(V2, 1);
        terminal.println("Saturday-Sunday is ON");
        terminal.flush();
      
      }          
    }
  }
  else{
    terminal.println("Saturday-Sunday INACTIVE today");
    terminal.flush();
    // nothing to do today, check again in 30 SECONDS time    
  }
  terminal.println();
}
}


BLYNK_WRITE(V8)//All days
{  
  if (alldays==1) {         
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  
    TimeInputParam t(param);
  
    terminal.print("All Days Checked schedule at: ");
    terminal.println(Time);
    terminal.flush();
    int dayadjustment = -1;  
    if(weekday() == 1){
      dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }
    if(t.isWeekdaySelected(weekday() + dayadjustment)){ //Time library starts week on Sunday, Blynk on Monday
    terminal.println("ALL DAYS ACTIVE today");
    terminal.flush();
    if (t.hasStartTime()) // Process start time
    {
      terminal.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
    }
    if (t.hasStopTime()) // Process stop time
    {
      terminal.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
    }
    // Display timezone details, for information purposes only 
    terminal.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time 
  //  terminal.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
    terminal.flush();
  
     for (int i = 1; i <= 7; i++) {  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
        if (t.isWeekdaySelected(i)) {
        terminal.println(String("Day ") + i + " is selected");
        terminal.flush();
        }
      } 
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    //Serial.println(startsecondswd);  // used for debugging
    if(nowseconds >= startsecondswd){    
      terminal.print("ALL DAYS STARTED at");
      terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
      if(nowseconds <= startsecondswd + 90){    // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED1, HIGH); // set LED ON
        Blynk.virtualWrite(V2, 1);
        // code here to switch the relay ON
      }      
    }
    else{
      terminal.println("All Day Device NOT STARTED today");
      terminal.flush();
        
    }
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    //Serial.println(stopsecondswd);  // used for debugging
    if(nowseconds >= stopsecondswd){
      digitalWrite(TestLED1, LOW); // set LED OFF
      Blynk.virtualWrite(V2, 0);
      terminal.print("All day STOPPED at");
      terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
      if(nowseconds <= stopsecondswd + 90){   // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED1, LOW); // set LED OFF
        Blynk.virtualWrite(V2, 0);
        // code here to switch the relay OFF
      }              
    }
    else{
      if(nowseconds >= startsecondswd){  
        digitalWrite(TestLED1, HIGH); // set LED ON  TEST!!!!!
        Blynk.virtualWrite(V2, 1);
        terminal.println("All day is ON");
        terminal.flush();
 
      }          
    }
  }
  else{
    terminal.println("All day INACTIVE today");
    terminal.flush();
    // nothing to do today, check again in 30 SECONDS time    
  }
  terminal.println();
}
}

BLYNK_WRITE(V10)//Up to you 
{  
  if (uptoyou==1) {         
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  
    TimeInputParam t(param);
  
    terminal.print("Up to you Checked schedule at: ");
    terminal.println(Time);
    terminal.flush();
    int dayadjustment = -1;  
    if(weekday() == 1){
      dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }
    if(t.isWeekdaySelected(weekday() + dayadjustment)){ //Time library starts week on Sunday, Blynk on Monday
    terminal.println("Up to you ACTIVE today");
    terminal.flush();
    if (t.hasStartTime()) // Process start time
    {
      terminal.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
    }
    if (t.hasStopTime()) // Process stop time
    {
      terminal.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
    }
    // Display timezone details, for information purposes only 
    terminal.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time 
    terminal.println("At least ONE day MUST be selected");
   // terminal.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
    terminal.flush();
  
     for (int i = 1; i <= 7; i++) {  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
        if (t.isWeekdaySelected(i)) {
        terminal.println(String("Day ") + i + " is selected");
        terminal.flush();
        }
      } 
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    //Serial.println(startsecondswd);  // used for debugging
    if(nowseconds >= startsecondswd){    
      terminal.print("Up to you STARTED at");
      terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
      terminal.flush();
      if(nowseconds <= startsecondswd + 90){    // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED1, HIGH); // set LED ON
        Blynk.virtualWrite(V2, 1);
        // code here to switch the relay ON
      }      
    }
    else{
      terminal.println("UP to you Device NOT STARTED today");
      terminal.flush();
         
    }
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    //Serial.println(stopsecondswd);  // used for debugging
    if(nowseconds >= stopsecondswd){
      digitalWrite(TestLED1, LOW); // set LED OFF
      Blynk.virtualWrite(V2, 0);
      terminal.print("Up to you STOPPED at");
      terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
      terminal.flush();
      if(nowseconds <= stopsecondswd + 90){   // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED1, LOW); // set LED OFF
        Blynk.virtualWrite(V2, 0);
        // code here to switch the relay OFF
      }              
    }
    else{
      if(nowseconds >= startsecondswd){  
        digitalWrite(TestLED1, HIGH); // set LED ON 
        Blynk.virtualWrite(V2, 1);
        terminal.println("Up to you is ON");
        terminal.flush();
       
      }          
    }
  }
  else{
    terminal.println("Up to you INACTIVE today");
    terminal.flush();
    // nothing to do today, check again in 30 SECONDS time    
  }
  terminal.println();
}
}

void reconnectBlynk() {
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
     BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}

void loop()
{

  if (Blynk.connected()) {
    Blynk.run();
  }
  timer.run();
}

triggering the V2 that is the on/off button widget will bring the program into manual mode and work untill we choose auto in the Blynk app. Thats the end game. Because physical button comes in handy.

You’re declaring the checkPhysicalButton function here, in your declaration section, and later as a proper function.

Pete.

Actually i got this working in the following way
Lets assume the program is running in manual mode
Then when ever we press the physical button it will work without a problem.
But when the program is running in say mon to fri or sat to sun or anyother auto mode
And then when when i turn on the with the physical button then it will turn back off after 10 sec .

Can you please give me some clue how to get this right. I know we cannot ask any help here. But after googling and some research am asking for help here. And btw am not having any background from software part. Am a tool and die maker. :sweat_smile:

@Madhukesh I don’t remember now how much was written by @psoro , by you and by me but here goes.

The sketch has an integer flag called manual (0 off and 1 on) .
If you incorporate this into the checkPhysicalButton() function you could then just change the first line of the activetoday() function from:

if(year() != 1970){

to:

if((year() != 1970) && (manual == 0)){ // i.e. NOT in manual mode

1 Like

Keep the questions to the forum.
manual 0 or 1 goes in physical button section and the existing if(year… line in the sketch is changed to the new version.

@Costas Am not saying that he owns the code or he alone has done with the code. I know that you have also contributed to the code. My intention was to say that which code i am about to edit and add some extra feature or something new. Thats it…

coming to the new version of

this will keep the terminal in the app blank. will not give any sought of update about the mode selected.
I will try something else. If i get that working i will update here.
Thank you for the idea.

Neither was I. I was simply saying I can’t remember too much about the code and I’m not going to spend hours looking through it, that’s your job.

Not sure what the terminal is. I did think afterwards that using the “manual” variable is perhaps not a good idea as it might interfere with other parts of the sketch. Perhaps use a variable of “manualButton” in checkPhysicalButton() function and the if(year....)

yeah i know…

i will try out. Thank you.

without looking through all of the code/links, here are a few things I spotted wrong with your posted code snipets.

In void checkPhysicalButton()

  if (digitalRead(TestLED1) == LOW) 

should probably be

 if (digitalRead(TacSwitch1) == LOW)

as this is the actual button/Input.

Also,

digitalWrite(TacSwitch1, ledState);

should probably be

digitalWrite(TestLED1, ledState);

as this is the output/led/relay

Additionally, void checkPhysicalButton() will not make the BLYNK_WRITE(V2) function run. You would need to add Blynk.syncVirtual(V2);

I would also change the if statement BLYNK_WRITE(V2) function back to how it was (as mentioned in your comments in the code).

void checkPhysicalButton()
{
  if (digitalRead(TacSwitch1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(TestLED1, ledState);

      // Update Button Widget
      Blynk.virtualWrite(V2, ledState);
      Blynk.syncVirtual(V2);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

BLYNK_WRITE(V2)  // ON-OFF Manual 
{
    if (param.asInt()==1) {              
       
    

Yeah i tried that out earlier. But it doesnt come in sync with the push button.
Lets say in app we turn on and later when we try to switch it off with push button, it takes 2 clicks to turn it off. But there is no sync issue on the app. Which ever the earlier state was in the push button ,the app works fine.

And is there any way or widget , that lets us control a gadget at a particular time in a day, and twice or more in the same day ? Ex. Mon - 10.00 Am to 11.00 Am and again 4.00 Pm to 5.00 Pm ?