case structures in labview
Oct 15, 2023
simple description of one way to do multiple if then cases in labview using compound arithmetic
Okay, this video is going to talk about how we can work with case structures in LabVIEW.
Specifically, case structures are what we're going to use to try and answer the question
of what we normally do in coding of if this, then that.
If this, then that.
Right?
So we want if statements, and we want them to be conditional on different sort of outcomes.
So we're going to control E, go back and forth between the main screen and the block diagram.
So what I've done is I've opened up the accelerometer program that came with the example of links.
So if we hit control E, it'll take us back, and you'll see I've just given myself a little
bit of room.
I've opened this up a little bit because I've added some things.
The main part of this is exactly what comes in the example that you'll have when you open
it.
So all of these things are already there, and I'm not using any of those right now.
I'm just using the while loop for this demonstration, and here's what we want to test.
I want to test, I hit control E, I want three different LEDs, named 1, 2, and 3, to light
up if any of my values for A, B, and C are greater than 1.
So for example, right now, A is greater than 1, B is not, and C is not.
It's equal to 1.
It's not greater.
Right?
So I only want this first light to light up.
So how am I going to make it do this?
Right?
So let's go back, control E to our block diagram, and this is what we see.
Here's where I have those values that can be input, right?
So if you right click on this, it's currently a control.
We could change it to an indicator.
So what's the difference between a control and an indicator?
Control is something that we can type in.
It's going to control something later on, whereas an indicator just shows you basically
the current value of a variable.
So down here, and it shows whether it's coming out the right hand side, it's a control.
If it's coming in from the left hand side, it's an indicator.
So later on, I'm going to talk about cases, and I just want to see what case we're at.
So this is currently set to an indicator, right there.
It's not a control.
I can't type on this and change its value.
Right?
I shouldn't change, or I shouldn't.
I shouldn't change.
I shouldn't be able to change this, right?
Okay?
Because it's set to an indicator, right?
It's receiving values and just showing it.
These things are, you're putting a value in and they're sending them somewhere.
So for my values of A, B, and C, I want these to be controls, and I want to be able to move
them.
If I click up or click down, I want to be able to do that, okay?
Now we want to ask the question, are any of these greater than one, right?
So for value A, we're going to send it over here to this greater than comparison.
Now to find that, you could come here, you could go to your comparison block, and you
could grab it.
For anything, if you know the name of it, you can hold down control and hit space, and
just type it as well.
Greater.
Is it greater than, just generic, this thing.
And we could drop it on there, and then it'll tell you what it's expecting.
Here's X, here's Y, here is X greater than Y.
So it tells you exactly what to do if you hover over these things.
So we can go ahead and delete that.
So now we're asking, is A greater than one?
If so, then we're going to do something.
Is B greater than one?
If so, then we're going to send this value and go do something with it.
Is C greater than one?
We're going to take this value and send it somewhere.
If any of these are false, I'm going to send the value of zero out.
If this is false, zero.
If true, then I give them values.
Now, why have I chosen one, two, and four as the values that are going to get sent on?
I'll show you in a moment.
Where these are going to, if you follow the line,
they're going inside what's called a compound arithmetic.
Again, compound arithmetic, right there, compound arithmetic.
Another way to find this, if you go here and go to your, I think, numeric tab,
right here, compound arithmetic.
What this does is it takes the number from each of these scenarios,
it enters them into these little boxes here, and it outputs.
I wonder if we hover over if it'll show us.
It just shows us the result.
It adds these up, three values.
So for example, if all three of these conditions were true,
we would have one cent, we would have two cent, and we would have four cent.
This compound arithmetic would add those up.
One plus two plus four is seven, right?
So if all those are greater than one, this case number,
since this is an indicator, should show seven.
Let's test it real quick.
Let's go ahead and run this program.
Give it a second to start.
If it gives us an error, we're just gonna start it again.
See, this is common that it gets this timeout error.
No problem, we run it one more time.
So it's now running, and we said if all three of these variables were greater
than one, then we should have a case equal to seven.
So greater than one, greater than one, greater than one.
Over here, our case is equal to seven, right?
That's terrific.
All three of them are turned on, right?
If only, what about if just A and B are turned on?
Well, both of these are greater than one.
So this sent a value of one to the compound arithmetic.
This sent a value of two to the compound arithmetic, and this sent zero.
One plus two plus zero equals three, and in fact, we're at case three.
What about if just A and C are turned on, right?
Well, this sent a value of one, this sent a value of four, so
we're at case number five.
Now, how do we make this turn on the lights and control the lights?
Let's stop this and let's program that.
We want the following.
If it's case, we can set our different cases here.
This is just a case structure, right?
Normally, if you come up here to case structures and you create one,
say a case structure, by definition, it has true or false.
But you see here, we have numbers.
Well, that's because this is expecting a true or false.
If I took an integer, I think this will work, and
I just did a numeric constant of zero, and I sent this in here,
it'll change it to a different type of structure, where now it's accepting
different numbers based on the number that comes in, right?
So we can get rid of this.
So I've already done that here, and
we have a number coming from our compound arithmetic function being sent over here.
So based off the number that this thing spits out,
we're gonna get different cases, right?
And right now, I have cases defined for zero, one, four, and two.
But I don't have cases for what happens if both case number one and
number, for case A and C, if those are both true,
then we're gonna send a one and a four, so that needs to be case number five.
So let's make one.
You can do that really easily.
I'm just gonna go here, and you're gonna do add case after.
I right-clicked, and then I go add case after, and we're gonna say five.
If the value happens to be five, what do we want to have happen?
Let's go and let's take these things.
These are just Boolean constants, they're controls.
Let's go to case five.
We're gonna put them here, and then we're gonna connect them to the outputs,
which is the indicator of whether or not those values are being met, right?
So we've done five.
What if all three of them were turned on?
Then we need a new case.
Come right-click, add a new case.
If it equals seven, one plus two plus four,
then we want to paste these things again, and we want to wire them up.
Oops, wire it up, wire it up.
Now, what if just one and two are turned on?
That needs to be case three.
We haven't defined that yet, so let's do one and two, case A and B,
if they're both there.
Add it, this is gonna be three.
We want to paste these, wire them up.
Now, what we haven't been doing is changing the values.
These are all set to false.
We're gonna have to go through and have to set them all.
We'll do that in a moment.
What about if case B and C are correct?
That's gonna give us six.
And again, that's the only way we could add it to six.
That's why we've chosen these weird values of one, two, and four.
We want, as we combine these together,
to have no way to have two different cases exist.
We only want one way to exist.
Show an example of that in just a second.
Okay, now I think we've got all the cases that we need to worry about ready to go.
All the different outcomes and possibilities, I think we've got them ready.
So now let's start making sure that they're wired up correctly.
Case zero, these should all be set to false.
We don't want to show any of these are lit up
because none of these are fitting their criteria.
If it is case one, the only way we get that is if A is lit up.
So we want true to be sent and the other two to be false.
And it's already done and ready.
If it's case four, the only way that we get to four is if just the case for
case C is true.
So we want just that one to be lit up, and it's ready to go.
Case two is only if this middle one is lit up.
It's the only way we get there.
Case five only happens if both A and C are correct.
So let's change these.
True, true, I'm just left clicking those.
Case seven is if they're all true.
And case three is if one and two are true.
A and B are true.
Case six is if B and C are true.
So this should be good to go.
Control E, come back here, tell it to run.
If it gives us a timeout error, we're just gonna accept it and do it again.
It actually worked for a second, that timeout, let's do it again.
Okay, and here we are.
Now we can change things in real time, right?
Right now, it currently is set to three, one, and four.
So A and C are greater than one, and they are lit up.
The first one and the third one are lit up.
If we make this one greater, now they all light up.
This one goes back and we lower this one below, only the third one lights up.
If we turn them all below, they should all turn on.
This is greater and this is greater, then they turn on.
So this is how you do if and then statements in LabVIEW.
It's not the most intuitive way, it's a little bit clunky to use this
compound arithmetic, but that's how we're gonna go about doing it.

