Looking for coding advice for multiple, long-press, buttons widgets

Hi all, can anyone suggest the best way to code up two button widgets one button for on (push mode) and the other button to turn off (push mode) and having both button requiring a long press?

@Costas, @Gunner …So i managed to solve my own issue here thanks to a couple of existing solved forum topics. Much deserved credit to @Jamin and @psoro (apologies if others assisted and I haven’t mentioned them)…I managed to put parts of their code together to solve my problem. Two button widgets (push mode) one for ON and the other for OFF both require a long press to activate. This may assist others trying to achieve a similar function.

I’m wondering if @psoro could explain the "void checklastbuttonpressed () " as it does what I need it to do but I don’t quite understand it.

   BLYNK_WRITE(V20){ //Timer code to require button to be pressed for 1 sec before activating
     
  if(param.asInt()){
           newTimer2 = timer.setTimeout(1000, deviceOnA);
        }
        else {
            timer.deleteTimer(newTimer2);
        }
               }
      
    BLYNK_WRITE(V21){       //Timer code to require button to be pressed for 1 sec secs before activating
        if(param.asInt()){
             newTimer3 = timer.setTimeout(1000, deviceOffA);
                       }
        else {
                        timer.deleteTimer(newTimer3);
            }
       }


    void checklastbuttonpressed (){
        if((deviceOn==1)&&(deviceOff==0)){ oldstatus=1; }
        if((deviceOn==0)&&(deviceOff==1)){ oldstatus=2; }
        
        }



    void deviceOnA() {  // ON-OFF Manual 

             if (digitalRead(btnPin)==HIGH){
                 checklastbuttonpressed ();
             if (deviceOn==0){
                       
             digitalWrite(btnPin, LOW); // set LED ON
                  }
             
              else {            
                 digitalWrite(btnPin, HIGH); // set LED ON 
                  }
        }
         
        }

    void deviceOffA() { // ON-OFF Manual 

        if (digitalRead(btnPin)==LOW){
            checklastbuttonpressed ();
        if   (deviceOff==0){  
              digitalWrite(btnPin, HIGH); // set LED ON 
                  }
             
              else {            
                  digitalWrite(btnPin, LOW); // set LED ON
                    }
          
    }
    }
1 Like

Now that’s what we like to see :+1:

You are only showing a snippet of your code, but as near as I can tell it was a way to see which of the two buttons was last presed and setting a flag acordingly… However, while you are calling the void, you are not doing anything with the flag result… thus, in your example, it is just extraneous code,

Hi @GG07, as @Gunner stated, with this piece of code, the variable “oldstatus” changes depending on last mode selected before click on “Manual/Auto” button to turn on/off the light manually.
This way, if I press the “Manual/Auto” button again to change to “Auto”, the function “restorelastbuttonpressed” remember your last choice “Monday-Friday”, “Saturday-Sunday”, “All days” or “Uptoyou” mode and fix the buttons accordingly

Please, check the full code cause only the part you are showing is not doing all the process.

HTH