In the attached file, I need to detect a threshold around 0.5 in both signals then find the difference time between in all happening time in which threshold is met. In the example, two points are such as that. One of them is around the 10s and the other is around 130s. But my code detects just the last one (around 130s).
After looking at your code for five seconds, it is peppered with race conditions and your results are unpredictable. In the second frame, the local variables is read way before the indicator gets updated with new values, because the inner FOR loops and the case structure execute in parallel.
As for the task, I am sure you could do it with 5% of the current code using a single loop. Try it.
(Your first inner FOR loops do absolutely nothing. What is their purpose? Your second FOR loops are pretty pointless. You need to iterate n-1 times, because else you run out of elements in the last iteration. Index array is resizeable, you only need once instance. There is the "in range and coerce function", try it). Your indicators get updated as many times as the boolean turns true and only the last value is available after the loops complete. Your sequence structure serves no purpose. Why is there an outer while loop? You are spinning it as fast as the computer allowws, recalculating the same over and over, burning 100% of a CPU code yielding nothing new ever.)
Read the rest of my message above. Now you placed it inside the FOR loop, but there is sitll no way to tell if the local variable gets read before or after its indicator gets updated. Again, all you see at the end is the last update from some random calculation. Think dataflow!
You need to get rid of the local variables!
Thank you for your help. I think it works now but I am so eager to make it better. How is that now?
A bit better. But can still be improved based on Altenbach's comments and mine.
I cleaned up your code.
But it looks like your code can be replaced by the Threshold Detector. Two subVI's Done!
Your code is wrong. You started out with i=0 in the first iteration and compared i with i-1. When i is 0, i-1 = -1. There is no such thing as an array index of -1.
So working in the loop, you need to start at 0 and look ahead to i+1. When true, the code is returning i.
The Threshold detector is probably set up to (can't tell, the algorithm is hidden in a dll), to give the first index that is after the crossing.
To make them give identical results, you can easily do a +1 on the array of indices.