## Recommended Posts

Hi,

I'm a beginner with Labjack (U12) and Daqfactory, so i need some HUGE helps..i hope someone help me to find a solution for my school project...

the project basiclly is :

1. there are 10 switches and every switch will be activated in different time intervals...

2. when the first switch get activated, the time will start and be counted until the last switch get activated..

3. the program will detect the last getting activated switch and calculate the duration differences among switches ( the first one and last one, the second one and last one, 3.-10. , 4.-10. ,5.-10. , 6.-10. , 7.-10, 8.-10, 9.-10. ) and show that values on the screen.................

can anybody help me?

##### Share on other sites

I'm assuming these are external switches that you are reading through a digital input?

First, read all the inputs at some reasonably fast interval, say 0.05. If you need more resolution than that, you probably should consider streaming them, and I don't believe the U12 supports streaming of that many digital i/o, but truthfully, I've forgotten.

After that there are several ways to do it. I would probably use the channel event. Click on the channel name in the workspace and then select the Event tab. You'll do this for all ten channels. For the first channel, the event would be:

```if (switch1[0] &amp;&amp; !switch1[1])
global starttime = systime()
endif```

this assumes you named your channels switch1, switch2, etc. This will set the start time when the switch goes high. Next, for switch2, its very similar:

```if (switch2[0] &amp;&amp; !switch2[1])
global switch2time = systime()
endif```

and switch3 through 10 are identical, except of course change the 2's to 3's, 4's, etc.

Finally, create screen components, probably variable value. The expression for switch2's time would be:

`switch2time - starttime`

and switch3:

`switch3time - starttime`

etc.

##### Share on other sites

and secondly all my equipment is at school Lab. so i cant do what u suggest for the time being..

but i did something by myself before as u said but i couldnt succeed... because every cycle the last switch is

changing among 10 switches .. so the program have to detect the last getting activated switch in every cycle .. ( calculation of duration for each switch depends on the last switch) ... how can i do???

##### Share on other sites

I don't quite understand what you are trying to do. Could you please reexplain with perhaps more detail or a step by step? I want to make sure I understand so I don't send you down the wrong path.

##### Share on other sites

i'll try to make it clear.....

** i have 10 switches and these switches will be high one by one

( !! these 10 switches will be activated randomly and one of them will be the last ( so the last activated switch will be different in every cycle ))

** "when a switch get activated, it is high until the last ( 10. ) switch is high "...

** the program will calculate the duration of activation ( switch is high) for each switch..

##### Share on other sites

OK, I didn't realize they would be activated randomly and assumed they were being activated sequentially. I think I was picturing a bunch of photoeyes on a track and you were using it to measure acceleration or something.

Anyhow, do you want the time between the first switch and each other switch, or the total time each switch is high?

##### Share on other sites

u are right.. i'll use it for the distance calculation .. u know it depends on both the time and acceleration...

"Anyhow, do you want the time between the first switch and each other switch, or the total time each switch is high? " ....... i need only the total time for each switch as i tried to explain in my previous reply ...

i hope u can help me ... nevertheless i really want to thank u again for your intereset u've showed so far...

##### Share on other sites

So you basically want to calc the time from one edge to another. As most things there are a number of ways to do this. You can use the search function or you can use min/max which will also return the time of the min/max. Using Search:

1) At the beginning of your experiment, either record the time (by setting Systime() to a variable: global starttime = systime()), or clear the history of the channel. Lets say you clear the history: switch1.clearhistory()

2) Run the experiment, taking the data.

3) When you stop the experiment, the following code will tell you how long switch1 was high for its first pulse. In your experiment, presumably, each switch is high for only one duration:

```private loc = search(switch1 == 1)
if (loc == -1)
return  // now pulse found
endif
private st = switch1.time[loc]
loc = search(switch1 == 0,loc+1)
if (loc == -1)
loc = numrows(switch1)+1 // pulse never ended, go to end
endif
global totaltime1 = switch1.time[loc] - st```

If you don't clearhistory() but instead record the starttime of the experiment, then you need to subset switch1 in the first search:

`private loc = search(switch1[starttime,systime()] == 1)`

This will limit the search to data between starttime and the current system time. You don't need to subset the second search (though you could) since you are specifying where to start the search from.

##### Share on other sites

First, you CANNOT use waitfor, wait, or delay in a channel's event. Doing so simply stalls your data acquisition. OK, you can do it, but it requires a rather in depth understanding of the threading in DAQFactory and the reprecusions, so I'll just say you should never use them in an event.

Second, you can't put anything inside of {} but constants. If you want to wait for multiple comparisons, use the && (and) boolean operator: (switch2[0] == 1) && (switch3[0] == 1) && etc..

Next, you can't do Max(switch1,switch2,switch3, etc). Max expects but one parameter. If you want to concat several arrays, and switch1, switch2, switch3, etc are arrays, then you want the concat function:

max(concat(switch1,switch2,switch3, etc.))

I still don't understand what you are doing then if the answer I gave previously didn't work. Do you want to simply record the time it takes from the first switch going high to all ten switches going high?

##### Share on other sites

Of course i want to simply record duration but İ couldnt perform neither the previous suggestion u gave nor my work that u tried to correct

