How would you create a script that is executed more than once?

3 ビュー (過去 30 日間)
Anthony Coelho
Anthony Coelho 2021 年 4 月 30 日
コメント済み: Anthony Coelho 2021 年 4 月 30 日
My question is how would I go about adding to my script so that there are multiple executions or checks? My script will currently only carry out either the first part of the script, or the second part of the script based on distance taken from a sensor (It will also only take this measurement once), It will then stop.
if any(distance > 0.1) % If measured distance is greater than 0.1m, then..,
writeDigitalPin(a,'D10',1); % power digital pin D10,
else % else, execute the following for loop,
for PauseReverse = 0:0.5 % have 1 cycle only of for loop,
pause(5); % pause for 5 seconds before next step,
writeDigitalPin(a,'D11',1); % power on pin D11,
pause(10); % Keep power on for 10 seconds,
writeDigitalPin(a,'D11',0); %power off pin D11,
end % end the for loop,
end % end the if condition.
Prefferably, the initial if condition would be executed, followed by the script executing itself repeatedly until the measured distance is less than 0.1, where it would then carry out the final for loop and stop the script.
Im sorry if that didn't make much sense and I would be grateful for any responses, thank you.
  1 件のコメント
Anthony Coelho
Anthony Coelho 2021 年 4 月 30 日
David fletcher - "You will also need to update the distance within the while loop, otherwise the loop would never end (if it was initially entered). What is the value of distance returned from the sensor (your use of any(distance....) suggests that it's a vector rather than a scaler value?"
I did not update the distance measurement within the while loop, just like David mentioned, after rewriting it, it now works as intended (I should have also initially provided more information) - thanks for the help!

サインインしてコメントする。

採用された回答

David Fletcher
David Fletcher 2021 年 4 月 30 日
while any(distance > 0.1) % If measured distance is greater than 0.1m, then..,
writeDigitalPin(a,'D10',1); % power digital pin D10,
end % else, execute the following for loop,
for PauseReverse = 0:0.5 % have 1 cycle only of for loop,
pause(5); % pause for 5 seconds before next step,
writeDigitalPin(a,'D11',1); % power on pin D11,
pause(10); % Keep power on for 10 seconds,
writeDigitalPin(a,'D11',0); %power off pin D11,
end % end the for loop,
  4 件のコメント
Anthony Coelho
Anthony Coelho 2021 年 4 月 30 日
Sorry, this was where distance is updated, its a measurement taken from a ultrasonic sensor connected to a arduino;
clear;
a = arduino('COM3', 'Uno', 'Libraries', 'Ultrasonic'); % create arduino object and include ultrasonic library,
ultrasonicObj = ultrasonic(a,'D6','D5') % TriggerPin = D6, EchoPin = D5
distance = readDistance(ultrasonicObj) % measure distance from ultrasonic sensor to object and return the measured distance in meters,
% Digital pin 10 rotates the motor anti-clockwise,
% Digital pin 11 rotates the motor clockwise,
if any(distance > 0.1) % If measured distance is greater than 0.1m, then..,
writeDigitalPin(a,'D10',1); % power digital pin D10,
else % else, execute the following for loop,
for PauseReverse = 0:0.5 % have 1 cycle only of for loop,
pause(5); % pause for 5 seconds before next step,
writeDigitalPin(a,'D11',1); % power on pin D11,
pause(10); % Keep power on for 10 seconds,
writeDigitalPin(a,'D11',0); %power off pin D11,
end % end the for loop,
end % end the if condition.
David Fletcher
David Fletcher 2021 年 4 月 30 日
You will also need to update the distance within the while loop, otherwise the loop would never end (if it was initially entered). What is the value of distance returned from the sensor (your use of any(distance....) suggests that it's a vector rather than a scaler value?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArduino Hardware についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by