Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. Confused myself.... Two toggle inputs to toggle one output state... ideas please...

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

Scheduled Pinned Locked Moved My Project
logic
3 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • greglG Offline
    greglG Offline
    gregl
    Hero Member
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • AnticimexA Offline
      AnticimexA Offline
      Anticimex
      Contest Winner
      wrote on last edited by Anticimex
      #2

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

      Do you feel secure today? No? Start requiring some signatures and feel better tomorrow ;)

      1 Reply Last reply
      1
      • greglG Offline
        greglG Offline
        gregl
        Hero Member
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        21

        Online

        11.7k

        Users

        11.2k

        Topics

        113.1k

        Posts


        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • MySensors
        • OpenHardware.io
        • Categories
        • Recent
        • Tags
        • Popular