Best way to check multiple Boolean values?

In your program you need to check which button out of a couple of buttons, say, on a joystick, is currently pressed. Depending on which one it is you return a different value. So basically checking which of the multiple boolean values is true. What are some ways to do this, specifically in LabVIEW?

There’s always the old “check one, if false, check the next, repeat.” (a whole bunch of if…else statements in C). But in LabVIEW this means putting a lot of Case structures within themselves, which is a major pain especially if there are a lot of values/buttons to check.

Is there maybe a way to put all the button values into an array, search for a true or 1, and return which index it was? I don’t think there’s a “search array” VI.

In C++ you could do


if (joystick.GetRawButton(1) && joystick.GetRawButton(2)) {

}

Valid boolean operators are || (or) and && (and). I’m not sure how LabVIEW processes these but I’m sure there’s a boolean conditional in LabView.

I’m struggling to understand a situation where you have that many mutually exclusive buttons.

For instance - we have a button to reverse our front roller, that leads to a case statement that sets the motor speed. We have another case statement that fires the release on the catch.

The two have nothing to do with eachother, and the system works just fine if both are pressed or not pressed at the same time.

What is your situation, more specifically, where you have a bunch of buttons that affect eachother if they are pushed? (That is, if you can tell us without giving away your robot design). I’ve having trouble defining your problem statement in my head.

Put all of your boolean values in an array, then use the “Boolean Array to number” vi. This converts it from binary into a number. Wire that into a single case structure and create cases for the different values. For instance, “off off on” would be “1” and “off on off” would be “2”.

I think Luke already suggested an excellent way to do this, but to answer your question–yes you can do as you suggest and use Build Array to make an array of Booleans and then use Search 1D Array to search for a True (or False) value in the Boolean array. Both of these functions are found in the LabVIEW Array palette.

Just to add a bit more to the excellent suggestions. If you have say six buttons, it is sometimes useful to see how many are on, or to see if one of the first two, one of the second two, and one of the last two are on.

For searching how many are on, another trick is to send the Boolean array to the Boolean to (0,1) conversion function. This will give you an array of integers with a 0 for falses and a 1 for trues. Then you can do things like Sum in the numeric palette to answer how many Booleans were true in the array.

To check for combinations, you can use Boolean operations directly on arrays, and sometimes that is nice. For example to And your buttons with a mask to see if specific ones are on. If you fine the arrays of Booleans difficult to read off, some people prefer to cast them to numbers and then do Boolean operations on the integers, for example MyInt AND Mask. In other words, the Boolean operations work on integers as well as Booleans.

Finally, to make the most self documenting code, it is often better to build clusters of the Booleans, or to convert the array to a cluster. The cluster allows you to name the individual Booleans, and of course you can use Boolean operations on the clusters too. So MyBooleanCluster AND Mask gives a very readable bit of code. It is often then useful afterwards to do quick conversions to integers to compare with 0 or to convert to array to search, etc. If you want to spend the time to make a typedef cluster of your Boolean cluster, you can make very readable code and cut out lots of logic debugging.

Greg McKaskle

Thanks for the great replies! Now that I know that there is a “search array” VI, I think I like that idea best. But Greg Mckaskle gave some good points too.

One poster asked about my scenario more specifically. I tried to keep it general because this could be used for multiple instances, and I didn’t just want help on my specific problem. But I can see how it could be confusing. So let me give an example, my current scenario. We have a ball kicker on our robot this year. We plan to program it to kick with various speeds depending on which “fire” button the operator pushes. So at one point in the programming we need to sense which button is pressed, if one is pressed at all. That’s where I’m coming from.