##### Share on other sites

The code I provided before actually works perfectly, except I got the subtraction backwards in the last statement so the times were negative. I have attached a sample showing it working. Simply click the Begin Exper button to start the experiment, which clears the history and inits the switch to off. Then click on the switch1 to turn it on, wait a few seconds and turn it off. Then click on the Calc Duration button and the duration that switch1 is on is displayed to the right. You could also do this from an automated sequence or event, but its always good to get it working manually first.

durationcalc.ctl

##### Share on other sites

Finallyyyy, its workinggg

##### Share on other sites

İ read my previous mails and found out a thing that can lead misunderstanding.. thats my fault and i apologize for this situation..

** "when a switch get activated, it is high until the last ( 10. ) switch is high "... is wrong

##### Share on other sites

Like many things in DAQFactory there are a number of ways to do this. Lets keep it simple and do it the brute force way. First, you'll need two variables to store the start and total time. Create a sequence with this script:

global starttime = 0

global totaltime = 0

Every time you want to reinitialize the experiment you should run this sequence. Then, in the event for each of the 10 switch channels, put:

```if (switch1[0] &amp;&amp; !switch1[1])
if (starttime == 0)
starttime = systime()
else
if (switch1[0] &amp;&amp; switch2[0] &amp;&amp; switch3[0] &amp;&amp; switch4[0] &amp;&amp; switch5[0] &amp;&amp; etc...)
totaltime = systime() - starttime
endif
endif
endif```

For each channels event, you'll want to change the first line to match the channel (i.e. switch2[0], etc), but leave the rest of the script alone.

##### Share on other sites

IT'S WORKING.... THANX FOR YOUR ENDLESS HELP.........

but i've another question:))..lol..

the timing value for each channel have to be min. 0,001 s and i know that labjack doesnt support that values so i decided to use NI 6224 device... my question is that can i use the daqfactory with this device or can u suggest any other device which has at least 20 analog inputs and cheaper than NI 6224... u know that am student and the price of the NI devices r very expensive for me to pay.....

##### Share on other sites

The price of NI equipment is expensive for anyone They have to pay for all the advertising some how!

Actually, the LabJack U3 can stream to about 50Khz or so. It sounds like you need 1khz, so you can easily stream all the analog inputs at this speed. The U3 has 16 analog inputs, and UE9 has 14, so you'd just need two of whichever works better for you. Unless you need high precision, the U3 should be fine. Two U3's are only US\$216, which is cheaper than many of the cable assemblies NI sells.

Please note that no matter what hardware you purchase, NI, LabJack or otherwise, you cannot really set your timing in your channel table to less than 0.01. This is true no matter what software you use under windows. This is because you have to leave some time windows to use the processor. You can get around this a little using a dual core processor, but really if you get below 0.01 you will want to use the streaming mode of the hardware. Streaming is telling the hardware how fast you want to take data and it then does it on its own, sending data back in blocks. As I mentioned, the U3 can do about 50k samples per second which is more than enough for your end need.

I should also mention that even though the table in the DAQFactory channel shows only to 0.001, DAQFactory records time to the microsecond.

Finally, make sure and review the DAQFactory - LabJack application guide and associated samples for doing streaming with the LabJack.

##### Share on other sites

Will i be troubled by Daqfactory if i use NI 6224 device at 0.001 or 0.0001 timing value although my PC has dual ( or 4) core processor .. seemingly, i cant use labjack because of its number of analog inputs and its sample rates

##### Share on other sites

I'm not sure about the 6224. I don't know if it runs on NIDAQ or requires NIDAQmx. In general, I recommend sticking with the LabJack as it will easily do it if you stream. The problem is that Windows is not a true realtime operating system and so even with multiple cores you can't get precisely 0.001 or 0.0001 timing. DAQFactory has a mode that does better than other programs by boosting the priority, but there are still things in windows that can affect this. You are much better with streaming a LabJack.

##### Share on other sites

i've been searching the driver of NI USB-6218 device for using it with Daqfactory but i couldnt find anything.. can u help me for the driver....

##### Share on other sites

it's written in usersguide document at 305. page that we can create a driver for u !!... is it real??.. can u create a driver for NI USB-6218 device?? and can u tell me how to add this card to device menu for channells..

##### Share on other sites

If the 6218 supports NIDAQ then you need to rerun the DF installer and select NIDAQ from the devices list as its not installed by default I believe. That said, I believe the 6218 only supports NIDAQmx which we do not have a high speed driver for. NI has made accessing their devices from C++ using mx quite annoying, basically making simple functions much more difficult (and slow) than they need to be, presumably to force you to use their software, whether its LabView, LabWindows, or something else.

##### Share on other sites

Somehow I missed your second post. We can usually write you a driver, though typically there is an engineering charge involved. Currently none of our NI drivers support streaming so you aren't going to be able to go faster than 0.01 seconds, and maybe not even that. We may be able to add streaming, but first I need to know if your device runs under NIDAQ or NIDAQmx only. The NI website is unclear on this, and they don't like to talk directly to us now that they are charging for technical support.

##### Share on other sites

it runs under NIDAQmx... if u can write the driver ,please add the streaming...