two while loops running at same time

19 views (last 30 days)
Payaam Khalid
Payaam Khalid on 6 Dec 2022
Commented: Walter Roberson on 6 Dec 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

Answers (2)

Les Beckham
Les Beckham on 6 Dec 2022
Edited: Les Beckham on 6 Dec 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 Comments
Walter Roberson
Walter Roberson on 6 Dec 2022
Well, background pool exists these days. Using it (or Parallel Computing Toolbox) is probably not a good design, but it could be done.

Sign in to comment.


Walter Roberson
Walter Roberson on 6 Dec 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.)

Categories

Find more on Graphics Performance 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!