フィルターのクリア

for loop help (turn on at a point and turn off when it drops to another point)

1 回表示 (過去 30 日間)
Aaron LaBrash
Aaron LaBrash 2022 年 4 月 30 日
回答済み: Voss 2022 年 4 月 30 日
I need to make a for loop for a pump to turn on when a tank is 85% full, and then turn off when it gets down to 20%, but I can't figure out how to write this. Any help would be greatly appreciated.
so far I had this, with v being volume, a1 being 85% full, and a2 being 20% full. with how it is now, it will only be on if it is above 85%
if v>=(a1);
a = 1;
elseif v<=(a2);
a = 0;
else a = 0;
end

回答 (1 件)

Voss
Voss 2022 年 4 月 30 日
If I understand the situation, your code needs to have three outcomes: (1) turn the pump on, (2) turn the pump off, (3) leave the pump alone. When the tank is between 20% and 85% full, the pump may be on or off, but it should be left on if it's already on or left off if it's off.
So you'll need more than a=0 and a=1; perhaps another state a=2 that says to leave the pump alone. Or maybe more clear:
if v >= a1
action = 'turn pump on';
elseif v <= a2
action = 'turn pump off';
else
action = 'do nothing';
end
Then if there's code that interacts with the pump, it may need to know whether the pump is already on:
is_pump_on = false;
if is_pump_on && strcmp(action,'turn pump off')
% take the action of turning the pump off
elseif ~is_pump_on && strcmp(action,'turn pump on')
% take the action of turning the pump on
end

カテゴリ

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

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by