Creating while loop for control UNTIL condition is met

4 Ansichten (letzte 30 Tage)
Jake Marquette
Jake Marquette am 2 Aug. 2019
Kommentiert: Jake Marquette am 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 Kommentare
dpb
dpb am 2 Aug. 2019
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.
Jake Marquette
Jake Marquette am 2 Aug. 2019
@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

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by