if and then statements

hi
i have this code for watering system

if ((output_value < 40)&&(output_value > 70))

which i have looked up on the forum
it would like to look at sensor and if its 40 to turn valve on which it does and then turns off again ,i would like it to keep on untill sensor is 70 ,not sure if this code would do that, i think it will but then if it reaches 70 i dont want it to come back on again untill its at 40 again thats the problem im having,basically it will give the plants a good soaking and then not turn back on until they are dry again

int sensor_pin = A0 ;

int output_value = 0;//changed

int valve_pin = 8 ;//relay

void setup ( ) {
 Serial.begin(9600);//changed

 Serial.print ("Reading From the " );
 Serial.print (" Moisture = ");
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);

lcd.print ("Reading From the " );
lcd.setCursor(0, 1);


pinMode (valve_pin, OUTPUT);

delay (2000);

}


void loop ( ) {

output_value = analogRead (sensor_pin);

output_value = map (output_value , 1023 ,0 , 0 , 170);

 


lcd.clear ( );

lcd.setCursor (0, 0);

lcd.print (" Moisture = ");

lcd.print (output_value);

lcd.setCursor (0, 1);

//if (output_value < 40)//changed
if ((output_value < 40)&&(output_value > 70))//changed
{

lcd.print (" Valve: ON ");

   digitalWrite(6, HIGH); //green
digitalWrite(7, LOW); //red

digitalWrite (valve_pin , LOW);    //Relay operates on opposite signal

delay (1000);

}

else

{
if (output_value > 70)//changed
lcd.print (" Valve: OFF ");

   digitalWrite(6, LOW); //green
digitalWrite(7, HIGH); //red

digitalWrite (valve_pin , HIGH);    //Relay operates on opposite signal

delay (1000);

}

delay(1000);

}

i had thought maybe about having a variable for a low_ output_value =40 and a variable for high_output_value =70 still not sure if this will work ,ive done bit of programming over the years with if then else statements but pretty lost with arduino ide any help would be most welcome
colin

if ((output_value < 40)&&(output_value > 70))

How is any value going to be both less than 40 and greater than 70 at the same time ?

Did you perhaps mean

if ((output_value < 40) || (output_value > 70))  //smaller than 40 OR greater than 70

hi
thankyou for reply
the value will always be above or equal to 40 when it will turn valve on until it reaches 70 then it will turn off but i want it to drop to 40 again before it turns back on hope that helps a bit
or i have just thought would an easier way be to put a delay of say 20 secounds on my original code then turn the valve off
colin

the value will always be above or equal to 40 when it will turn valve on until it reaches 70 then it will turn off but i want it to drop to 40 again before it turns back on

What was wrong with your original formulation?

i

if (output_value <= 40) turn valve on
if (output_value >= 70)  turn valve off

hi
many thanks for that now im looking at it it looks right to me
will give it a go, just nice to get a bit of help when you really need it
thanks colin

hi
forgot to say i have it up and running in garden but wanted to improve code to do what i wanted so will make a mock up in the house to make sure it works as i want it to and then take computor into garden and upload to original as i have not tried the piece of code id posted did not want to mess original one up
thanks colin