While Loop

Hey guys so I am trying to build a While Loop that will change one pin to HIGH while some sensor readings are false. What my question is, are the or’s in the parentheses going to work? I want the valvePin to be set on HIGH if any of the analog pin readings are below 400 (dryness). In other words will this code execute in the correct way… I cannot test because I have yet to build the the entire module.

Here is my code,

if (hr0 || hr1 || hr2 < dryness){

while (analogRead(hr0 || hr1 || hr2) < dryness) { //3 sensor readings

digitalWrite(valvePin, HIGH) //opens valve

}
digitalWrite(valvePin, LOW) //turns of valve once sensors are back to normal
}

What you are asking the Arduino to do is analogue read its choice of 3 inputs.

Try

While (analogRead(hr1) or analogRead(hr2) etc.

note I used "or" because I am using an iPad keyboard.

Weedpharma

Hi,
Welcome to the Forum

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Forget the while, use if … else…

Read the three values

if your three values are above the threshold
{
do this
}
else
{
do that
)

 (analogRead(hr0 || hr1 || hr2) < dryness)

Will not work, don’t try to be economical with how much code you use to do something.

// input values
read hr0
read hr1
read hr2

// level check
check if hr0 is < dryness
set or reset flag0
check if hr1 is < dryness
set or reset flag1
check if hr2 is < dryness
set or reset flag2

// flag check
if flag0 set OR flag1 set OR flag2 set
{
output HIGH
}
else
{
output LOW
}

This is just pseudo code, you will have to do the code formatting.

Tom… :slight_smile:

while (analogRead(hr0 || hr1 || hr2) < dryness)

Would be written as:

while (analogRead(hr0) < dryness || analogRead(hr1) < dryness || analogRead(hr2) < dryness){

TomGeorge:
Hi,
Welcome to the Forum

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Forget the while, use if … else…

Read the three values

if your three values are above the threshold
{
do this
}
else
{
do that
)

 (analogRead(hr0 || hr1 || hr2) < dryness)

Will not work, don’t try to be economical with how much code you use to do something.

// input values
read hr0
read hr1
read hr2

// level check
check if hr0 is < dryness
set or reset flag0
check if hr1 is < dryness
set or reset flag1
check if hr2 is < dryness
set or reset flag2

// flag check
if flag0 set OR flag1 set OR flag2 set
{
output HIGH
}
else
{
output LOW
}

This is just pseudo code, you will have to do the code formatting.

Tom… :slight_smile:

Everything you say makes sense except when you say to forget the while… I want the make the Arduino set a pin to high UNTIL the sensor readings are true how do I do that with an If Else.

the loop repeats over and over and over. So the scenario looks like this. (for whatever sensor value being true means)

void loop(){

     if(sensorValue is true){
           set the pin high
     else
           set the pin low

Now loop runs over and over repeating that.

the sensor is true, set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is true set the pin high
the sensor is false set the pin low

See how it stays high until the condition is false?