Go Down

Topic: can't get it work right! (Read 755 times) previous topic - next topic

latif

i can't get the data displayed right in the screen!!
i tried to find out what's wrong but i couldn't, i took the temp/humidity code somewhere from the net and modify it to my code!
Code: [Select]
#include <LiquidCrystal.h>  // include the library code:
LiquidCrystal lcd(12, 11,10, 5, 4, 3, 2);  // initialize the library with the numbers of the interface pins
int backlight = 13;        // tun the back light of lcd on.
int voltage = 0;         // the volatge value to compare with the sensor value.
int pump = 6;             // select the pin for the pump.
int dataPin = 9;
int sckPin = 8;

int sensorOne = A0;        // select the input pin for the first sensor.
int valve = 0;             // the pin for the first sensor valve.
int sum=0;                 // the sum of the sensorOne reading.
int average =0;            // the average reading for sensorOne reading.

int sensorTwo = A1;        // select the input pin for the second sensor.
int valve2 = 1;            // the pin for the first sensor valve.
int sum2=0;                // the sum of the sensorTwo reading.
int average2=0;            // the average reading for sensorTwo reading.

int temp = A2;             // select the input pin for the temp sensor.
int humidity = A3;         //  humidity sensor.
int maxTemp = 85;          // the maximum temperature.
int valve3 = 7;            // the pin for the first sensor valve.
int sum3=0;                // the sum of the sensorTwo reading.
int average3=0;            // the average reading for sensorTwo reading.

void resetSHT()
{
 pinMode(backlight,OUTPUT);
 pinMode(dataPin,OUTPUT);
 pinMode(sckPin,OUTPUT);
 
 shiftOut(dataPin, sckPin, LSBFIRST, 255);
 shiftOut(dataPin, sckPin, LSBFIRST, 255);
 
 digitalWrite(dataPin,HIGH);
 for(int i = 0; i < 15; i++){
    digitalWrite(sckPin, LOW);
    digitalWrite(sckPin, HIGH);
    digitalWrite(backlight,HIGH);
  }
}

//Specific SHT start command
void startSHT()
{
 pinMode(sckPin,OUTPUT);
 pinMode(dataPin,OUTPUT);
 digitalWrite(dataPin,HIGH);
 digitalWrite(sckPin,HIGH);
 digitalWrite(dataPin,LOW);
 digitalWrite(sckPin,LOW);
 digitalWrite(sckPin,HIGH);
 digitalWrite(dataPin,HIGH);
 digitalWrite(sckPin,LOW);
}

void writeByteSHT(byte data)
{
 pinMode(sckPin,OUTPUT);
 pinMode(dataPin,OUTPUT);  
 
//  digitalWrite(dataPin,LOW);
 shiftOut(dataPin,sckPin,MSBFIRST,data);
 
 pinMode(dataPin,INPUT);

 //Wait for SHT15 to acknowledge by pulling line low
 while(digitalRead(dataPin) == 1);
 
 digitalWrite(sckPin,HIGH);
 digitalWrite(sckPin,LOW);  //Falling edge of 9th clock
 
 //wait for SHT to release line
 while(digitalRead(dataPin) == 0 );

 //wait for SHT to pull data line low to signal measurement completion
 //This can take up to 210ms for 14 bit measurments
 int i = 0;
 while(digitalRead(dataPin) == 1 )
 {
   i++;
   if (i == 255) break;
   
   delay(10);
 }
 
 //debug
 i *= 10;
// lcd.print("Response time = ");
// lcd.println(i);
}

//Read 16 bits from the SHT sensor
int readByte16SHT()
{
 int cwt = 0;
 unsigned int bitmask = 32768;
 int temp;
 
 pinMode(dataPin,INPUT);
 pinMode(sckPin,OUTPUT);
 
 digitalWrite(sckPin,LOW);
 
 for(int i = 0; i < 17; i++) {
   if(i != 8) {
     digitalWrite(sckPin,HIGH);
     temp = digitalRead(dataPin);
     lcd.setCursor(0,2);
     lcd.print(temp,BIN);
     cwt = cwt + bitmask * temp;
     digitalWrite(sckPin,LOW);
     bitmask=bitmask/2;
   }
   else {
     pinMode(dataPin,OUTPUT);
     digitalWrite(dataPin,LOW);
     digitalWrite(sckPin,HIGH);
     digitalWrite(sckPin,LOW);
     pinMode(dataPin,INPUT);
   }
 }
 
 //leave clock high??
 digitalWrite(sckPin,HIGH);
 
 lcd.println();
 
 return cwt;
}

