two while loops running at same time

조회 수: 17 (최근 30일)
Payaam Khalid
Payaam Khalid 2022년 12월 6일
댓글: Walter Roberson 2022년 12월 6일
while ~stop
% Read current voltage value
x = readVoltage(a,'A0');
% Get current time
t = datetime('now') - startTime;
% Add points to animation
addpoints(h,datenum(t),x)
% Update axes
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
% Check stop condition (check the button on D6)
stop = readDigitalPin(a,'D6');
end
while x>=2.9
if x>=3.3
disp('signal recevied soil is dry time to water')
writeDigitalPin(a,'D2',1);pause(5);writeDigitalPin(a,'D2',0);
x=x-0.5;
elseif x>=2.9
disp('signal received soil is wet but not wet enough time to water')
writeDigitalPin(a,'D2',1);pause(5);writeDigitalPin(a,'D2',0);
x=x-0.5;
end
pause(20)
end
I have these 2 while loops i want to run them at the same time independent of eachother how would i do that the first while loop is to graph an animated line of a moisture sensors values the second is to check the value of a moisture sensor

답변 (2개)

Les Beckham
Les Beckham 2022년 12월 6일
편집: Les Beckham 2022년 12월 6일
You can't code them as separate loops and have them run "at the same time". Depending on how you want the timing to work, you should do something like this using only one loop (assuming you want to check your moisture sensor and update your plot every twenty seconds and stop the loop completely on the "stop condition"):
while 1
% Read current voltage value
x = readVoltage(a,'A0');
% Get current time
t = datetime('now') - startTime;
% Add points to animation
addpoints(h,datenum(t),x)
% Update axes
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
% Check stop condition (check the button on D6)
stop = readDigitalPin(a,'D6');
if stop
break; % break out of the loop
end
if x>=3.3
disp('signal recevied soil is dry - time to water')
writeDigitalPin(a,'D2',1);pause(5);writeDigitalPin(a,'D2',0);
x = x-0.5; % << why are you doing this?
elseif x>=2.9
disp('signal received soil is wet but not wet enough - time to water')
writeDigitalPin(a,'D2',1);pause(5);writeDigitalPin(a,'D2',0);
x=x-0.5; % << why are you doing this?
end
pause(20)
end
I don't know why you have the x=x-0.5; lines. If you want the plot to reflect that offset, move the if/else block above the addpoints line or just subtract off the offset when you read the analog input instead.
  댓글 수: 3
Les Beckham
Les Beckham 2022년 12월 6일
Good point, Walter.
I should have qualified that with "(unless you have the Parallel Computing Toolbox and are willing to make your code much more complicated)".
Walter Roberson
Walter Roberson 2022년 12월 6일
Well, background pool exists these days. Using it (or Parallel Computing Toolbox) is probably not a good design, but it could be done.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2022년 12월 6일
In order to run two loops independently of each other, you will need to use one of:
The loops will not be able to communicate directly with each other unless you use spmd. There are things you can do with parallel.pool.DataQueue to have the controlling thread relay data between workers. Also, you would need to test whether the arduino object gets shared between the workers (it might not.)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by