Discussion:
Simple Variable increment question
(too old to reply)
labtech5555
2008-03-15 13:40:05 UTC
Permalink
This is probably the simplest thing to do but i cannot figure it out using labview.Using line by line coding its so easy.
 
I want to assign a variable with initial value x=0. Then increment that variable value everytime a case statement is true and
diplay the value to the screen.Therefore x now becomes x=x+1.Dead easy...can some one show me a screen picture how this is done ?
 
If i create a local variable i cannot wire it as an output to anything which is confusing me.
 
Labtech5555
Dennis Knutson
2008-03-15 14:40:06 UTC
Permalink
Lets do it without a local variable. Instead of the case statement, you could also use an event structure.
<img src="Loading Image...">
&nbsp;Message Edited by Dennis Knutson on 03-15-2008 08:29 AM


Shift Register.PNG:
http://forums.ni.com/attachments/ni/170/309129/1/Shift Register.PNG
altenbach
2008-03-15 16:10:05 UTC
Permalink
labtech5555 wrote:

I can get it working ok with boolean data but still gives strange results if try to use intergersas with my initial example above.


What is your definition of "strange results"??? Please attach your code so we can tell you what the problem is.
&nbsp;
If you want to modify the example by Dennis with a local variable instead of a shift register, lust delete the shift register and place a local variable(read) to the&nbsp;left of the case structrure where the wire enters the case. Then you need a second one in a data dependent frame outside the loop so it resets before the loop starts. A shift register is the highly preferred solution. (A shift register (or feedback node)&nbsp;is cleaner and more efficient, in starts at zero automatically, it does not require extra data copies in memory, it uses dataflow to minimize the possibilties of race conditions, etc.)
labtech5555
2008-03-15 16:10:04 UTC
Permalink
Hi Guys,
Thanks for your help,first Matt.Sorry i am currently at home using a very old version of Labview so cannot open your file.
A picture of the screen might be better or version 8.0 which i could open later...I've had a look at the help again an now see how to
change the local variable from read to write.I can get it working ok with boolean data but still gives strange results if try to use intergers
as with my initial example above.
Dennis,I understand your program and that might be a solution for what i am trying to do but would still like to understand how it
can be done with a local variable.
Labtech5555
Dennis Knutson
2008-03-15 17:40:06 UTC
Permalink
If the variable was incrementing constantly, then you probably&nbsp;have the mechanical action of the boolean to one of the switch modes. Set it instead to Latch (i.e. Latch When Released). Here's an event structure with a local. The while loop and case statement in my earlier post can be use as well.
<img src="Loading Image...">
You'll need to shift your thinking a bit when you move to LabVIEW. I went throug the same learning curve when I moved from text based languages to LabVIEW. There are some excellent resources for Learning LabVIEW at <a href="http://www.ni.com/academic/lv_training/how_learn_lv.htm" target="_blank">http://www.ni.com/academic/lv_training/how_learn_lv.htm</a>.
&nbsp;Message Edited by Dennis Knutson on 03-15-2008 11:36 AM


Increment with Local.PNG:
http://forums.ni.com/attachments/ni/170/309152/1/Increment with Local.PNG
labtech5555
2008-03-15 17:40:04 UTC
Permalink
Hi Guys,
I am used to line by line C programming&nbsp;which of course&nbsp;uses variables to control things.Changing to labview
is totally differant thinking as event driven,&nbsp;but i'm still thinking about variables which are nomally easy to control and declare in C programming..
If i seen an example what i wanted originally&nbsp;then i may agree its the wrong way to go and use what Dennis has suggested.
I can't try it untill i go back to work on Monday on the project i' writing...
The example i tried to today is in version 6 so to old to post and i've deleted it already anyway..Instead of getting x incrementing by by 1 at
a time it was going up by a very large number everytime the case was triggered.If no success i'll right it again at work and post&nbsp;on Monday&nbsp;..
Thanks,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Labtech5555
altenbach
2008-03-15 17:40:06 UTC
Permalink
labtech5555 wrote:

The example i tried to today is in version 6 so to old to post and i've deleted it already anyway..Instead of getting x incrementing by by 1 at a time it was going up by a very large number everytime the case was triggered



This means two things that are probably easy to correct:

- Your loop does not have a wait statement. (every polling&nbsp;UI loop needs a wait (the earlier eample by Dennis above&nbsp;has a 250ms wait, limiting the loop time to 4/second).

- Your button is NOT set to latch action. (it is probably set to "switch when pressed" (like a door bell) or "switch until relased") This means that if you press it, it will stay TRUE for a long time, and since the loop spins at a speed only limiited by the CPU (millions of iterations/second!) the increment executes many times in a row. If you set the button to latch action, it will only be true exactly once per press and will reset to false as soon as the code has a change to read the new value. To change the mechnical action, right-click on the button and select "mechanical action...latch when released".


Message Edited by altenbach on 03-15-2008 10:39 AM
altenbach
2008-03-16 17:10:04 UTC
Permalink
altenbach wrote:


&nbsp;(it is probably set to "switch when pressed" (like a door bell) or "switch until relased")


Sorry, I accidentally mixed&nbsp;things up when I&nbsp;added the&nbsp;illustrative description. Correct is of course:
&nbsp;
..&nbsp;(it is probably set to "switch when pressed" (like a light switch door bell) or "switch until relased" (like a door bell) )&nbsp;&nbsp;...
:D

Continue reading on narkive:
Loading...