LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Controlling LEDs in main VI from SubVI

I’m taking a university course where all assignments must be written in LabView. I’m new to this and we are expected to learn it on our own since the course does not give us any lessons in LabView. I’m currently working on an assignment that involves building a traffic lights simulator with different functionalities.

 

One functionality is an emergency switch that makes the yellow lights blink until the user flicks the switch again. I have tried to implement this functionality in a SubVI, but I’m struggling to understand how I can control the lights (LEDs) in the main VI from the SubVI. I would really appreciate it if someone can offer me some advice as I’m feeling very lost.

0 Kudos
Message 1 of 7
(1,408 Views)

There are learning resources at the top of the forum.

 

All you need is a simple state machine. No subvis at all.

 

See how far you get. If you get stuck show us what you have and ask specific questions.

0 Kudos
Message 2 of 7
(1,404 Views)

Hi johnloser77,

 

I have attached a very simple light blinking VI (Example.vi) that you can use as an example to understand the basics, as well as expanding it for further functionalities.

As it has been said, you just need a simple state machine to make this work; no subVIs here. 

 

Main points to consider in Example.vi:

- While loop makes the code execute continuously, so we can read the switch at each iteration, and output accordingly.

- Case structure has 2 cases: True and False. If the switch is True, True case will execute. And otherwise.

 

This post also deals with Traffic lights simulation: Solved: case structure: boolean control did not switch from false to true - NI Community

Take a look at this link, too: Application Design Patterns: State Machines - NI

 

We've all been lost at the beginning, make sure you take your time to understand the very basics.

Keep us updated!

Fort

0 Kudos
Message 3 of 7
(1,354 Views)

Here's a simple example for one light (modified from here)

 

See if it can give you some ideas....

 

altenbach_0-1662824731964.png

 

 

You can easily expand it to simulate a full intersection with multiple lights and such. Keep it simple!

 

0 Kudos
Message 4 of 7
(1,341 Views)

If you are familiar with other programming languages (Basic, C/C++, Java, etc.), then LabVIEW will have a few "surprises" for you.  Unlike (what I might call) "sequential" Languages, where you write down the steps "in order" that you execute them, sometimes "surrounding" them with other statements that represent a "boundary" that means "These are grouped together as one" (like a "Repeat .. Until" sequence, or a Function/Subroutine declaration, LabVIEW is a "Data Flow" language that has the following ideas:

  • There are Structures and Functions that take Data In and produce Data Out.  Note that a sub-VI is an example of a User-Define Structure/Function, usually with Data In and/or Data Out terminals.
  • Data are carried in "Wires", and (generally) travel from Left (where data were put in) to Right (where the data can come out).
  • The "Three Rules of Data Flow are:
    • A Structure or Function does not operate until all of its "required" Data Inputs have Data present.  [Note some inputs might be "optional" and have a default value if not wired).
    • When a Structure or Function exits, all of its Data Outs must have Data (wired to them from "inside" the Structure or Function).  No data appears on the Output Terminals until the Structure/Function exits.
    • If two Structures or Functions are not "serially" connected by wires (i.e. Data Out from one going into Data In of another), you can't determine which executes first.  This last feature means that two "unrelated" structures, for example, two While Loops or For Loops placed one above the other, with no output from one going to the Input of the other, execute effectively simultaneously (one might run "faster" than the other if it has to do a lot more computing, of course ...).

On the Block Diagram Palette, the first item shows the common LabVIEW Structures, including For and While Loops and the Case Statement.  The other Palette items are the more common Functions, arranged (more-or-less) by type of Data being processed (Numeric, Strings, Arrays, etc.).  Sub-VIs that you write I can think of as "Functions" (because they "Do Something" to the Data).

 

Bob Schor

0 Kudos
Message 5 of 7
(1,331 Views)

Hi Johnloser77,

 

as all the profis' reponses, the best solution is to make it as simple as possible, but if you insist to do data exchange between VIs, you can use FGV(Functional Global Variable), it is basically a VI you can use to save your data buffers and share between different structures and VIs. It has save the data in its Loop Shift Register.

 

https://learn-cf.ni.com/teach/riodevguide/code/rt_functional-global-variable.html

 

Regards,

FrankenChino

0 Kudos
Message 6 of 7
(1,274 Views)

Let's go back to the beginning:

 


@johnloser77 wrote:

One functionality is an emergency switch that makes the yellow lights blink until the user flicks the switch again. I have tried to implement this functionality in a SubVI, but I’m struggling to understand how I can control the lights (LEDs) in the main VI from the SubVI. I would really appreciate it if someone can offer me some advice as I’m feeling very lost.


OK, so you say you want to implement the "functionality" in a subVI, but you even fail the mention where that "emergency switch" is. There are many possibilities:

 

  • A physical switch connected to a digital IO?
  • A virtual switch ion the front panel of the subVI?
  • A virtual switch on the front panel of the mainVI?
  • etc.

 

90% of the correct solution is starting with a detailed definition of the problem. Maybe start by attaching the code you have so far.

0 Kudos
Message 7 of 7
(1,258 Views)