Pages: [1]   Go Down
Author Topic: doesn't go throw the whole condition  (Read 885 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have if condition it read data and data2 from analogRead when one of them is higher than voltage then it will change the digital pin from high to low, but i steams doesn't go throw for loop inside if condition and it just stop whenever data is higher than voltage.
also, is there a way that i can just print to lcd temp and humidity without going throw the if condition, because i have more than one if statement, here it wait for for loop and delay 10 sec to get updated to lcd.
Code:
if (data < voltage && data2 < voltage){
    digitalWrite(pump, HIGH);
    digitalWrite(valve, HIGH);
    digitalWrite(valve2, HIGH);
      for(int i=0; i<10; i++){
        lcd.setCursor(0,3);
        lcd.print("Temp:");
        lcd.print((long)temperature, DEC);
        lcd.setCursor(8,3);
        lcd.print(" Humidity:");
        lcd.print((long)humidity, DEC);
        lcd.setCursor(0, 2);
        lcd.print("S_1:");
        if (data > 260 && data < 295)
        lcd.print("MID");
        else if (data < 260)
        lcd.print("LOW");
        else
        lcd.print("HIGH");
        lcd.setCursor(7,0);
        lcd.print("Pump:ON");
        lcd.setCursor(0,1);
        lcd.print("V1:ON");
        lcd.setCursor(9, 2);
        lcd.print("S_2:");
        if (data2 > 260 && data2 < 295)
        lcd.print("MID");
        else if (data2 < 260)
        lcd.print("LOW");
        else
        lcd.print("HIGH");
        lcd.setCursor(7,1);
        lcd.print("V2:ON");
        lcd.setCursor(14,1);
        lcd.print("V3:OFF");
       sum += data;
       sum2+= data2;
       delay(1000);
     }
     average = sum/10;
     average2 = sum2/10;
     if(average > voltage || average2 > voltage){
    digitalWrite(pump, LOW);
    digitalWrite(valve, LOW);
    digitalWrite(valve2, LOW);
    }
    sum = 0;
    sum2 = 0;
    lcd.clear();
  }

i have attached the full codes.

* finally.pde (10.39 KB - downloaded 8 times.)
« Last Edit: March 20, 2011, 08:44:21 pm by latif » Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please post your whole code, as I can only say that you should print those values outside your if statement now.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

UK
Offline Offline
Full Member
***
Karma: 2
Posts: 110
Kittens eat Arduinos
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have posted raw code.  No comments to communicate the intent.

So when I read it - the result is that I pretty much agree with the complier.  Where as you clearly are not agreeing with the compiler.

To fix this, please add comments to the code that explain what you mean to achieve.
Since you are having problems with the if/else clauses, comment the intent of these, and also comment the code that you want to always run.

This will help others review your code, and from my experience, will help you (the author) clarify what you expect the code to do, perhaps leading you to find your own problem..
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is the file

* finally.pde (10.39 KB - downloaded 10 times.)
« Last Edit: March 20, 2011, 05:26:58 pm by latif » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
lcd.print("Starting.");
  delay(1300);
  lcd.setCursor(9,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(10,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(11,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(12,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(13,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(14,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(15,1);
  lcd.print(".");
  delay(1300);
You really need to get introduced to the 'for' loop.

The IDE includes a useful tool that auto-formats your code for you.
It is really useful for code visualisation.
« Last Edit: March 20, 2011, 04:51:14 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
lcd.print("Starting.");
  delay(1300);
  lcd.setCursor(9,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(10,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(11,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(12,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(13,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(14,1);
  lcd.print(".");
  delay(1300);
  lcd.setCursor(15,1);
  lcd.print(".");
  delay(1300);
You really need to get introduced to the 'for' loop.

The IDE includes a useful tool that auto-formats your code for you.
It is really useful for code visualisation.
i totally forgot about it! just fixed it.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3700
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's usually a dodgy proposition to try and guess what someone else's intent was with code that doesn't function, but it appears to me in your original post, that you're measuring sensor data, comparing it to a desired value, seeing that the sensor data is low and then turning on some systems intended to improve the situation, presumably by adding water and providing heat. They run for 10 iterations, about 10 seconds, then you're looking to see if the average reading has improved and if it has, the systems you turned on can be turned off.

However, you read data and data2 only once, so your loop is always going to calculate the average as unchanged, the systems you want to turn off won't be.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

However, you read data and data2 only once, so your loop is always going to calculate the average as unchanged, the systems you want to turn off won't be.
that's my problem! i tried to fix it but without any positive results.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3700
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm guessing again, but I assume you tried this?
Code:
       sum += data;
       sum2+= data2;
       delay(1000);
       data = analogRead(sensorOne);      // read sensorOne value.
       data2 = analogRead(sensorTwo);     // read sensorTwo value.
      }

That might have improved matters, but there is still a possible flaw: If the readings are still too low, you won't turn anything off and things will continue to get moister and hotter. There are some delays in your loop at the top. If one of the data values rises above voltage before the next iteration, there is no code path that will ever turn things off.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm guessing again, but I assume you tried this?
Code:
       sum += data;
       sum2+= data2;
       delay(1000);
       data = analogRead(sensorOne);      // read sensorOne value.
       data2 = analogRead(sensorTwo);     // read sensorTwo value.
      }

That might have improved matters, but there is still a possible flaw: If the readings are still too low, you won't turn anything off and things will continue to get moister and hotter. There are some delays in your loop at the top. If one of the data values rises above voltage before the next iteration, there is no code path that will ever turn things off.
you might have not downloaded the file i posted for the full codes
Logged

Pages: [1]   Go Up
Jump to: