two while loops running at same time

54 ビュー (過去 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 Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by