int getTempSHT()
{
 startSHT();
 writeByteSHT(B0000011);
 return readByte16SHT();
}

int getHumidSHT()
{
 startSHT();
 writeByteSHT(B00000101);
 return readByte16SHT();
}


latif

Continue ...
Code: [Select]
void setup() {
 pinMode(backlight, OUTPUT);
 digitalWrite(backlight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
 lcd.begin(20, 4);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("     Welcome to");    // change this text to whatever you like. keep it clean.
 lcd.setCursor(0,1);           // set cursor to column 0, row 1
 lcd.print("Senior Project ELET440");
 lcd.setCursor(0,2);
 lcd.print("Probe Auto-Watering.");
 lcd.setCursor(0,3);
 lcd.print("Starting.");
 delay(2000);
 lcd.setCursor(9,3);
 lcd.print(".");
 delay(2000);
 lcd.setCursor(10,3);
 lcd.print(".");
 delay(2000);
 lcd.setCursor(11,3);
 lcd.print(".");
 delay(2000);
 lcd.setCursor(12,3);
 lcd.print(".");
 delay(2000);
 lcd.setCursor(13,3);
 lcd.print(".");
 delay(2000);
 lcd.setCursor(14,3);
 lcd.print(".");
 delay(4000);                // Delay 5 seconds.
 lcd.clear();
 delay(150);

 pinMode(dataPin,OUTPUT);
 pinMode(sckPin,OUTPUT);
 pinMode(pump, OUTPUT);       // declare pump as output.
 pinMode(valve, OUTPUT);      // declare valve as output.
 pinMode(valve2, OUTPUT);     // declare valve2 as output.
 pinMode(valve3, OUTPUT);     // declare valve3 as output.
 Serial.begin(9600);       // initialize serial communication with computer
 resetSHT();
}

void loop() {
 digitalWrite(pump, LOW);     // turn off the pump.
 digitalWrite(valve, LOW);    //turn off 1st valve.
 digitalWrite(valve2, LOW);   // turn off 2nd valve.
 digitalWrite(valve3, LOW);   // turn off 3rd valve.
 
 analogRead(sensorOne);      // read sensorOne value.
 lcd.setCursor(0, 1);
 lcd.print("S_1: ");
 lcd.print(analogRead(sensorOne));
 analogRead(sensorTwo);     // read sensorTwo value.
 lcd.setCursor(9, 1);
 lcd.print("S_2: ");
 lcd.print(analogRead(sensorTwo));
 int temp = getTempSHT();
 lcd.setCursor(0,2);
 lcd.print("Temp:");
 lcd.print(temp);
 temp = getHumidSHT();
 lcd.setCursor(9,2);
 lcd.print("Humi:");
 lcd.print(temp);
 
 if (analogRead(sensorOne) < voltage && analogRead(sensorTwo) < voltage){
   while (analogRead(sensorOne) < voltage &&  analogRead(sensorTwo) < voltage){
   digitalWrite(pump, HIGH);
   digitalWrite(valve, HIGH);
   digitalWrite(valve2, HIGH);
     for (int i=0; i<6; i++){
       lcd.setCursor(0, 1);
       lcd.print("S_1: ");
       lcd.print(analogRead(sensorOne));
       lcd.setCursor(0,0);
       lcd.print("Pump:ON");
       lcd.setCursor(9,0);
       lcd.print("V1:ON");
      sum += analogRead(sensorOne);
      delay(5000);
    }
    average = sum/10;
    if(average > voltage){
    break;
   }
   /////// update the valve condition
      for (int i=0; i<10; i++){
       lcd.setCursor(9, 1);
       lcd.print("S_2: ");
       lcd.print(analogRead(sensorTwo));
       lcd.setCursor(15,0);
       lcd.print("V2:ON");
       sum2 += analogRead(sensorTwo);
      delay(5000);
    }
    average2 = sum2/10;
    if(average2 > voltage){
    break;
    }
   digitalWrite(pump, LOW);
   digitalWrite(valve, LOW);
   digitalWrite(valve2, LOW);
   lcd.setCursor(0,0);
   lcd.print("Pump:OFF");
   lcd.setCursor(9,0);
   lcd.print("V1:OFF");
   lcd.setCursor(15,0);
   lcd.print("V2:OFF");
  }
 }
 
 else if(analogRead(sensorOne) < voltage){
   while (analogRead(sensorOne) < voltage){
    digitalWrite(pump, HIGH);
    digitalWrite(valve, HIGH);
    for (int i=0; i<6; i++){
       lcd.setCursor(0, 1);
       lcd.print("S_1: ");
       lcd.print(analogRead(sensorOne),DEC);
       lcd.setCursor(0,0);
       lcd.print("Pump:ON");
       lcd.setCursor(9,0);
       lcd.print("V1:ON");
      sum += analogRead(sensorOne);
      delay(5000);
    }
    average = sum/10;
    sensorOne = average;
  }
    digitalWrite(pump, LOW);
    digitalWrite(valve, LOW);
    lcd.setCursor(0,0);
    lcd.print("Pump:OFF");
    lcd.setCursor(9,0);
    lcd.print("V1:OFF");
 }
  else if(analogRead(sensorTwo) < voltage){
    while(sensorTwo < voltage){
    digitalWrite(pump, HIGH);
    for (int i=0; i<10; i++){
       lcd.setCursor(9, 1);
       lcd.print("S_2: ");
       lcd.print(analogRead(sensorTwo),DEC);
       lcd.setCursor(0,0);
       lcd.print("Pump:ON");
       lcd.setCursor(15,0);
       lcd.print("V2:ON");
      sum2 += analogRead(sensorTwo);
      delay(5000);
    }
    average2 = sum2/10;
     if(average2 > voltage)
     break;
    digitalWrite(pump, LOW);
    digitalWrite(valve2, LOW);
    lcd.setCursor(0,0);
    lcd.print("Pump:OFF");
    lcd.setCursor(15,0);
    lcd.print("V2:OFF");
    }
  }
  else if(getTempSHT() > maxTemp && getHumidSHT() < 70){
   while (getTempSHT() > maxTemp && getHumidSHT() < 70){
    digitalWrite(pump, HIGH);
    for (int i=0; i<6; i++){
       lcd.setCursor(0, 2);
       lcd.print("Temp: ");
       lcd.print(getTempSHT());
       lcd.setCursor(0,0);
       lcd.print("Pump:ON");
      sum3 += getTempSHT();
      delay(5000);
    }
    average3 = sum3/10;
    if (average3 > maxTemp || getHumidSHT() > 70){
    break;
    lcd.setCursor(0,0);
    lcd.print("Pump:OFF");
    }
  }
 }
  else
    lcd.setCursor(0, 3);
    lcd.print("the probe is in great condition.");
}

AWOL

#2
Mar 07, 2011, 10:06 am Last Edit: Mar 07, 2011, 10:19 am by AWOL Reason: 1
There seems to me to be a lot of code there that isn't anything to do with an LCD.
Why don't you concentrate on the bit that does?

Statements like this:
Quote
i can't get the data displayed right in the screen!!
i tried to find out what's wrong but i couldn't,

help no-one.

Code: [Select]
for (int i=0; i<6; i++){
      sum += analogRead(sensorOne);
      delay(5000);
    }
    average = sum/10;


Read six values and divide by ten to get the average?
Hmmm.

Probably a good idea to zero 'sum' before you use it too.


Code: [Select]
  else
    lcd.setCursor(0, 3);
    lcd.print("the probe is in great condition.");

Missing some braces, perhaps.


Do you see how a lot of your code looks very similar to other bits?
That's usually a clue that you can make your code shorter, which gives the bugs fewer dark corners to hide in, makes your code easier to read, debug and maintain.
"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.

PaulS

Code: [Select]
int sensorOne = A0;        // select the input pin for the first sensor.
int sensorTwo = A1;        // select the input pin for the second sensor.
int temp = A2;             // select the input pin for the temp sensor.
int humidity = A3;         //  humidity sensor.

The aliases A0, A1, A2, and A3 are to be used when using analog pins as digital pins. Is that what you are doing? Are your sensors, whatever they are, digital sensors?

If they are, why are you using analogRead to read them? If they are not, why are you referring to the pins with their digital names?

You should be using 0, 1, 2, and 3, not A0, A1, A2, and A3.

AWOL

Quote
The aliases A0, A1, A2, and A3 are to be used when using analog pins as digital pins

The aliases can be used for both analogue and digital.
"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.

latif

I'm using SHT15 temp/humidity sensor, also 2- VG400 for the soil.

AWOL

That doesn't help a great deal.
Ideally, you will have a test program that simulates good readings, and then you can pass them to your LCD drivers to debug your display problems.
"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.

gardner

Is your LCD a 1-line 16-character one?
If so, you may find it works if you treat it as a 2-line by 8-character one.
Read this thread:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1282665074

Go Up