Problem with my code...

Hi this is my first time posting on here and im pretty new to arduinos so sorry if its in the wrong place…

Im working on a project where a soil moisture sensor turns on valve to water plants… but i want to add a light sensor so it only works in the daytime.

Im having a problem with getting two if statements to work with one thing…

if ( percentage < 75 && (Light >100 ) )
{
digitalWrite(7, HIGH); // WATER ON
}
else if (percentage > 75 && (Light <100 ) )
{
digitalWrite(7, LOW); // WATER OFF
}

Heres the code i’ve made, it is reading the data and will work to turn on the water when there is enough light, but then when i cover the light sensor it doesnt turn off.

Im wondering what i need to do to get it working?

Thanks in advance if anyone can help

charlie

When debugging if/else it is helpful to print the values being tested before testing them to see if they are what you think

Please post your complete program as advised in Read this before posting a programming question so that we can see the context. For instance, where is the value of percentage set, noting that the test of its value is inverted between the if and the else

Please post the complete program.

I think the logic will be easier to manage if you separate the light from the moisture - something like this pseudo code

read the brightness sensor
read the moisture sensor

if it is bright
  daytime = true
else
  daytime = false

if (daytime == true) {
   if it is too dry
      turn pump on
   else
     turn pump off
}

...R

Sorry that was stupid of me, heres the complete code… but it think its just the if section that iv got wrong.

Thanks so much for your replys already

#define SensorPin A0

float sensorValue = 0;
int percentage;
const int Psensor = A1;

int map_low = 400;
int map_high = 210;
int Light;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(7, OUTPUT);//watering
pinMode(Psensor, INPUT);
}

void loop() {

{sensorValue = analogRead(SensorPin);
Serial.println(sensorValue);
percentage = map(sensorValue, map_low, map_high, 0, 100);

Serial.print(" | percentage: “);
Serial.print(percentage);
Serial.println(”%");}

analogRead(Light);
Serial.println(Light);
Light = analogRead(Psensor);
Serial.println(Light);
delay(1000);
{{

if ( percentage < 75 && (Light >100 ) )
{
digitalWrite(7, HIGH); // WATER ON
}
else if (percentage > 75 && (Light <100 ) )
{
digitalWrite(7, LOW); // WATER OFF
}

//if ( percentage > 75 && (Light <100 ) )
// {
// digitalWrite(7, LOW); //

}}}

heres the complete code

I see that you ignored the advice I linked to

}}}

This formatting is particularly nasty

In your code sample, what happens if percentage is exactly 75 or Light is exactly 100 ? You could use >= or <= but Robin’s approach would be a neater way to deal with that problem. That way, if, say, the statement if (Light>100) evaluates to false, then by implication the ‚else’ part must be true. This leaves no room for ambiguity.

{{is equally nasty, and unnecessary

#define SensorPin A0

float sensorValue = 0;
int percentage;
const int Psensor = A1;

int map_low = 400;
int map_high = 210;
int Light;

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(7, OUTPUT);//watering
pinMode(Psensor, INPUT);
}

void loop() {


{sensorValue = analogRead(SensorPin);
Serial.println(sensorValue);
percentage = map(sensorValue, map_low, map_high, 0, 100);

Serial.print(" | percentage: ");
Serial.print(percentage);
Serial.println("%");}

analogRead(Light);
Serial.println(Light);
Light = analogRead(Psensor);
Serial.println(Light);
delay(1000);
{{




if ( percentage < 75 && (Light >100 ) )
   {
     digitalWrite(7, HIGH); // WATER ON
  }
  else if (percentage > 75 && (Light <100 ) )
  {
  digitalWrite(7, LOW); // WATER OFF
  }
 
//if ( percentage > 75 && (Light <100 ) )
 //  {
//  digitalWrite(7, LOW); //




}}}

Im Sorry i didn’t read all the way down the link, is this how i should send it?

I apreciate your fast responce.

I just tried what Robin sugested and Wrote this but it think its not quite correct, What am i missing?

{

if (Light > 100) daytime = true
else
 daytime = false

if (daytime == true) {
  if (percentage < 75)
     digitalWrite(7,HIGH);
  else
    digitalWrite(7,LOW);

}

Thanks again

I just tried what Robin sugested and Wrote this

But decided not to use code tags when posting for some reason, nor posting the complete program

#define SensorPin A0

float sensorValue = 0;
int percentage;
const int Psensor = A1;

int map_low = 400;
int map_high = 210;
int Light;
int daytime;
void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(7, OUTPUT);//watering
pinMode(Psensor, INPUT);
}

void loop() {


{sensorValue = analogRead(SensorPin);
Serial.println(sensorValue);
percentage = map(sensorValue, map_low, map_high, 0, 100);

Serial.print(" | percentage: ");
Serial.print(percentage);
Serial.println("%");}

analogRead(Light); 
Serial.println(Light);
Light = analogRead(Psensor);
Serial.println(Light);
delay(1000);
{

if (Light > 100);
daytime = true
else
  daytime = false;

if (daytime == true) {
   if (percentage < 75)
      digitalWrite(7,HIGH);
   else
     digitalWrite(7,LOW);
}






}}

Im new to this, really sorry for my ignorance, im not trying to be rude

You should never have a semicolon at the end of an “if”

daytime = Light > 100;

No need for the “if”

#define SensorPin A0

float sensorValue = 0;
int percentage;
const int Psensor = A1;

int map_low = 400;
int map_high = 210;
int Light;
int daytime;
void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(7, OUTPUT);//watering
pinMode(Psensor, INPUT);
}

