LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

loop cvi

Hi,

I made a loop "for" with

for(i=0;i==359;i++) {//code}

but the program don't want to enter in the loop "for".

Sometimes I have "warning : will never be executed" by speaking about i++ so I tried with i=i+1 this don't change anything. And this error doesn't stop the run of the program that stop only the enter in the loop.

Somebody knows why I have this problem?

 
0 Kudos
Message 1 of 11
(5,822 Views)

The problem lies in the exit loop condition: see this page for reference.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 11
(5,816 Views)

I have this code :

int CVICALLBACK StartMot (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
    switch (event)
    {
        case EVENT_COMMIT:
        int i;
        ComWrt(1,"\r",StringLength("\r"));

        for(i=0;i==359;i++)
        {
            /* begin the rotation */
            ComWrt(1,"SK(i)",StringLength("SK(i)"));
               /* wait 5 seconds the time to make the measure */
             ComWrt(1,"\r",StringLength("\r"));
             DelayWithEventProcessing (5);
            /* stop the measure */
             ComWrt(1,"CP(i)",StringLength("CP(i)"));
             ComWrt(1,"\r",StringLength("\r"));

            //break;
        }
        GetCtrlVal(panelHandle,PANEL_intervalTimer,&IntervalTimer);

        return 0;
    }
    return 0;
}

 

I think it's the same that example that you sent me.

0 Kudos
Message 3 of 11
(5,810 Views)

It seems you have not read the page I linked beforeSmiley Frustrated Let me quote some text highlighting the relevant one:

 

for ( init; condition; increment ) {
   statement(s);
}

Here is the flow of control in a 'for' loop −

  • The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

  • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop.

 

In your case, you set i = 0 entering the for loop; immediately after you test for i == 359, which obviously is false, so the loop does not execute.

That's all.

This is not CVI specific, is pure C syntax.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 11
(5,807 Views)

I red it but I didn't understand everything I confess. 😛 So, that means I have to write for (i=0;i==1,i++)?

Because I thought "for" function was "from i=0 to i=359 you have to do i++" so it works by doing :

i=0

i=1

i=2

...

i=359

it doesn't work like that?

Message 5 of 11
(5,804 Views)

A programming language is highly structured and you must understand how the convention works in order to exploit it at best.

 

While in Basic you would write

for i = 0 to 359

in C you must write

for (i = 0; i < 360; i++)

In both cases, the for loop is executed for each value of i starting from 0 and ending at 359 inclusive. The condition clause in C syntax must be true for each value of i in that interval, and i < 360 is the only one that guarantees that.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 11
(5,800 Views)

I tried befor with i<360 but someone told me to change by i==359 because i<360 it was a mix between a while loop and a for loop

0 Kudos
Message 7 of 11
(5,793 Views)

I think the problem comes from DelayWithEventProcessing but I don't know what is this problem

0 Kudos
Message 8 of 11
(5,792 Views)

I saw your thread in the french forum: don't trust a LabVIEW programmer as an advisor for CVI! Smiley Wink

 

Let's start from scratch and see if we can proceed from a very simple structure to a more complex one.

 

First of all, do not consider acquisition from the external device, simply create the for loop that handle the motor adding a simple Delay (1.0) inside the loop insteaf of 5-sec pause. Work on this structure until it works well handling the motor.

 

When you are done with it, modifiy it with a Delay (5.0) so that the process can wait for the acquisition. Do not include yet communications with the acquisition device.

 

When you are done with it, replace the Delay with DelayWithEventProcessing, which as you can see by looking at the source code is simply a small loop that includes calls to ProcessSystemEvents (). Still do not process any other task. You should see the motor stepping with 5-sec pauses  between steps.

 

Only at this moment you can consider to add other tasks like the communications with the other device. You will need to design how those two tasks interact with each other, but let's proceed step by step and complete the preceding steps before.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 9 of 11
(5,778 Views)

It's a good idea ! I will try this method to find where the problem comes from!

Anaël.

0 Kudos
Message 10 of 11
(5,754 Views)