#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <DHT.h>
#define DHTPIN 7 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
int chk;
float hum; //Stores humidity value
float tem;
int phval = 0;
float phDesired; // PH setpoint
const float phThresh = 0.15; // Stop adjusting when within this of `pHDesired`.
const float adjustVol = 1.0; // mL
const float mlToMs = 1.0; // Calibrate to find this.
const int waitBetweenAdjustments = 180 * 100UL; // ms
const int primeTime = 1000; // ms. Time to prime pumps
const int loopDelay = 1000; // ms. Time between main loop polling
unsigned long int avgval; int buffer_arr[10],temp; //Raw Data sample array - pH
// ******************** Special char -+********************
byte customChar[8] = {
0b00000,
0b00100,
0b00100,
0b11111,
0b00100,
0b00100,
0b11111,
0b00000
};
// ******************** Void setup ********************
void setup() {
Serial.begin(9600);
dht.begin();
lcd.init();
lcd.begin(16, 2);
lcd.createChar(0, customChar);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Welcome to");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Smart");
lcd.setCursor(0, 1);
lcd.print("Agriculture");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Soil Quality");
lcd.setCursor(0, 1);
lcd.print("Assessment");
delay(2000);
lcd.clear();
pinMode(12, OUTPUT); // sets the digital pin 1 as output
pinMode(13, OUTPUT); // sets the digital pin 1 as output
}
// ******************** Main Prog********************
void loop(void) {
for(int i=0;i<10;i++)
{
buffer_arr[i]=analogRead(A0); // RAW data of pH sensor 10 sample
delay(30);
}
for(int i=0;i<9;i++)
{
for(int j=i+1;j<10;j++)
{
if(buffer_arr[i]>buffer_arr[j])
{
temp=buffer_arr[i];
buffer_arr[i]=buffer_arr[j];
buffer_arr[j]=temp;
}
}
}
avgval=0;
for(int i=2;i<8;i++)
avgval+=buffer_arr[i];
float volt=(float)avgval*5.0/1023/6; //taking avg of 6 values of PH (2-8)
float ph_act = 2.8 * volt; // 0-5V => 0-14
float phDesired=analogRead(A1)*5.0/1023*2.8;
lcd.setCursor(0, 0);
lcd.print("pH SP:");
lcd.setCursor(6, 0);
lcd.print(phDesired);
lcd.setCursor(11, 0);
lcd.write((byte)8);
lcd.setCursor(12, 0);
lcd.print(phThresh);
lcd.setCursor(0, 1);
lcd.print("pH PV:");
lcd.setCursor(6, 1);
lcd.print(ph_act);
if (abs(ph_act - phDesired) > phThresh) {
if ((ph_act - phDesired) > phThresh) {
addPhModifier(12);
//delay(waitBetweenAdjustments); // delay between adjustment
}
else if ((ph_act - phDesired) < -phThresh) {
addPhModifier(13);
//delay(waitBetweenAdjustments);
}
delay(loopDelay);
}
else {
// do nothing
}
lcd.clear();
delay(2000);
//Read data and store it to variables hum and temp
hum = dht.readHumidity();
tem= dht.readTemperature();
//Print temp and humidity values to serial monitor
lcd.print("Humidity: ");
lcd.print(hum);lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(tem);
lcd.println("C");
delay(10000);
//delay(50);
}
// ******************** Support functions ********************
// Populate this function to calibrate dose volumes: It's easier
// to work in volume than in time.
float calibrate_volume(int volMl) {
return 1.; // placeholder
}
void addVolume(int pump, float volMl) {
float timeMs = mlToMs * volMl;
digitalWrite(pump, HIGH);
delay(timeMs);
digitalWrite(pump, LOW);
}
void addPhModifier(int pump) {
addVolume(pump, adjustVol);
}
// Run this function while the pumps' inputs are connected to the
// pH up and down solutions, and output is plugged into a drain.
void primePumps() {
digitalWrite(12, HIGH); // Turn on pump 1
digitalWrite(13, HIGH); // Turn on pump 2.
delay(primeTime);
digitalWrite(12, LOW); // Turn off pump 1.
digitalWrite(13, LOW); // Turn off pump 2.
}