Thread Subject: Trouble running MATLAB code

Subject: Trouble running MATLAB code

From: Matthew Fung

Date: 5 Oct, 2008 00:26:02

Message: 1 of 5

Hi,

I'm having trouble with my code.
This is what my code does: It detects analog inputs from my DATA acquisition device (USB-1208FS), which in this case is some voltage readings, and then evaluates and outputs a specific voltage. I keep getting the error "Invalid Count" when I run the program.

Here's the code:
%Create an analog input "ai" and analog output "ao" with ID 0
ai = analoginput('mcc',0);
addchannel(ai,0:1);
ao = analogoutput('mcc',0);
addchannel(ao,0:1);


%Define duration to every 1 ms
duration = 0.001;
%Set the sample rate to 1000 Hz
set(ai,'SampleRate',1000)
%Acquire rate from the input
ActualRate = get(ai,'SampleRate');
%Define the amount of samples to acquire PER trigger
set(ai,'SamplesPerTrigger',ActualRate*duration)

%Set the Trigger to repeat an infinite number of times
set(ai,'TriggerRepeat',0);
set(ao,'RepeatOutput',0);


control_variable = 1;

while( control_variable==1 )

start(ai);
wait(ai,1);
    
    vin = peekdata(ai,1);

    if (vin(1,1)>=3.00 && vin(1,1)<=3.19)
    putdata(ao,[0.40 0.00]);
        
    elseif (vin(1,1)>=3.20 && vin(1,1)<=3.39)
    putdata(ao,[0.80 0.00]);
            
    elseif (vin(1,1)>=3.40 && vin(1,1)<=3.59)
    putdata(ao,[1.20 0.00]);
            
    elseif (vin(1,1)>=3.60 && vin(1,1)<=3.79)
    putdata(ao,[1.50 0.00]);
            
    elseif (vin(1,1)>=3.80 && vin(1,1)<=3.99)
    putdata(ao,[1.80 0.00]);
            
    elseif (vin(1,1)>=4.00 && vin(1,1)<=4.19)
    putdata(ao,[2.10 0.00]);
            
    elseif (vin(1,1)>=4.20 && vin(1,1)<=4.39)
    putdata(ao,[2.50 0.00]);
            
    elseif (vin(1,1)>=4.40 && vin(1,1)<=4.59)
    putdata(ao,[2.90 0.00]);
        
    elseif (vin(1,1)>=4.60 && vin(1,1)<=4.79)
    putdata(ao,[3.30 0.00]);
        
    elseif (vin(1,1)>=4.80 && vin(1,1)<=5.00)
    putdata(ao,[4.00 0.00]);
            
    else
    putdata(ao,[0.00 0.00]);
    
    end;
            
    if (vin(1,2)>=3.00 && vin(1,2)<=3.19)
    putdata(ao,[0.00 0.40]);
        
    elseif (vin(1,2)>=3.20 && vin(1,2)<=3.39)
    putdata(ao,[0.00 0.80]);
            
    elseif (vin(1,2)>=3.40 && vin(1,2)<=3.59)
    putdata(ao,[0.00 1.20]);
            
    elseif (vin(1,2)>=3.60 && vin(1,2)<=3.79)
    putdata(ao,[0.00 1.50]);
            
    elseif (vin(1,2)>=3.80 && vin(1,2)<=3.99)
    putdata(ao,[0.00 1.80]);
            
    elseif (vin(1,2)>=4.00 && vin(1,2)<=4.19)
    putdata(ao,[0.00 2.10]);
            
    elseif (vin(1,2)>=4.20 && vin(1,2)<=4.39)
    putdata(ao,[0.00 2.50]);
            
    elseif (vin(1,2)>=4.40 && vin(1,2)<=4.59)
    putdata(ao,[0.00 2.90]);
        
    elseif (vin(1,2)>=4.60 && vin(1,2)<=4.79)
    putdata(ao,[0.00 3.30]);
        
    elseif (vin(1,2)>=4.80 && vin(1,2)<=5.00)
    putdata(ao,[0.00 4.00]);
            
    else
    putdata(ao,[0.00 0.00]);
    
    end;
    
stop(ai);

start(ao);
pause(0.001);
stop(ao);

end;

stop(ai);
delete(ai);


Any help will be appreciated. Btw, is there an easier way to make the if and elseif statements without writing all that in?

Thanks

Subject: Trouble running MATLAB code

From: Sherryl Radbil

Date: 6 Oct, 2008 13:00:22

Message: 2 of 5

Hi Matthew,
Please provide the exact and entire error message you get and also which line you get it on.

This sounds like an error we've seen returned by the MCC driver but it is usually associated with trying to add more channels to your object than exist, so it's surprising that you're seeing it.

It's unclear how this program works since TriggerRepeat and RepeatOutput are being set to zero. Can you explain?

Sherryl

Subject: Trouble running MATLAB code

From: Matthew Fung

Date: 6 Oct, 2008 23:25:08

Message: 3 of 5

"Sherryl Radbil" <sherryl.radbil.dontspamme@mathworks.com> wrote in message <gcd256$7b0$1@fred.mathworks.com>...
> Hi Matthew,
> Please provide the exact and entire error message you get and also which line you get it on.
>
> This sounds like an error we've seen returned by the MCC driver but it is usually associated with trying to add more channels to your object than exist, so it's surprising that you're seeing it.
>
> It's unclear how this program works since TriggerRepeat and RepeatOutput are being set to zero. Can you explain?
>
> Sherryl
>

Hi Sherryl. Thank you so much for replying. This is a more simplified version of the code. The TriggerRepeat was a typo and should have been infinite. In short, I have set up an infinite loop to acquire voltage inputs from a strain gauge.

Unfortunately, the daq does not allow simultaneous input and output, so what I have created is a loop that turns off output everytime I need input and vice versa.

In this version, the daq will simply output whatever the input voltage is.

ie. vin = vout

Here is the code:

ai = analoginput('mcc',0);
addchannel(ai,0:1);
ao = analogoutput('mcc',0);
addchannel(ao,0:1);

duration = 0.001;
set(ai,'SampleRate',1000)
ActualRate = get(ai,'SampleRate');
set(ai,'SamplesPerTrigger',ActualRate*duration)

set(ai,'TriggerRepeat',inf);
set(ao,'RepeatOutput',0);

control_variable = 1;

while( control_variable==1 )

start(ai);
pause(0.1);
    
vin = peekdata(ai,1);

putdata(ao,[vin(1,1) vin(1,2)]);
    
stop(ai);

start(ao);
pause(0.1);
stop(ao);

clear(ai);
clear(ao);

end;

stop(ai);
delete(ai);

Subject: Trouble running MATLAB code

From: Matthew Fung

Date: 6 Oct, 2008 23:31:09

Message: 4 of 5

I have debugged the entire program up until the start(ao) statement. Everything including the input statements seem to be in working order until it reaches the start statement.

The exact error that I receive is MCC: Invalid Count. I also believe it is coming from the driver.

Thanks again.

Subject: Trouble running MATLAB code

From: Matthew Fung

Date: 6 Oct, 2008 23:33:32

Message: 5 of 5

PS The error occurs on line 27.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com