Set loop counter.


Recommended Posts

Hi,

I'm a total beginner with DAQfactory. I have a LJ U3. I've done the LJ App guide and have managed to control outputs using input devices no prob. I'm struggling with the following...

I have a digital output on a simple loop:

while(1)
   Output=1
   delay(4)
   Output=0
   delay(4)
endwhile

It's a simple 4 secs on 4 secs off cycle.

How would I set this to cycle for a set amount of times?

Any help appreciated. cheers.

Link to comment
Share on other sites

replace the while(1) with a for loop:

for (private x = 0, x < 10, x++)
   output=1
   delay(4)
   output=0
   delay(4)
endfor

Now, just as a trick, you could also do:

for (private x = 0, x < 10, x++)
   output= !output[0]
   delay(4)
endfor

Both will toggle output 10 times.

Link to comment
Share on other sites

Hi,

Thanks for that!

In the end, I managed to write the following:

global cycles=0
while(cycles<10000 & Fuel_Temp<40)
   Motor1=1
   delay(4)
   Motor1=0
   delay(4)
   cycles=cycles+1
endwhile

I also have a simple button to start/stop the sequence.

I now have 2 things I'm trying to do.

-The button restarts the count, Obviously it does this as it reads the variable (cycles) =0 when it starts the sequence. How can I Pause the sequence as opposed to stop start?

-I can log my fuel_temp (EI-1034) against time. How can I log my variable "cycles" against time so I can see how many it's done? I can see this on a "variable value component" at the moment, but I'd like to log it in the event of any failures.

thanks again for any help !

Link to comment
Share on other sites

Sequence looks good. A few cosmetic things:

1) I like to put () around each boolean expression:

while ((cycles < 10000) && (Fuel_temp<40))

also, not that you want && not &. & is a bitwise and which works differently.

2) you can do cycles++ instead of cycles=cycles+1 (you can also do cycles+=1)

OK, on to your questions:

a) pausing: you'll need another variable, call it pauseme:

global cycles=0

global pauseme = 0

while((cycles<10000) & (Fuel_Temp<40))

waitfor(!pauseme,1)

Motor1=1

delay(4)

Motor1=0

delay(4)

cycles++

endwhile

Then just set pauseme to 1 to pause, 0 to resume using a screen component

:blink: logging: logging sets only log channels, so you'll either have to stuff cycles into a channel or use an export set. The channel method is probably easier. Because things are so slow, I'd just do this:

1) create a new channel called cycles, Device Type "Test", I/O Type "D to A", Chan #, D# of 0.

2) change you code to:

cycles=0

global pauseme = 0

while((cycles[0]<10000) & (Fuel_Temp<40))

waitfor(!pauseme,1)

Motor1=1

delay(4)

Motor1=0

delay(4)

cycles=++

endwhile

All I did was remove the global declaration because now you have a channel called cycles and added a [0] in the while because cycles now has a history. You may need to restart DAQFactory or do system.clearglobals() to eliminate the cycles global you had. Then you can just add the cycles channel to the logging set.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.