two while loops running at same time

56 Ansichten (letzte 30 Tage)
Payaam Khalid
Payaam Khalid am 6 Dez. 2022
Kommentiert: Walter Roberson am 6 Dez. 2022
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

Antworten (2)

Les Beckham
Les Beckham am 6 Dez. 2022
Bearbeitet: Les Beckham am 6 Dez. 2022
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 Kommentare
Les Beckham
Les Beckham am 6 Dez. 2022
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 am 6 Dez. 2022
Well, background pool exists these days. Using it (or Parallel Computing Toolbox) is probably not a good design, but it could be done.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 6 Dez. 2022
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.)

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by