LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Creating Integer Variables

Hi Forum,

 

I am writing a program for photogate testing and need to create a variable that is either a 1 or 0 that I can use to change whether a case structure is entered or not. From what I have seen, LabVIEW's "variables" are different than variables in written languages like python or MATLAB, where you can assign a variable a value like this...

 

var = 0;

 

... which creates the object, or variable, "var" and gives it a value of 0.

 

A generalized, written-out example relating to my application is as follows. My problem is that I can't find an equivalent to what "var" would be in LabVIEW.

 

--------------

start of loop:

--------------

 

var = 0;

 

if (var == 0 and (photogate is activated)) {

      var = 1;

      (record photogate time);

}

 

--------------

end of loop

-------------

 

 

In this generalized loop, the if statement (case structure) is satisfied only on the first iteration because, inside the loop, the "var" variable is changed and causes one of the if statement's conditions to always be false thereafter.  

 

To summarize: How can I make an object like "var" in my examples that I can use to store a value? And if LabVIEW doesn't have something like this, what would you recommend as a different solution?

 

I am unable to upload any code for NDA reasons.

 

Thanks in advance!

0 Kudos
Message 1 of 10
(4,166 Views)

@snakefreysdoge wrote:

My problem is that I can't find an equivalent to what "var" would be in LabVIEW.


In LabVIEW, the variable is the wire.  For your exact situation, you will want to add a Shift Register to your loop to hold the value and allow you to update it each iteration.  Use a constant outside of the loop to initialize the Shift Register.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 10
(4,155 Views)

If you are experienced with text-based languages like Matlab and Python, you should be able to pick up the key elements of LabVIEW by looking at the first two Tutorial listings at the beginning of this Forum.  I recommend the LabVIEW "Boolean" type (instead of 0/1).  Data travel in "wires", which are (conveniently) color-coded to match the data type -- thin blue for integers, thin green for boolean, thin orange for floats, slightly thicker for 1D arrays, pairs of lines for 2D arrays (all color-coded to match the type).

 

Give it a try -- you'll be surprised how easy/intuitive it can be.

 

Bob Schor

0 Kudos
Message 3 of 10
(4,130 Views)

Hi snake,

 


@snakefreysdoge wrote:

To summarize: How can I make an object like "var" in my examples that I can use to store a value? And if LabVIEW doesn't have something like this, what would you recommend as a different solution?


You really should take those Training resources offered at the top of the LabVIEW board to learn the basics!

In LabVIEW the wire is the variable, and you need to obey the "THINK DATAFLOW!" mantra…

 

See this:

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 10
(4,092 Views)

As others have already said, you need to familiarize with the dataflow paradigm and forget terms from text based code. There are good training resources listed at the top of this forum.

 

If there are only two states (0,1), a boolean datatype is more appropriate (unless you want to add more states later, e.g. 2=unknown, 3=sensor fault, 4= building on fire, etc. 😉 )

 

So far you have only told use where the value is supposed to be going, but not where it comes from (A much more important fact!). I assume you are reading some external sensor which returns information on an output terminal of a provided LabVIEW driver. You would just wire that to your case structure, no variable needed. If you also want to see the state on the front panel for operator feedback, connect an indicator to the same wire.

 

If you read the sensor with each iteration, no "storage" is needed. If you only read the sensor occasionally, but need the value during multiple successive iterations, You can keep it in a shift register or feedback node. (see here for a very recent example).

 

0 Kudos
Message 5 of 10
(4,062 Views)

@Bob_Schor wrote:

If you are experienced with text-based languages like Matlab and Python, you should be able to pick up the key elements of LabVIEW by looking at the first two Tutorial listings at the beginning of this Forum.  I recommend the LabVIEW "Boolean" type (instead of 0/1).  Data travel in "wires", which are (conveniently) color-coded to match the data type -- thin blue for integers, thin green for boolean, thin orange for floats, slightly thicker for 1D arrays, pairs of lines for 2D arrays (all color-coded to match the type).

 

Give it a try -- you'll be surprised how easy/intuitive it can be.

 

Bob Schor


Not convenient to me - I'm colorblind.  😄

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 6 of 10
(4,050 Views)

To start with, I'd reiterate something very important that crossrulz said. I feel it can't be stressed enough for programmers who are coming from text land into LabVIEW land.

 

wires are your variables

 

Everyone here has given good advice. An hour or so of watching LV tutorials, especially those geared towards text programmers picking up the language will be extremely beneficial.

 

That having been said, let me try to answer your question directly.  Here is the loop you listed would look like directly inputted into LabVIEW (with a 50ms sleep time so that it doesn't free spin)

 

BowenM_0-1626722353675.png

 

0 Kudos
Message 7 of 10
(4,042 Views)

The only change I'd make is to use T/F Booleans for "binary" decision, rather than 0/1.  Note that this greatly simplifies the subsequent logic which no longer requires "numeric" comparisons, can all be done with pure Logic gates.

 

Bob Schor

0 Kudos
Message 8 of 10
(4,022 Views)

Can we go back to the original post and clarify some things?

 


@snakefreysdoge wrote:

 

 

--------------

start of loop:

--------------

 

var = 0;

 

if (var == 0 and (photogate is activated)) {

      var = 1;

      (record photogate time);

}

 

--------------

end of loop

-------------

 


While I admittedly don't deal with text based code, there are several issues here.

First of all, the "var=0" probably belongs before the loop because if you set it every time inside the loop right before the comparison, it is guaranteed to be zero at every iteration.

 

It seems to me you want to record time of any FALSE->TRUE transition, but you have not told us HOW you want to record that (append to log file? Append to a string on the front panel? etc.). You also need to define what you mean by "time" (Local time and date? GMT time? Time since the prevous photogate event? Time since start of the measurement?)

 

All these tasks are trivially simple and I can show you how. First, you better define what you actually want. Please let us know!

0 Kudos
Message 9 of 10
(3,948 Views)

Here's a simple example that logs time whenever the photogate goes from FALSE to TRUE.

 

Any other conditions and time formats (do you need milliseconds?) can easily be implemented. (The FALSE case is just wired across)

 

altenbach_0-1626793256572.png

 

0 Kudos
Message 10 of 10
(3,925 Views)