MATLAB Answers

0

Creating while loop for control UNTIL condition is met

Asked by Jake Marquette on 2 Aug 2019
Latest activity Commented on by Jake Marquette on 2 Aug 2019
Hello -
I am in need of a while loop to act as a controller to keep a signal within a certain range. Basically I have a minimum and maximum value that I need an incoming signal to stay between cyclically. Here is the general process:
  • Increase value from 0 until the minimum is reached
  • One the minimum is reached, increase value until maximum is reached
  • Once maximum is reached, begin decreasing until minimum is reached again
  • Once minimum is reached, begin increasing till maximum. Repeat this cycle keeping the value between the min and max
This is a simplified version of my code so far
while true
if signal_value < min
% Increase from 0 until minimum
send increase.signal
elseif signal_value > min && signal_value < max
% Increase from minimum to maximum
send increase.signal
elseif app.ConvertedDataBuffer(end) >= app.highPressure
% Decrease once maximum is reached
send decrease.siganl
end
end
My code is getting caught up in the second elseif statement. Once it reaches the max and begins decreasing, it immediately begins increasing again. Is there a way I can decrease UNTIL the minimum is condition is met, and then increase again?

  3 Comments

Keep a current direction variable.
While Walter has the answer to the question, don't use min and max as variables (or constant stored in a variable) as they are Matlab builtin functions. Using them as you have above will alias the real function names making them unaccessible, not a good idea.
@Walter thank you, I was able to solve the issue using directional variables to further check.
@dpb, yes those aren't my real variable names I just used them to make the pseudocode easier to understand

Sign in to comment.

Products


Release

R2019a

0 Answers