LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

For / While loop first execution

I open the program below and run it and Numeric 2 and 4 only increment 9 times

The loops both get executed 10 times (0,1,2,3,4,5,6,7,8,9)

cwhw112_0-1695773184549.png

every subsequent run, Numeric 2 and 4 only increment 10 times

cwhw112_1-1695773278313.png

I tried making 19 the default values for Numeric 2 and 4, saved, closed, opened and Numeric 2 and 4 are shown as 19 but when I run it the first time, they change to 9

 

If I change it to a shift register then it seems to work even on the first run

 

cwhw112_3-1695773758058.png

 

 

0 Kudos
Message 1 of 21
(1,449 Views)

You might want to attach your Vi instead of showing pictures. We cannot run or debug pictures!

 

Default values for indicators are pointless, they get overwritten by the data in the wire. Default values make sense for controls only.

Why don't you initialize your shift registers and feedback nodes of you don't want to retain data between calls?

 

(Please mind your numeric representations. There should be no orange at all anywhere! )

0 Kudos
Message 2 of 21
(1,445 Views)

ref attached

Download All
0 Kudos
Message 3 of 21
(1,425 Views)

I think you missunderstand how these uninitialized shift registers and feedback nodes function.

They DO NOT read the value of the output, they have an own value. After every run, they just uses the last value they had, if you close the VI / LabVIEW it starts at 0.

 

Greetings

Timo

0 Kudos
Message 4 of 21
(1,384 Views)

@cwhw112 wrote:

If I change it to a shift register then it seems to work even on the first run


There's a subtitle difference between when a shift register resets and when a feedback node resets.

In fact, when a feedback node resets can be changed if you wire the initial value (to reset on compile\load or on first call. This makes a difference, especially when working on the VI (this can force a re-init) and\or when using continuous run vs running in a loop.

 

 

Also (seemingly minor things that become huge when you generate more code):

  • why not make integer values integers? 
  • why not use increment\decrement in stead of adding 1?
  • why not subtract outside the loop?
  • I'd always use '>=' to compare a stop condition. Although '=' works '>=' is just safer for the future.

You're probably learning, but there's merit in considering these kinds of things as soon as possible.

Message 5 of 21
(1,369 Views)

Please consider the things, Wiebe wrote to you.

 

In my opinion you don't understand the priciple of a feedback node.

In your picture the output of the FB-node is on the right side and will return the initial value on first run. This will be zero and shown by indicator 2. Then this value is incremented by 1 and fed into the FB-nodes input to store this value until the next call.

In the second loop the FB-node returns the stored 1 (displayed by Indicator 2) and gets the incremented 2...

In the last loop the FB-node returns the stored 9 (displayed by Indicator 2), gets the incremented 10 and then the loop quits.

So, until there everything as expected.

 

>>>>>If I change it to a shift register then it seems to work even on the first run

 

I think, now you see the difference in your code and why it behaves differently: you wired the indicator behind the increment operation, so you see the incremented value right in the first loop- execution. You could get the same result if you wire indicator 2 to the INPUT of the FB-node, because this is after the increment- operation 😉

 

Greets, Dave
0 Kudos
Message 6 of 21
(1,353 Views)

Reply to t.n14 comment

ok, so on first time open, its value is 0

and my loop runs 10 times

1st time through loop, inside the loop the addition of the start value of zero and the constant 1 results in a value of 1

2nd time through loop ... results in a value of 2

3rd time....3

4th....4

5th...5

6th....6

7th...7

8th...8

9th...9

10th...10

 

but at the end of the 10 loops the resultant value is 9, not 10

0 Kudos
Message 7 of 21
(1,303 Views)

reply to Wiebe comment

I created a constant of zero wired to the initializer and every time I run it (first time after opening file or X time) Numeric 2 is 9

I put in an increment and it operates similarly (first run after opening file Numeric 3 is 9, second time run Numeric 3 is 19....29...39...)

cwhw112_0-1695852897499.png

The number of times the loop increments i on each run is the same but the first run it is as though the loop is not gone through the first time

0 Kudos
Message 8 of 21
(1,301 Views)

reply to Dave comment

 

i think i understand your description of the feedback node but it seems counter-intuitive.

it starts out at value zero and when the loop is entered it first pushes that value out to Numeric 2

i would think that the addition would take the constant 1 and initial value of Numeric 2, add them together and then the result of 1 would go out on the wire on the right of the add and into the left of the feedback node and then the feedback node would put the 1 out on the wire on the right.

so that the feedback node waits to output until all inputs have arrived (i thought this was the timing of all function -wait to output until all inputs arrive and operation is complete)

but with the way you have described, at the end of the 10 loop iterations, Numeric 2 is 9 and Feedback node contains 10

the second time I run it after power up:

Loop FeedbackNode Numeric2

1                 11                10

2                 12                11

3                 13                12

4                 14                13

5                 15                14

6                 16                15

7                 17                16

8                 18                17

9                 19                18

10                 20                19

 

your description makes everything make sense but I dont care for the feedback nodes operation. Maybe the lesson is to use the shift registers.

Thank you 

0 Kudos
Message 9 of 21
(1,289 Views)

@cwhw112 wrote:

i would think that the addition would take the constant 1 and initial value of Numeric 2, add them together and then the result of 1 would go out on the wire on the right of the add and into the left of the feedback node and then the feedback node would put the 1 out on the wire on the right.


As I already said, you need to connect your Numeric 2 indicator to the wire going into the feedback node, not after.

Message 10 of 21
(1,281 Views)