How can i break out of my while loop

4 views (last 30 days)
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

Image Analyst
Image Analyst on 3 Aug 2014
Edited: Image Analyst on 3 Aug 2014
Put this where you want to start timing:
tStart = tic;
Put this in the loop where you want to check how much time has elapsed, and break out of the loop if it has been more than 3 minutes:
tElapsed = toc(tStart); % Answer in seconds.
if tElapsed > 3*60 % 3 minutes
break;
end
  3 Comments
Image Analyst
Image Analyst on 3 Aug 2014
Edited: Image Analyst on 3 Aug 2014
So set two timers going, each starts when the state goes to 0 for that variable. Then get the min elapsed time:
if sIO1 == 0 && io0 == 0
tElapsed = toc(max(t1, t2));
end
[EDITED] to get the min elapsed time you need to get the max starting time, not the min like I had it.
dpb
dpb on 3 Aug 2014
OK, I'm bored this AM... :) Despite my previous comment here's another demo for OP of the two timer solution--
>> ts=tic; % set the "go" time -- here I used the same start
while 1
t(1)=toc(ts);
pause(10*rand)
t(2)=toc(ts);
disp(t)
if min(t)>15, break, end
end
0.0004 8.1488
8.1498 17.2116
17.2118 18.4929
>>

Sign in to comment.

More Answers (1)

dpb
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
Tochukwu on 3 Aug 2014
Thanks for the reply but I wanted the loop to break only if the both switches IO0 and IO1 have a state of 0 for 3mins, this is so if the switches are not operated the loop can stop after 3mins to display ('System error switches not detected') thanks
dpb
dpb on 3 Aug 2014
Edited: dpb 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...

Sign in to comment.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!