NI DAQ-Altering analog outputs based upon continuous background inputs

2 views (last 30 days)
I'm having trouble with the following:
  1. Input data on 4 channels for 10 seconds, and store this data in a text file
  2. Output 5 volts on the first output channel during the full 10 seconds
  3. Output 5 volts for 0.5 seconds after one of the inputs is over a threshold on the second channel
Setup:
s = daq.createSession('ni');
s.addAnalogInputChannel(d.ID, 0:4, 'Voltage');
s.addAnalogOutputChannel(d.ID,0:2, 'Voltage');
s.Rate = 1000;
s.DurationInSeconds = 10;
Add a listener to trigger the output required event:
lh = s.addlistener('DataRequired', @outputOn);
s.queueOutputData([data0 data1]);
s.startBackground;
The dataRequired function:
function outputOn(src, event, userInfo)
threshold = 1.7;
if any(event.Data > threshold) % How do I focus on just one of the channels?
data0 = piecewiseF(linspace(0, 10, 10000), userInfo)';
data1 = 5;
src.queueOutputData([data0, data1]); % Should I queue the data here or in the main code?
end
end
I have defined a piecewise function that mimics the output, but I'm not sure how to implement it either.
function y = piecewiseF(x, userInfo)
y = zeros(size(x));
region1 = x < 0.1; % First interval
y(region1) = 0;
region2 = (0.1 <= x) & (x <= 0.6); % Second interval
y(region2) = 5;
region3 = (0.6 < x); % Third interval
y(region3) = 0;
end

Answers (0)

Community Treasure Hunt

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

Start Hunting!