10-17-2018 04:15 AM
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.
10-17-2018 08:34 AM - edited 10-17-2018 08:35 AM
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
10-17-2018 09:54 AM
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.).
Bob Schor
10-18-2018 01:38 AM
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
10-18-2018 02:41 AM
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.
10-18-2018 03:09 AM
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.
10-18-2018 11:32 AM
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
10-18-2018 11:57 AM
@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.
10-18-2018 03:28 PM
@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