#include <OneWire.h> //One-wire interface for temp sensor
#include <SimpleRotary.h> //Rotary encoder
#include <DallasTemperature.h> //Rotary encoder
#include <LiquidCrystal_I2C.h> // LCD library
#include <Servo.h>
#define NTC_PIN A0 //Pin A0
#define HALF_POWER 127 // 50% of 255
#define FULL_POWER 255
int tasviePin = 5; // LED Digital Pin With Tasvie
int ledPin = 7; // LED Digital Pin with PWM
int buzzerPin = 8; // buzzer Pin
int heaterPin = 13; // heater Pin
int phPin = A7; // buzzer Pin
const int BEST_PH = 7;
int cycleHeater = 0;
//pin Ultrasonik
#define trigPin 2
#define echoPin 3
#define WATER_THRESHOLD 100
//Variables
int chk;
float temp; //Stores temperature value
bool blink = false;
bool heaterOn = false;
unsigned long currentTime = 0;
unsigned long heater_start_time = 0;
const int lcdColumns = 20;
const int lcdRows = 4;
const int i2c_addr = 0x27;
long distance;
unsigned long highTime = millis() / 1000; // convert to seconds
Servo heatingServo;
// Initialize the DHT sensor and LCD
LiquidCrystal_I2C lcd(i2c_addr, lcdColumns, lcdRows);
float readTemperature() {
int analogValue = analogRead(NTC_PIN);
temp = 1 / (log(1 / (1023. / analogValue - 1)) / 3950 + 1.0 / 298.15) - 273.15;
temp = temp + 0.5 * cycleHeater;
}
void update_distance() {
long duration;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
}
void setup() {
//set the temperature sensor
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight();
//set the heater
pinMode(heaterPin, OUTPUT);
heatingServo.attach(heaterPin);
heatingServo.write(105);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//set the buzzer
pinMode(buzzerPin, OUTPUT);
pinMode(phPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(tasviePin, OUTPUT);
}
void loop() {
//take the current time
currentTime = millis();
if (currentTime / 1000 - highTime < 3) { // first 3 seconds
analogWrite(ledPin, 128);
} else { // last 6 seconds
analogWrite(ledPin, 255);
if(currentTime/1000 - highTime == 9){
highTime = currentTime / 1000;
}
}
readTemperature();
update_distance();
float pH = map(analogRead(phPin), 0, 1023, 0.0, 14.0);
if(pH < BEST_PH){
analogWrite(tasviePin, 255);
}else{
analogWrite(tasviePin, 0);
}
// Control the heater
if(temp > 24 && temp < 29){
heaterOn = false;
}else if (temp < 24.0) {
heaterOn = true;
heatingServo.write(120);
} else if (temp > 29.0) {
heaterOn = false;
heatingServo.write(105);
}
if(heaterOn){
if(currentTime - heater_start_time >= 1500){
cycleHeater = cycleHeater + 1;
heater_start_time = currentTime; // Reset the start time
}
}
// If the conditions are not as expected, sound the buzzer
if (temp < 24.0 || temp > 29.0 || distance < WATER_THRESHOLD) {
// tone(buzzerPin, 1000); // Generate a sound
} else {
noTone(buzzerPin);
}
lcd.setCursor(0, 0);
lcd.print("Water Level: ");
lcd.print(distance, 1);
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temp, 1);
lcd.setCursor(0, 2);
lcd.print("pH Value: ");
lcd.println(pH, 2); // Print pH value with 2 decimal places
//wait until 100ms have passed (10Hz), since main loop has been started
while(millis() < currentTime + 100) {
}
}