#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define SensorPin A0 // the pH meter Analog output is connected with the Arduino’s Analog
#define analogPin A1 // Hardware adj setpoint PH
unsigned long int avgValue; //Store the average value of the sensor feedback
float b, Std_Ph; // ตั้งค่าที่จะทิ้งสารเคมี ตรงนี้
int buf[10], temp;
int Valve_NaOH_1 = 8; // ควบคุมการเปิด/ปิด โซดาไฟ 1
int Valve_Effluent_1 = 9;
int Valve_NaOH_2 = 10; // ควบคุมการเปิด/ปิด โซดาไฟ 2
int Valve_Effluent_2 = 11;
int LED = 13; // ตรวจสอบสถานะการทำงานของ sensor
bool comReady1=false;
String comString1="";
unsigned long previousTimeLed1 = millis();
long timeIntervalLed1 = 500; // time scan PH
int ledState1 = LOW;
unsigned long previousTimeAnalog = millis();
long timeIntervalAnalog = 750; // time scan Analog Adj PH
int countDown=60;
bool Val_Open = false;
bool NaOH_Open = false;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(Valve_NaOH_1, OUTPUT);
pinMode(Valve_Effluent_1, OUTPUT);
pinMode(Valve_NaOH_2, OUTPUT);
pinMode(Valve_Effluent_2, OUTPUT);
digitalWrite(Valve_Effluent_1, HIGH); // เริ่มต้นโปรแกรม วาล์วน้ำทิ้งปิด
digitalWrite(Valve_NaOH_1, HIGH); // เริ่มต้นโปรแกรม วาล์วสารเคมีปิด
digitalWrite(Valve_Effluent_2, HIGH); // เริ่มต้นโปรแกรม วาล์วน้ำทิ้งปิด
digitalWrite(Valve_NaOH_2, HIGH); // เริ่มต้นโปรแกรม วาล์วสารเคมีปิด
bool Val_Open = false;
Serial.begin(9600);
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.setBacklight(HIGH);
lcd.home (); // ไปที่ตัวอักษรที่ 0 แถวที่ 1
lcd.print("Wastewater Treatment");
lcd.setCursor(0,1); // ไปทตัวอักษรที่ 3 แถวที่ 2
lcd.print(" System ");
lcd.setCursor(0,2);
lcd.print(" Kfresh ");
lcd.setCursor(0,3);
lcd.print(" Design by Narin V1 ");
Serial.println("Ready"); //Test the serial monitor
delay(3000);
}
void loop()
{
static double ti=1000; // time interval
static double pvtime=millis(); // previous time
double elaps=0;
//------------------- Task 1 --------------------------------
unsigned long currentTime = millis();
// task 1
if(currentTime - previousTimeLed1 > timeIntervalLed1) {
previousTimeLed1 = currentTime;
if (ledState1 == HIGH) {
ledState1 = LOW;
}
else {
ledState1 = HIGH;
}
digitalWrite(LED, ledState1);
check_PH(); // ทำการอ่านค่า PH พร้อมกับการกระพริบ LED ขา 13 เพื่อแสดงว่าโปรแกรมยังอยู่ใน Loop การทำงาน
}
//------------------- Task 2 ----------------------------------
// รับค่า จาก Serial Port เพื่อเปลี่ยนแปลง ค่า Setpoint
if(Serial.available()>0){
serialEvent();
}
if(comReady1){
Std_Ph=comString1.toFloat();
comReady1=false;
}
//------------------- Task 3 ----------------------------------
// Read Analog for Adj PH 10 bit or 1023 to 0-14 by Map Fuction
unsigned long currentTime_A = millis();
if(currentTime_A - previousTimeAnalog > timeIntervalAnalog){
previousTimeAnalog = currentTime_A;
int val = analogRead(analogPin);
val = map(val,0,1023,0,1400);
Std_Ph = val/100.00; // เก็บ ค่า Analog ไปใส่ ค่า Setpoint PH
}
//------------------- Task 4 ----------------------------------
if(Val_Open){
Val_Open = false;
Serial.println(Val_Open);
Serial.println(countDown);
if(countDown>0){
unsigned long cctime = millis();
if ( cctime - pvtime > ti ){
pvtime = cctime;
countDown--;
Serial.println(countDown);
digitalWrite(Valve_Effluent_1,LOW);
digitalWrite(Valve_Effluent_2,LOW);
lcd.setCursor(0, 0);
lcd.print("Time Delay : ");
lcd.setCursor(13,0);
lcd.print(countDown);
lcd.print(" s ");
lcd.setCursor(0, 3);
lcd.print("NaOH: OFF,Valve: ON ");
}
}else {
pvtime=millis();
digitalWrite(Valve_Effluent_1,HIGH); // wait until countDown done
digitalWrite(Valve_Effluent_2,HIGH);
lcd.setCursor(0, 0); // ไปที่ตัวอักษรที่ 0 แถวที่ 0
lcd.print("Wait for Treatment ");
Serial.print(" add NaOH : N ");
lcd.setCursor(0, 3);
lcd.print("NaOH: OFF,Valve: OFF");
Serial.println(": Solinoid Value : Close ");
}
}
//------------------- Task 5 ---------------------------------- // รอแก้ไข ง่วงนอน
if(NaOH_Open){
NaOH_Open = false;
Serial.println(NaOH_Open);
Serial.println(countDown);
if(countDown>0){
unsigned long cctime = millis();
if ( cctime - pvtime > ti ){
pvtime = cctime;
countDown--;
Serial.println(countDown);
digitalWrite(Valve_Effluent_1,LOW);
digitalWrite(Valve_Effluent_2,LOW);
lcd.setCursor(0, 0);
lcd.print("Time Delay : ");
lcd.setCursor(13,0);
lcd.print(countDown);
lcd.print(" s ");
lcd.setCursor(0, 3);
lcd.print("NaOH: OFF,Valve: ON ");
}
}else {
pvtime=millis();
digitalWrite(Valve_Effluent_1,HIGH); // wait until countDown done
digitalWrite(Valve_Effluent_2,HIGH);
lcd.setCursor(0, 0); // ไปที่ตัวอักษรที่ 0 แถวที่ 0
lcd.print("Wait for Treatment ");
Serial.print(" add NaOH : N ");
lcd.setCursor(0, 3);
lcd.print("NaOH: OFF,Valve: OFF");
Serial.println(": Solinoid Value : Close ");
}
}
}
void check_PH()
{
Serial.print("STD = ");
Serial.print(Std_Ph, 2);
Serial.print(" PH | ");
lcd.setCursor(0, 1); // ไปทตัวอักษรที่ 0 แถวที่ 1
lcd.print("PH Setpoint = ");
lcd.setCursor(14, 1);
lcd.print(Std_Ph, 2);
for (int i = 0; i < 10; i++) //Get 10 sample value from the sensor for smooth the value
{
buf[i] = analogRead(SensorPin);
delay(10);
}
for (int i = 0; i < 9; i++) //sort the analog from small to large
{
for (int j = i + 1; j < 10; j++)
{
if (buf[i] > buf[j])
{
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
}
avgValue = 0;
for (int i = 2; i < 8; i++) //take the average value of 6 center sample
avgValue += buf[i];
float phValue = (float)avgValue * 5.0 / 1024 / 6; //convert the analog into millivolt
phValue = 3.5 * phValue; //convert the millivolt into pH value
Serial.print("Measured Value = ");
lcd.setCursor(0, 2);
Serial.print(phValue, 2);
lcd.print("Actual PH = ");
lcd.print(phValue, 2);
lcd.print(" PH ");
Serial.print(" PH ");
if ( Std_Ph >= phValue ) { // cutting point
lcd.setCursor(0, 0);
lcd.print("Processing Treatment");
Serial.print(" add NaOH : Y ");
NaOH_Open = true;
digitalWrite(Valve_NaOH_1, LOW); // Valve_NaOH = ON
digitalWrite(Valve_NaOH_2, LOW); // Valve_NaOH = ON
lcd.setCursor(0, 3);
lcd.print("NaOH: ON ,Valve: OFF");
Serial.println(": Solinoid Value : Close ");
Val_Open = false;
digitalWrite(Valve_Effluent_1, HIGH); // Valve_Effluent = OFF
digitalWrite(Valve_Effluent_2, HIGH); // Valve_Effluent = OFF
lcd.setBacklight(HIGH); // เปิดหน้าจอไฟจอแสดงผล
countDown=60; //
} else {
digitalWrite(Valve_NaOH_1, HIGH); // Valve_NaOH = OFF
digitalWrite(Valve_NaOH_2, HIGH); // Valve_NaOH = OFF
Serial.println(": Solinoid Value : On by delay ");
Val_Open = true;
// digitalWrite(Valve_Effluent_1, HIGH); // Valve_Effluent = ON
lcd.setBacklight(LOW); // ปิดหน้าจอไฟจอแสดงผล
}
}
void serialEvent()
{
static String comStr="";
while (Serial.available()>0)
{
char inChar = (char)Serial.read(); // get the new byte:
comStr += inChar; // add it to the comString:
if (inChar == '\n')
{
comStr.trim();
comString1=comStr;
comStr="";
comReady1 = true;
}
}
}