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?
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.
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
}
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);
}
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?
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.