Clear Filters
Clear Filters

How to output analog signals after analog input voltage exceeds 5V?

2 views (last 30 days)
Hello everyone, I am currently using the analog input and analog output channels of NI DAQ devices with session-based interface. Now i want to output a pulse for 2 seconds whenever the PC detects a input voltage exceeding 5V, and then input restarts. Here's my code,
si=daq.createSession(ni’);
si.Rate=10;
si.DurationInSeconds=10;
si.addAnalogInputChannel('Dev1','ai0','Voltage');
si.Chennels.InputType='SingleEnded';
lh=addlistener(si.'DataAvailable',@pulseoutput);
si.IsContinuous=true;
si.startBackground();
function pulseoutput(hObject,eventdata)
if any(eventdata.Data>=5.0)
stop(hObject);
s=daq.createSession('ni');
addAnalogOutputChannel(s,'Dev',0,'Voltage');
t=0:0.001:2;
w=4;
outputSignal=rectpuls(t,w)';
queueOutputData(s,outputSignal);
s.startForeground;
rerun();
end
end
function rerun()
si=daq.createSession(ni’);
si.Rate=10;
si.DurationInSeconds=10;
si.addAnalogInputChannel('Dev1','ai0','Voltage');
si.Chennels.InputType='SingleEnded';
lh=addlistener(si.'DataAvailable',@pulseoutput);
si.IsContinuous=true;
si.startBackground();
end
Whenever voltage exceeding 5V is detected, a pulse will be generated successfully, but the analog input will not restart again and error message appears'Timeout expired before operation could complete'. Any suggestions on how to correct the codes? Thank you in advance.

Answers (0)

Categories

Find more on Analog Input and Output in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!