void loop() {


{sensorValue = analogRead(SensorPin);
Serial.println(sensorValue);
percentage = map(sensorValue, map_low, map_high, 0, 100);

Serial.print(" | percentage: ");
Serial.print(percentage);
Serial.println("%");}

analogRead(Light); 
Serial.println(Light);
Light = analogRead(Psensor);
Serial.println(Light);
delay(1000);
{


 daytime = Light > 100;

if (daytime == true) {
   if (percentage < 75)
      digitalWrite(7,HIGH);
   else
     digitalWrite(7,LOW);
}


}}

Thanks just added that and it runs, the system is reacting to the change in light and turning on, but when i cover the sensor it still isnt turning off, to test im using an led instead of the watering system. As for the soil moisture sensor its currently reading 17% so shouldnt be effecting anything as its threshold is 75%

thanks again

analogRead(Light);

What is this supposed to do?

Steve

#define SensorPin A0

float sensorValue = 0;
int percentage;
const int Psensor = A1;

int map_low = 400;
int map_high = 210;
int Light;
int daytime;
void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(7, OUTPUT);//watering
pinMode(Psensor, INPUT);
}

void loop() {


{sensorValue = analogRead(SensorPin);
Serial.println(sensorValue);
percentage = map(sensorValue, map_low, map_high, 0, 100);

Serial.print(" | percentage: ");
Serial.print(percentage);
Serial.println("%");}


Serial.println(Light);
Light = analogRead(Psensor);
Serial.println(Light);
delay(1000);
{


 daytime = Light > 100;

if (daytime == true) {
   if (percentage < 75)
      digitalWrite(7,HIGH);
   else
   if (daytime == false) {
   if (percentage > 75)
     digitalWrite(7,LOW);
}




}}}

Not sure… thought i needed it, just removed it and also added the else statement i think i need?

   if (daytime == true)
    {
      if (percentage < 75)
        digitalWrite(7, HIGH);
      else if (daytime == false)
      {
        if (percentage > 75)
          digitalWrite(7, LOW);
      }
    }

You test whether daytime is true then in the associated code block you test whether daytime is false.

I suspect that you meant

  if (daytime == true)
  {
    if (percentage < 75)
    {
      digitalWrite(7, HIGH);
    }
  }
  else if (daytime == false)
  {
    if (percentage > 75)
    {
      digitalWrite(7, LOW);
    }
  }

Note the use of { and } around dependant code blocks even when they are single lines. This and Auto format in the IDE makes the code logic easier to see

With 2 parameters there are 4 possible combinations. I am not clear what you are trying to do but you may need to test all of them

Please explain in English what you are trying to do

UKHeliBob:
I suspect that you meant

I don't think that is correct either because it could only turn the pump off when it is dark

@charlieashman, have another look at Reply #2. I think you are making things unnecessarily complicated.

Or else you have not described the requirement fully. I have been relying on this from your Original Post " but i want to add a light sensor so it only works in the daytime."

...R

#define SensorPin A0

float sensorValue = 0;
int percentage;
const int Psensor = A1;

int map_low = 400;
int map_high = 210;
int Light;
int daytime;
int wet;
void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(7, OUTPUT);//watering
pinMode(Psensor, INPUT);
}

void loop() {


{sensorValue = analogRead(SensorPin);
Serial.println(sensorValue);
percentage = map(sensorValue, map_low, map_high, 0, 100);

Serial.print(" | percentage: ");
Serial.print(percentage);
Serial.println("%");}

//analogRead(Light); 
Serial.println(Light);
Light = analogRead(Psensor);
//Serial.println(Light);
delay(1000);
{
wet= percentage < 75;

 daytime = Light > 100;
 

      digitalWrite(7, HIGH);
  
  

  if (daytime == false)

      digitalWrite(7, LOW);
    }

if (wet == false)
      digitalWrite(7, LOW);

  
}

Hi Robin, you’re reply was very useful thanks.

i think iv finally got it working

it is now turning on and off as i turn a light on and off, i just need to test that if the soil moisture percentage goes over 75% it will also still turn off.

PS. i know the code is very messy and horrible but its the best i can do, il try to sort it out a bit…

edit, just tested the moisture sensor aswell, its all working as it shoudl. thanks everyone for the help

charlieashman:
[i think iv finally got it working

It still looks messy to me. I would do it like this

    wet = percentage < 75;
    daytime = Light > 100;
    
    if (dayTime == true) {
        if (wet == false) {
            digitalWrite(7, HIGH);
        }
        else {
            digitalWrite(7, LOW);
        }
    }

    else { // make sure the pump is off at night, even if it is dry
        digitalWrite(7, LOW);
    }

Code should be written so you can easily understand it 6 months later :slight_smile:

I think it would also be useful to allow for a deadband between wet and dry and between daylight and dark as illustrated like this. Known as hysteresis - it stops the system hunting between the two states. (By the way I may have the numbers wrong, but you should get the idea)

if (percentage < 75) {
   wet = false;
}
if (percentage > 85)  {
  wet = true;
}

…R