Confused myself.... Two toggle inputs to toggle one output state... ideas please...


  • Hero Member

    Hey everyone,

    I have a sketch where i have two inputs and i need to set one output... but im getting lost in the logic.

    One "input" is a V_light ON/OFF from Vera - im treating the data as boolean.
    the second input is a push button which toggles another boolean state.

    the Output is a boolean state "IsRemoteOnOff()"

    The logic i need is that either input can toggle the output.

    Here is the code:

    boolean IsRemoteOnOff() {
    	
    if (lastToggleVeraRemote != toggle_vera_remote || lastToggleRemoteBtn != toggle_remote_btn) { //the Vera state has been changed or the button has been pressed.
    		
    		if (lastToggleVeraRemote != toggle_vera_remote){
    			lastToggleVeraRemote = toggle_vera_remote ;	//if it was the Vera remote that changed then update the status for the vera remote
    		}
    		if (lastToggleRemoteBtn != toggle_remote_btn){  //if it was the pushbutton then update the status of the 
    			lastToggleRemoteBtn = toggle_remote_btn ;
    		} 			
    		
    	return true;  //remote is active
    	}
    else {
    	return false; //remote is not active
    }
    }
    

    The problem is that the state is nearly immediately switched back
    Can anyone see what im doing wrong? Im sure its something really simple..but my brain is smoked and its just not coming to me!


  • Contest Winner

    I think you should separate the logic as one if-case to manage the lastToggle envent and a new one for reading the values.
    Your code as it looks now, first requires the toggle value to be changed, then if the toggle value is changed, yo set the lastToggle variable to the current value, thus immediately makes all conditions to the first if-case false, so that after the first if-case has been true once, it will the next time, by definition, be false, so the return value will be false after each call it has returned true on.
    Your comment suggests that "or the button has been pressed." But the if case actually does not distinguish from the button being pressed or not, it just triggers if the state has changed, so it is true for the button being released as well.

    Simple example:

    boolean lastToggleVeraRemote, lastToggleRemoteBtn;
    boolean IsRemoteOnOff()
    {
        updateToggles();
    
        if (lastToggleVeraRemote || lastToggleRemoteBtn)
        {
            return true;  //remote is active
        }
        else
        {
            return false; //remote is not active
        }
    }
    

    If you for some reason need to have logic to update the LastToggle variables on change, it is a different thing than determine if the toggles are 'active' and in my opinion they should be handled "outside" this logic.

    boolean lastToggleVeraRemote, lastToggleRemoteBtn, toggle_vera_remote, toggle_remote_btn;
    boolean updateRemotes()
    {
        updateToggles();
    
        if (lastToggleVeraRemote != toggle_vera_remote)
        {  //if it was the Vera remote that changed then update the status for the vera remote
            lastToggleVeraRemote = toggle_vera_remote ;    
        }
        if (lastToggleRemoteBtn != toggle_remote_btn)
        {   //if it was the pushbutton then update the status of the button
            lastToggleRemoteBtn = toggle_remote_btn ;
        }             
    }
    

    But of course they can be combine if you prefer.

    boolean lastToggleVeraRemote, lastToggleRemoteBtn, toggle_vera_remote, toggle_remote_btn;
    boolean IsRemoteOnOff()
    {
        updateToggles();
    
        if (lastToggleVeraRemote != toggle_vera_remote)
        {  //if it was the Vera remote that changed then update the status for the vera remote
            lastToggleVeraRemote = toggle_vera_remote ;    
        }
        if (lastToggleRemoteBtn != toggle_remote_btn)
        {   //if it was the pushbutton then update the status of the button
            lastToggleRemoteBtn = toggle_remote_btn ;
        }             
    
        if (lastToggleVeraRemote || lastToggleRemoteBtn)
        {
            return true;  //remote is active
        }
        else
        {
            return false; //remote is not active
        }
    }

  • Hero Member

    Thanks @Anticimex - Ill give this a try hopefully tonight. ( ive been away for a few days)


Log in to reply
 

Suggested Topics

  • 8
  • 2
  • 2
  • 29
  • 90
  • 2

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts