LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Contolling two switches with each other , i.e one switch controls the other.

My boss has given me an extremely difficult task which I have been trying to crack since yesterday afternoon. No luck till now. Here is the task. There are 2 switches that write to a global boolean variable. And this gobal boolean variable is then connected to an LED output, so that we can control the LED output by using these switches. Now here is it how it goes. There is first a main VI that has an event structure with 3 events one is timeout, second is value change in switch 1 and third is stop the while loop. So as the name suggest the second event is where the switch 1 writes the value to the boolean global variable. In timeout the boolean global variable writes its value to the led output. Now in timeout also a subvi is placed on it. The subvi has switch 2 that also writes to the boolean. Now when I run the program and turn on switch one led is not turning on. I know why it is not turning on. Its because as switch one writes to the boolean making it on and boolean turns on led one, then subvi in the timeout will also run where switch 2 will also write to boolean that will make it off and boolean will turn off the led. I also know how to solve this problem. Make switch 2 as an input of the sub vi using connector pane and then connect switch1 to the subvi and then to the boolean, this way it will work. Simple. But my boss wants something more, he needs to control one switch with other. By connecting subvi with switch 2 in its connecter input pane and connecting this subvi to switch one, I know I am able to control switch 2 with switch one. But the reverse is not possible. I cannot turn on switch one by turning on switch 2. That is what my boss requires. I am currently trying to learn data value reference and user events to some how do this. But I want to know am i doing the right thing. Or is there a way to do this. I have also attached the VIs named main vi, sub vi and global boolean. 

Download All
0 Kudos
Message 1 of 9
(3,742 Views)

Honestly, you need a clearer problem definition and criteria for success.  You gave us a long description but a lot of it entangles your *requirements* with the *implementation* you've tried without success.

 

Deep breath and take a step back.   We're gonna approach this methodically by defining a logic truth table.  While I don't completely follow your requirements, I'm supposing they can all be handled by (1) establishing an initial switch state and then (2) responding appropriately to every switch transition.

 

You can show this in an 8-row truth table.  One column each for the logic *state* of switch 1 and switch 2.  One column each for the the transition occurring on switch 1 and switch 2.  (Note: we'll disallow simultaneous transitions since an event structure won't ever see simultaneous transitions. 

    For a given state of switches 1 & 2, either switch has 1 possible transition.  So 4 possible states and 2 possible transitions per state make 8 rows all together.

 

The last 2 columns for your truth table are your outputs -- the resulting desired states for switch 1 and switch 2.  Once you can define your *requirements* clearly like this, the code will be much more straightforward to write, test, and verify.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 2 of 9
(3,712 Views)

Honestly, this sounds like a Disaster Waiting to Happen, with all kinds of possibilities for Race Conditions, Variables not reflecting "Reality", etc.  Do not put this logic in anything critical (like a Nuclear Reactor, a Steel Smelter, etc.).

  • Switches write to Global Variable.  So one can turn it on, the other turn it off.
  • Global is read some unknown time later and value shown in LED.  Why not skip the Global and write directly to the LED when the switch is changed, eliminating the TimeOut and variable delay?
  • Why are there two controls for this LED?  Does one take precedence over the other?  Is there a need to know which one turned the LED on?
    • You can actually program the LED to tell if Button 1 or Button 2 turned it on!  Let's say the LED is a "Green" LED.  Wire Button 1 so "On" turns on the LED and makes sure its "On" color is Green.  Wire Button 2 so "On" turn on the LED and changes its button color to Red.  If the LED is glowing Green, you know Button 1 turned it on, but glowing Red means Button 2 was pushed.  I've used this feature to make Status Buttons, Green = Good, Yellow = Warning, Red = Error, and Black = "System turned off".

Bob Schor

 

0 Kudos
Message 3 of 9
(3,703 Views)

Forget about all this. I understand that this is not possible to do. All I want to do now is programmatically control a boolean control switch. Is that possible. When I turn one switch on, the other must also turn on automatically. I dont need it both ways. Just one main switch and one sub switch. I tried wiring the main switch to the property node of sub switch, the property being the value. But when I turn on main switch, the second switch turns on for a sec and goes back. I guess it is going back to its default value. So I need to change every time its default value. I am thinking that may also not be possible. But if it is, please let me know

 

0 Kudos
Message 4 of 9
(3,683 Views)

Even the above is not important. I dont need to control one switch with another. There is no need for that. But I will need help in using two switches to control a boolean led output. Main VI and SubVI. SubVI is in the main VI timeout event of event structure. The two switches, one in main vi and other in sub vi. Also LED in Main VI. I am able to control led with switch one in MAIN VI. But when I try to turn on switch 2, switch turn on for a sec and automatically turns off. So all I need now is control led in main vi using switch 2 in sub vi. If the question is not clear, i will upload the code i am doing now. Let me try something and will upload that if nothing is working. If anyone can help me, it will be quite helpful. Thanking You. 

0 Kudos
Message 5 of 9
(3,678 Views)

I have attached the project which I explained above. I was able to complete the task. I am able to control the led with both switches. But my switch 2 only works as a push button. I can permanently turn on switch 2 and off. Its not because of the mechanical action. Its because as soon as switch 2 is turned on, it turns back to off position automatically. If there is any way that problem can be solved, it will be helpful. 

0 Kudos
Message 6 of 9
(3,675 Views)

That is certainly the strangest bit of Event-handling Code I've ever seen!  The sub-VI has a "naked" Event structure, not in a loop, and is called over and over, basically handling the same Event "from scratch".  So "You push the second button down, the Code it goes round and round, Oh oh oh oh, oh oh, And it comes out Here" (this is a paraphrase of a favorite song from my childhood, about a trumpet -- replace "button" with "valve", "Code" with "music").  On one call, the button will merit a "Mouse Down" Event, but the next "fresh" call, it starts all over, and the Mouse is already Down, so it can't "go down" again, and hence it registers an "Up" appearance (or at least that's how I explain it).

 

If you really want this to work, I'd suggest making the sub-VI have a real Event Loop, and have the Event "send a message" (via a Queue, or even just by way of the Global) to the main routine.

 

Note I haven't tested this -- the entire design seems "broken" to me, very difficult to understand what is going on, rationale isn't clear, and someone else "looking at the code" (after you are no longer "in charge" of this code, or maybe after 3 months have elapsed, whichever comes first) will not have a clue what this is supposed to do, let alone extend or maintain it.

 

Bob Schor

0 Kudos
Message 7 of 9
(3,656 Views)

@govindsankar wrote:

Forget about all this. I understand that this is not possible to do. All I want to do now is programmatically control a boolean control switch. Is that possible. When I turn one switch on, the other must also turn on automatically. I dont need it both ways. Just one main switch and one sub switch. I tried wiring the main switch to the property node of sub switch, the property being the value. But when I turn on main switch, the second switch turns on for a sec and goes back. I guess it is going back to its default value. So I need to change every time its default value. I am thinking that may also not be possible. But if it is, please let me know

 


It is possible to achieve just about anything in code. Have you taken the earlier suggestion to create a truth table defining how you actually want the system to behave. From that you can write code which would satifise those requirements. Based on the code you have been posting I highly recommend that you spend some time going through the LabVIEW training.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 8 of 9
(3,653 Views)

@govindsankar wrote:

Forget about all this. I understand that this is not possible to do. All I want to do now is programmatically control a boolean control switch. Is that possible. When I turn one switch on, the other must also turn on automatically. I dont need it both ways. Just one main switch and one sub switch. I tried wiring the main switch to the property node of sub switch, the property being the value. But when I turn on main switch, the second switch turns on for a sec and goes back. I guess it is going back to its default value. So I need to change every time its default value. I am thinking that may also not be possible. But if it is, please let me know. 


I've explained this behavior in my previous post -- you have not learned how to properly use Event Loops (not "naked" Event Structures).  I agree with other comments that you need to spend as much time as possible (a minimum of 3-6 hours) with the LabVIEW Introductory Tutorials, learning as much as you can before you start throwing down Structures and Functions.

 

Bob Schor

0 Kudos
Message 9 of 9
(3,642 Views)