LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I optimize this code?

Solved!
Go to solution
Highlighted

Hi all,

 

The function I want to realize is very simple. Reading the data from DAQ, play the sound file if a rising edge is detected, and stop playing if a falling edge is detected.

 

Attachment is my code, it works, but if I stop the program and start it again, the program will somehow get stuck(but no error), then all I can do is end the task and reopen the program. I put two case structures for each channel, I believe the problem is caused by the second case structure.

Piano1.jpg

 

I am a beginner of labview, could someone help me optimize this code?

Thank you!

0 Kudos
Message 1 of 11
(1,793 Views)
Highlighted

While this is not specifically in relation to the part of the code you are worried about it might help.

 

Have a look at the example finder for "Continuous Digital Input". I feel like using a digital input for your keys would make a lot of your logic much simpler so that you dont have to detect the rising edges.

 

After that  I would recommend a simple state machine using shift registers so that if there is no change then do nothing, if the state changes the trigger the playing or ending of the sound. That way you can merge your two case structures into a single one since in no instance should you be receiving a rising edge and a falling edge at the same time.

 

On another note, I would consider moving from the multiple file paths to a single folder path that you then create the paths from known information.

0 Kudos
Message 2 of 11
(1,750 Views)
Highlighted
Solution
Accepted by topic author yatang

First of all, use arrays to your benefit.  You can make this fairly simple by converting the Dynamic Data Type to an array of waveforms.  The Trigger Detection can process an array of waveforms.  You can then process all of the trigger detections in a FOR loop, updating which trigger (and the level) you are looking for dynamically.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Download All
Message 3 of 11
(1,709 Views)
Highlighted

Hi Crossrulz,

 

Thank you so much! I just have one more question.

 

I'm gonna use 12 channels of DAQ and play 12 different sound files. Shall I split 1D array of waveforms and creat 12 FOR loops? As only one sound file path can be created in a FOR loop.

0 Kudos
Message 4 of 11
(1,686 Views)
Highlighted

@yatang wrote:

I'm gonna use 12 channels of DAQ and play 12 different sound files. Shall I split 1D array of waveforms and creat 12 FOR loops? As only one sound file path can be created in a FOR loop.


You only need 1 loop.  You could always make the file paths an array and use the same FOR loop.  Just move the array control to be outside of the FOR loop and let it autoindex so that each channel will only look at its designated file.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 11
(1,677 Views)
Highlighted

Hi Corssrulz,

 

So what I should do is right-click the path control, select Browse Options, and change the Selection Mode to Folders. Then if I run the program, the first channel of DAQ will be connected to the first file in the folder, and the second channel will be connected to the second file in the folder.

Am I right?

0 Kudos
Message 6 of 11
(1,664 Views)
Highlighted

It is not as simple as just selecting a folder.  You would need to do a list folder, sort the files in some way, and build up an array of paths from the folder list (Build Array inside of a FOR loop).  Preferably do all of that before your main loop.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 7 of 11
(1,658 Views)
Highlighted

Hi crossrulz,

 

I sort the files using List Folder and build an array of paths.

It works, thank you very much.

Piano 2.jpg

0 Kudos
Message 8 of 11
(1,610 Views)
Highlighted

@yatang wrote:

Hi crossrulz,

 

I sort the files using List Folder and build an array of paths.


To make it a little more efficient, move that FOR loop for building the full file paths to be outside of the While loop.  No point in constanatly calculating the same value.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 9 of 11
(1,607 Views)
Highlighted

Oh yes, I forgot it.

0 Kudos
Message 10 of 11
(1,604 Views)