Here is the code I've written so far...Oh, if i forgot some information that you need please tell and i'll present it.
/*
Arduino Mega 2560 Hydroponic Controller V.1
Purpose:
-Monitor pH level of nutrient tank with pH probe and dose (pH-/pH+) wit two
peristaltic pumps.
-Monitor fluidlevel in the nutrient tank with float switch. In case it gets
to low a solenoid valve opens up and fils the tank.
-Measure temperature and humidity. If both get to high a fan removes hot and
humid air.
)
Todo's:
-Measure amount of light received during the day. (Primairely used for
loggin purposes
-Use second float switch to close off solenoid valve so i don't have to use
a timerfunction.
-Monitor and control EC value's. ie, When waterlevel & EC are low the
solenoid valve opens up while fertilizer is added.
Code found on the WWW. Thanks to everybody who contributed. You know who you are ;)
*/
//Define PIN's
#define dht_dpin 69 //Analog PIN 15
#define pHPin 62 //Analog PIN 8
#define pHPlusPin 10
#define pHMinPin 11
#define ventilatorPin 9
#define floatPin 7
#define solenoidPin 8
#define DHTTYPE DHT11
byte compFunc; //for passing error code back from complex functions.
byte dht_dat[4]; //Array to hold the bytes sent from sensor.
void setup()
{
pinMode(pHPlusPin, OUTPUT); //setting PIN's as output
pinMode(pHMinPin, OUTPUT);
pinMode(ventilatorPin, OUTPUT);
pinMode(solenoidPin, OUTPUT);
InitDHT(); //Initializing for reading DHT
Serial.begin(9600);
delay(300); //Must wait one second for DHT chip to stabilize. This is
//the first part 0.3 seconds.
Serial.println("Luchtvochtigheid, temperatuur & pH");
Serial.println("__________________________________\n\n");
delay(700); //rest of 1 second. Second part 0.7 seconds/
}
void loop()
{
//measuring pH level...
float sensorValue = 0;
sensorValue = analogRead(pHPin); //Read analog pin
float pH(0.0178 * sensorValue - 1.889); //Conversionformula for pH
if (pH >= 6.00) //If pH gets below 5 or above 6 a
{ //corresponding pump gets activated.
digitalWrite(pHMinPin, HIGH);
digitalWrite(pHPlusPin, LOW);
}
else if (pH <= 5.00)
{
digitalWrite(pHPlusPin, HIGH);
digitalWrite(pHMinPin, LOW);
}
else if (pH > 5.00 || pH < 6.00)
{
digitalWrite(pHMinPin, LOW);
digitalWrite(pHPlusPin, LOW);
}
//measuring temperature & humidity.
ReadDHT(); //Read from DHT chip
switch (compFunc){
case 0:
Serial.print("Luchtvochtigheid = ");
Serial.print(dht_dat[0], DEC);
Serial.println("% ");
Serial.print("Temperatuur = ");
Serial.print(dht_dat[2], DEC);
Serial.println(" *C ");
break;
case 1:
Serial.println("Error 1: DHT start condition 1 not met.");
break;
case 2:
Serial.println("Error 2: DHT start condition 2 not met.");
break;
case 3:
Serial.println("Error 3: DHT checksum error.");
break;
default:
Serial.println("Error: Unrecognized code encountered.");
break;
} //End of switch
Serial.print("pH = : "); //adding pH to screen
Serial.println(pH);
if(dht_dat[0] >= 80 && dht_dat[2] >= 35)
{
digitalWrite(ventilatorPin, HIGH);
Serial.println("Ventilator geactiveerd");
}
else
{
digitalWrite(ventilatorPin, LOW);
}
//measuring waterlevel...
int levelValue = LOW;
levelValue = digitalRead(floatPin);
if (levelValue == HIGH)
{
digitalWrite(solenoidPin, HIGH);
Serial.println("Solenoid klep geactiveerd");
}
else
{
digitalWrite(solenoidPin, LOW);
}
Serial.println(" \n\n");
delay(2000); //Delay can be adjusted to 800ms but better to
//set it to 2000ms for more stable reading.
Serial.flush(); //flush all other data.
}
void InitDHT()
{
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
}
void ReadDHT()
{
/*Uses global variables dht_dat[0-4], and compFunc to pass
"answer" back. compFunc=0 if read went okay.
Depends on global dht_dpin for where to look for sensor.*/
compFunc=0;
byte dht_in;
byte i;
// Send "start read and report" command to sensor....
// First: pull-down I/O pin for 23000us
digitalWrite(dht_dpin,LOW);
delay(50); //standard is 23
//Next line: Brings line high again,
//second step in giving "start read..." command
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40); //DHT22 datasheet says host should
//keep line high 20-40us, then watch for sensor taking
//line low. That low should last 80us. Acknowledges
//"start read and report" command.
//Next: Change Arduino pin to an input, to
//watch for the 80us low explained a moment ago.
pinMode(dht_dpin,INPUT);
dht_in=digitalRead(dht_dpin);
if(dht_in){
compFunc=1; //dht start condition 1 not met
return;
} //end if
delayMicroseconds(80);
dht_in=digitalRead(dht_dpin);
if(!dht_in){
compFunc=2; //dht start condition 2 not met
return;
} //end if
/*After 80us low, the line should be taken high for 80us by the
sensor. The low following that high is the start of the first
bit of the forty to come. The routine "read_dht_dat()"
expects to be called with the system already into this low.*/
delayMicroseconds(80);
//now ready for data reception... pick up the 5 bytes coming from
// the sensor
for (i=0; i<5; i++)
dht_dat[i] = read_dht_dat();
//Next: restore pin to output duties
pinMode(dht_dpin,OUTPUT);
//Next: Make data line high again, as output from Arduino
digitalWrite(dht_dpin,HIGH);
//Next see if data received consistent with checksum received
byte dht_check_sum =
dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
/*Condition in following "if" says "if fifth byte from sensor
not the same as the sum of the first four..."*/
if(dht_dat[4]!= dht_check_sum)
{compFunc=3;}//DHT checksum error
}
byte read_dht_dat()
{
//Collect 8 bits from datastream, return them interpreted
//as a byte. I.e. if 0000.0101 is sent, return decimal 5.
//Code expects the system to have recently entered the
//dataline low condition at the start of every data bit's
//transmission BEFORE this function is called.
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
//We enter this during the first start bit (low for 50uS) of the byte
//Next: wait until pin goes high
while(digitalRead(dht_dpin)==LOW);
//signalling end of start of bit's transmission.
//Dataline will now stay high for 27 or 70 uS, depending on
//whether a 0 or a 1 is being sent, respectively.
delayMicroseconds(45);//AFTER pin is high, wait further period, to be
//into the part of the timing diagram where a 0 or a 1 denotes
//the datum being send. The "further period" was 30uS in the software
//that this has been created from. I believe that a higher number
//(45?) might be more appropriate.
//Next: Wait while pin still high
if (digitalRead(dht_dpin)==HIGH)
result |=(1<<(7-i));// "add" (not just addition) the 1
//to the growing byte
//Next wait until pin goes low again, which signals the START
//of the NEXT bit's transmission.
while (digitalRead(dht_dpin)==HIGH);
} //end for
return result;
}