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

1 view (last 30 days)
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

Answers (1)

Voss
Voss on 30 Apr 2022
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

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!