How can i break out of my while loop
Show older comments
Hi i have written a chunk of code, to send out 5v and 0v respectively from my labjack using Matlab and this is subject to the state of two switches either on or off. I put the code in a continuous while loop to keep monitoring the state of the switches, and to execute a command if any of the switches go high. But i want to be be able to breakout of the loop and display ( System error, can't detect sensors) if the while loop has run for 3mins, so the while loop doesn't just run forever. How can i achieve this. Below is my code
ljAsm = NET.addAssembly('C:\Program Files (x86)\LabJackU12Legacy\drivers\LJDotNet.dll');
idnum = -1; %Using first found U12
demo = 0; %Normal operations
errorString = NET.createArray('System.Char', 50); %Empty string for error message
disp(['Driver version: ', num2str(lj.LabJack.GetDriverVersion())]); %Display Labjack version
while 1 %Run continuous while loop
%Setting analog output
analogOut0 = 5.0;
analogOut1 = 5.0;
%Reading Reed switch from Digital Input IO0
channel = 0; %IO0
readD = 0; %Read IO line (>0 for D line)
state = 0; %Returned state (>0 = high, 0 = low)
[errorCode, idnum, state] = lj.LabJack.EDigitalIn(idnum, demo, channel, readD, state);
if state<0
state=0
elseif state>0
[errorCode, idnum] = lj.LabJack.EAnalogOut(idnum, demo, analogOut0, analogOut1);%Set both AO0 and AO1 to 5V
end
disp(['IO' num2str(channel) 'state' num2str(state)])%Display the state of Reed Switch connected to IO0
%Setting analog Output
analogOut0 = 0.0;
analogOut1 = 0.0;
%Reading Reed switch from Digital Input IO1
channel = 1; %IO0
readD = 0; %Read IO line (>0 for D line)
state = 0; %Returned state (>0 = high, 0 = low)
[errorCode, idnum, state] = lj.LabJack.EDigitalIn(idnum, demo, channel, readD, state);
if state<0
state=0
elseif state>0
[errorCode, idnum] = lj.LabJack.EAnalogOut(idnum, demo, analogOut0, analogOut1);%Set both AO0 and AO1 to 0V
end
disp(['IO' num2str(channel) 'state' num2str(state)])% Display the state of Reed Switch connected to IO1
end
pause(3.0)%Pause for three seconds before next scan
Accepted Answer
More Answers (1)
dpb
on 3 Aug 2014
Try the following demonstration...
t3min=3/60/24;
tstop=now+t3min;
while now<tstop
disp(datestr(clock))
pause(15)
end
2 Comments
Tochukwu
on 3 Aug 2014
...I wanted the loop to break only if the both switches IO0 and IO1 have a state of 0 for 3mins...
But you didn't say that, did you? :)
IA's two timer solution is about as clean as it gets. I could write the same thing in a different syntax, but no real point in it...
Categories
Find more on Parallel for-Loops (parfor) 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!