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 CenterFile Exchange에서 Sources에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by