LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Variable value in for loop starts with last entry from previous run

Solved!
Go to solution

I am having trouble with a for loop...I am a beginner with Labview and maybe don't follow the order of events (since it doesn't  execute line by line like typical programming languages), but I think my problem is trivial.

 

Say I am running a for loop, for 16 iterations, and I want to build an array that, for example, consists of four 0s, four 1s, four 2s and four 3s.   I initialize the array outside, and use shift registers to add to the array in the for loop.

 

However, the first time I run the program I get funky results.  In subsequent runs, the first value in my array is a 3, and the rest of the rest of the array is shifted down by 1, so I only have three 3s at the end (for example).

 

Can someone help me understand how to solve this problem?

Thanks,

George

0 Kudos
Message 1 of 18
(4,015 Views)

George,

 

Much easier to build array using FOR loop with auto-indexing.

 

Message Edited by Wayne.C on 05-04-2009 02:54 PM
0 Kudos
Message 2 of 18
(4,006 Views)

Thanks.  The for loop is auto indexed.  But I have other variables created in the foor loop and their initial value is triggered by a random numebr generator and based on that, my value say starts either with a 0, 1, 2, or 3, but should never decrease from there.

So, sometimes the second time I run the program, my first value (or soemtimes the first two values) in the array is a 3 (since it was last the last time) and then it starts aggain say with the 0, 1, or 2 value...

0 Kudos
Message 3 of 18
(3,997 Views)
Sounds like an uninitialized shift register.
--
Tim Elsey
Certified LabVIEW Architect
0 Kudos
Message 4 of 18
(3,990 Views)
I'm not sure if this is what oyu mean, but connected to the shift register on teh left side is an "initialize array" command, initialized to zero
0 Kudos
Message 5 of 18
(3,985 Views)

george,

 

I guess I don't understand what you are trying to do.  Can you post your code?

0 Kudos
Message 6 of 18
(3,984 Views)

Sure, i broke out the section that was giving me trouble

Attached is the code.

Basically when healing pattern is 0, then tehe healing31 array should either be all 0s or 1s

When healing pattern is 1, healing31 array should be half 0s then 1s

when healing pattern is 2, the healing31 array should be an even distribution of 0s, 1s, 2s, and 3s

 

once you run it a second time, the last value for healing 31 starts the new array...it should never start with 2s or 3s

 

Hope it makes sense

 

 

0 Kudos
Message 7 of 18
(3,950 Views)

The same thing happens with the force array as well.

 

Actually sometimes the first 2 values in the Healing31 array are the old value...

0 Kudos
Message 8 of 18
(3,946 Views)

Your code is full of race conditions due to excessive overuse of local variables. There are eight disconnected code fragments in the loop and they can execute in any order becaus there is no data dependecy between them. Your result will vary wildely depeding on the exact execution order. LabVIEW does NOT execute top-to-bottom and left-to-right at all.

 

Get rid of all the local variables (none are needed!) and wire things together in the order they are supposed to execute.

0 Kudos
Message 9 of 18
(3,941 Views)
Like I said, the shift from line by line executing is new to me.  I was trying to keep the program visually in order, without wires running all over.  But I see what you're saying.  I'll try it and see.
0 Kudos
Message 10 of 18
(3,935 Views)