Minimum and maximum values to make an action

Dear All, I have been reading about control structures such as continue, break, do while, while and so on. But I am not sure which one is the best for the following situation:

I would like to change status of one relay to LOW if a variable value 1 is reached and just change back to HIGH when the variable value 1 is the first value plus variable value 2.
For instance: Set LOW with 50 and HIGH with 70 (50+20) and only set LOW again when it will be 50.

Which Control Structure is the most appropriated to do that?
Could you give me a example?

Thanks all.

Reinaldo Lorenzato

You wish to control a pin, HIGH/LOW based on the value of a variable. Your two key set-points are 50 and 70 and you've identified what you want to do at each of those values. Now you need to state what will happen with all the other possible values that aren't 50 or 70. What about less than 50? Greater than 70? Between 50 and 70? Answer those questions and we have a solution.

For Instance:
0 to 49 HIGH
50 to 70 LOW
71 and up HIGH

OK, put all that in English and you are almost done. Example:

if x is greater than 49 and x is less than 71 then LOW becomes

if ( (x > 49) && (x < 71) ) digitalWrite(pin, LOW);

const byte Pin = 2;
int X = 0;

void setup()
{
  pinMode(Pin, OUTPUT);
}

void loop()
{
  X = millis() / 1000;  // X = run time in seconds
  digitalWrite(pin, X < 50 || X > 70);  // On for the first 50 seconds, off for the next 20, then on again
}

digitalWrite(pin, X < 50 || X > 70);Very clever!

jremington:
digitalWrite(pin, X < 50 || X > 70);Very clever!

I prefer your own version - no thinking needed to figure it out.

...R

Thanks all,

I asked about some other structures and I got simple and "very clever" other options.

I asked about some other structures

"if" is a fine structure, appropriate to the problem and solves the problem completely.

"continue", "break", "while", "do while" are not appropriate to the problem.

Well, there are lots of more complex ways to do it:

void loop()
{
  X = millis() / 1000;  // X = run time in seconds

  if (X < 50 || X > 70)
    digitalWrite(pin, HIGH);
  else
    digitalWrite(pin, LOW);
}
void loop()
{
  X = millis() / 1000;  // X = run time in seconds

  if ((X < 50 || X > 70) == HIGH)
    digitalWrite(pin, HIGH);

  if (( X < 50 || X > 70) == LOW)
    digitalWrite(pin, LOW);
}
void loop()
{
  X = millis() / 1000;  // X = run time in seconds

  bool state = false;  // Default to off

  if (X < 50)
    state = true;  // On if less than 50

  if (X > 70)
    state = true;  // On if greater than 70

  digitalWrite(pin, state);  // On for the first 50 seconds, off for the next 20, then on again
}
void loop()
{
  X = millis() / 1000;  // X = run time in seconds

  bool state = false;  // Default to off

  if (X < 50)
    state = true;  // On if less than 50

  if (X > 70)
    state = true;  // On if greater than 70

  if (state)
    digitalWrite(pin, HIGH);
  else
    digitalWrite(pin, LOW);
}

Guys,
Another challenging situation:

If var <=50 set pin value = LOW
between 50 and 70 still set pin value = LOW
If var > 70 set pin value = HIGH
but just set pin value LOW when var =50

I couldn't solve it.

Any tip?

Tks

You mean like this?!?

If (var <= 50) digitalWrite(pin, LOW);
if (val > 50 && val <= 70)  digitalWrite(pin, LOW);
if (var > 70) digitalWrite(pin, HIGH);
If (var == 50) digitalWrite(pin, LOW);

I don't understand what you are trying to accomplish.

If var <=50 set pin value = LOW
...
but just set pin value LOW when var =50

Same requirement. Please explain.

I couldn't solve it.

Post your attempt, using code tags.

Tks Guys,

But it will not work. I did the same.

Imagine vessel with a water pump with two levels min and max but not with two sensors, only working with variables.

If the vessel reach the low level or below, in our case 50 it must turn on (LOW) the pump and stay on up to the maximum level (70) and stops (HIGH).

But just turn on again when the low level is reached again.

In your (our) case at 69 it will turn on and 70 turn off and so on.

I am thinking to do something with two diffent variables max and min.

The trick is how to do something with the range 50 to 70 that sometimes works and sometimes not.

I am still challenging myself!!

Any tip is always appreciatted!!

if(val<50)
  TurnPumpOn();
if(val>=70)
  TurnPumpOff();

Not work Wildbill :frowning:

Between 51 and 69 it must be on as well, but whet it achieve 70 it must turn off and stay off up to reach 50 again or less.

Delay or millis coud do that but sometimes the time required is too long (minutes).

Maybe there is no way instead having two sensors (max and min).

I'm confused.

I thought you were trying to get some kind of hysteresis in place so that you wouldn't get the pump coming back on at 69 after you just hit 70 but only when it drops to 50.

I'd envisaged a tank of water that you want to refill when it gets low, but you don't want the pump going on & off all the time, hence you wait until the level is 50 before pumping.

With the code as suggested, the pump will start when the level falls below fifty and will stay on until it hits 70, thus staying on between 51 and 69.

Do you have some additional condition that the pump must be triggered if the level is below seventy when the system starts up, but once filled, it can only come on again once the level is below fifty?

rlorenzato:
Not work Wildbill :frowning:

Between 51 and 69 it must be on as well, but whet it achieve 70 it must turn off and stay off up to reach 50 again or less.

Their code is correct for what you say you want to do. You must have made a mistake putting the idea into your sketch. If you show us the sketch that doesn't work, we can tell you how to fix your mistake.

But it will not work. I did the same.

What part of "post your code" do you not understand?

I wil retry your code again on my scketch.
Just to confirm, no else command ir needed, correct?

Below are the code that works today (but it is not what we are discussing above) and that's what I want to improve.

if (MSAVG <=50){
digitalWrite(pinoRele, LOW);
terminal.println("SISTEMA ATIVO");
}else{ //SENÃO
digitalWrite(pinoRele, HIGH);
terminal.println("SISTEMA INATIVO");
delay(1000);
}

Tks for your attention John and Wildbill