LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

count button when pressed

Solved!
Go to solution

Untitled.png

Hello everyone, here I have a problem.
I want to make a button, where when the button is pressed (active/true) it will count.

 

I tried using two types of buttons, OK button and Boolean, to see the difference. For more details see the attached image.

 

the problem is whether can the button labeled Boolean do the calculation like the OK Button.

Please help sir, I'm new to labview programming. Thank you

0 Kudos
Message 1 of 9
(2,789 Views)

Hi Oto,

 


@MangOto wrote:

the problem is whether can the button labeled Boolean do the calculation like the OK Button.

Please help sir, I'm new to labview programming. Thank you


When you are new to LabVIEW then you should take those Training resources as offered at the header of the LabVIEW board.

On your question: learn about the different mechanical behaviours of those buttons! (switching vs. latching…)

 

On your VI:

why do you need to count using a floating point value? Wouldn't an integer do the same job more nicely?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 9
(2,776 Views)

Thanks for the suggestion sir, I already understand the difference between toggle button and lock.
Based on the VI I made, I used two buttons.

 

1. The one labeled Boolean will operate the switch when pressed.

2. The Ok button operates the latch when pressed.

 

When viewed in Labview operations. My goal is to make the Boolean button work the same as the OK button. If it is true then it is worth 1 if false the value is still 1, if the "Boolean" button is pressed again or is true then the value to be shown will increase to 2 and when false it will still be worth 2 (will increase by 1 if it is true).

 

Untitled.png

I don't have any ideas, sir, can you give me a solution for this problem? I will be very grateful if you provide assistance.

and thank you sir for the comment too

0 Kudos
Message 3 of 9
(2,754 Views)

Hi Oto,

 


@MangOto wrote:

My goal is to make the Boolean button work the same as the OK button. 


Then you should set them to use the same mechanical behaviour...

Best regards,
GerdW


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

Gerds gave you the neatest solution for your question. This comes  from the additional behavior LabVIEW implements into the boolean switch, when you set the mechanical behaviour to latched. The "properties" dialog illustrates this very good.

But there are two ways you would implement this latched behaviour yourself. The one (worse) solution is to compare for every loop cycle the current state of the bool with the state of the previous cycle (use a shift register to store the previous state) and increment the counter only if the states are different and the current is TRUE.

The second (better) way is to use an event structure. And this is all the way better than polling the switch all the time, epecially since you don't use a wait timeout.

Greets, Dave
0 Kudos
Message 5 of 9
(2,709 Views)

@MangOto wrote:

Thanks for the suggestion sir, I already understand the difference between toggle button and lock.
Based on the VI I made, I used two buttons.

 

1. The one labeled Boolean will operate the switch when pressed.

2. The Ok button operates the latch when pressed.

 

When viewed in Labview operations. My goal is to make the Boolean button work the same as the OK button. If it is true then it is worth 1 if false the value is still 1, if the "Boolean" button is pressed again or is true then the value to be shown will increase to 2 and when false it will still be worth 2 (will increase by 1 if it is true).

 

Untitled.png

I don't have any ideas, sir, can you give me a solution for this problem? I will be very grateful if you provide assistance.

and thank you sir for the comment too


The OK button and the boolean button are both boolean buttons with different mechanical actions. Just set the mechanical action the same and use the same code.

0 Kudos
Message 6 of 9
(2,698 Views)

If your goal is to make the first loop act like the second you need to program it the same.  Even if you use the same mechanical action for your buttons you will not see the same behavior since the code is different.  We can't see what's in the other cases but in your first loop you have the Select function set up to toggle between a 1 and 0 based on the boolean switch.  Your wiring is messy so we can't even see which is selected but it looks like you can only toggle between 1 and 0.  You will never count up past that even if the false case has an increment.

 

Why did you code two different things if you want the same behavior?

 

EDIT:  Forgot to mention that you should put a Wait VI in the loop to prevent the loop from spinning so fast it consumes all your CPU cycles.  Put a 25mSec wait in there and then you will see that when you use a latch method it increments by 1 on each click and when you use a switch method it will keep incrementing 40 times per second when the switch is TRUE.

 

EDIT2:  DaveTW is right about the event structure option.  It is designed for this kind of thing and is the best way so look into it after you understand the way you're doing it now.

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019
0 Kudos
Message 7 of 9
(2,688 Views)
Solution
Accepted by topic author MangOto

In summary:

 

  • Learn about the various mechanical action settings of booleans (right-click...mechanical action).
  • Don't use switches for latch action. It is not natural!!! If you use a reasonable boolean, the mechanical actions is probably already what you need.
  • Simplify your code! You don't need case structures, select, and all these trimmings (See below).
  • Use the correct datatype. Orange is not correct for counting, i.e. integers.
  • You need to define a reasonable loop rate. (as fast as the computer allows is too fast!).
  • You can use one single loop with two shift registers to demonstrate both scenarios. Keep it simple!
  • When attaching code, use "save for previous". Many don't use LabVIEW 2021 yet.

 

Here's a more elegant solution and probably all you need. At your state of learning, I would avoid the event structure. that comes later.

 

altenbach_0-1661870330428.png

 

 

Message 8 of 9
(2,677 Views)

thank you very much sir for your help

0 Kudos
Message 9 of 9
(2,602 Views)