#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#define ONE_WIRE_BUS 2
#define SERVO_PIN 3
#define LIGHT_PIN 4 // LED for day-night shifting
#define WATER_LEVEL_PIN 5 // Push button pin
#define HEATING_RELAY_PIN 6 // Relay to control the heating element
#define COOLING_RELAY_PIN 7 // Relay to control the cooling element
#define HEATING_LED_PIN 8 // LED to simulate the heating element
#define COOLING_LED_PIN 9 // LED to simulate the cooling element
#define BUZZER_PIN 10 // Buzzer pin
#define OXYGEN_SENSOR_PIN A0 // Analog pin for oxygen sensor (using a potentiometer for simulation)
#define OXYGEN_LED_PIN 11 // LED pin to simulate oxygen pump
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Servo feederServo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long previousMillis = 0; // Variable to store the previous time
const long interval = 10000; // Interval in milliseconds (10 seconds)
const float TEMP_THRESHOLD_LOW = 22.0; // Lower temperature threshold
const float TEMP_THRESHOLD_HIGH = 28.0; // Upper temperature threshold
bool waterLevelLow = false; // Flag to indicate if water level is low
bool buzzerState = false; // Flag to indicate buzzer state
void setup()
{
sensors.begin();
pinMode(LIGHT_PIN, OUTPUT);
pinMode(WATER_LEVEL_PIN, INPUT_PULLUP); // Set pin mode to INPUT_PULLUP for the push button
pinMode(HEATING_RELAY_PIN, OUTPUT);
pinMode(COOLING_RELAY_PIN, OUTPUT);
pinMode(HEATING_LED_PIN, OUTPUT);
pinMode(COOLING_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT); // Initialize buzzer pin
pinMode(OXYGEN_SENSOR_PIN, INPUT); // Initialize oxygen sensor pin (potentiometer)
pinMode(OXYGEN_LED_PIN, OUTPUT); // Initialize LED pin to simulate oxygen pump
feederServo.attach(SERVO_PIN);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Smart Aquarium");
delay(2000);
lcd.clear();
}
void loop()
{
unsigned long currentMillis = millis(); // Get the current time
// Temperature Control
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
// Control the heating element based on temperature thresholds
if (temperature < TEMP_THRESHOLD_LOW)
{
digitalWrite(HEATING_RELAY_PIN, HIGH); // Turn on the heating relay (and heating element)
digitalWrite(HEATING_LED_PIN, HIGH); // Turn on the LED to simulate heating
}
else
{
digitalWrite(HEATING_RELAY_PIN, LOW); // Turn off the heating relay (and heating element)
digitalWrite(HEATING_LED_PIN, LOW); // Turn off the LED to simulate heating
}
// Control the cooling element based on temperature thresholds
if (temperature > TEMP_THRESHOLD_HIGH)
{
digitalWrite(COOLING_RELAY_PIN, HIGH); // Turn on the cooling relay (and cooling element)
digitalWrite(COOLING_LED_PIN, HIGH); // Turn on the LED to simulate cooling
}
else
{
digitalWrite(COOLING_RELAY_PIN, LOW); // Turn off the cooling relay (and cooling element)
digitalWrite(COOLING_LED_PIN, LOW); // Turn off the LED to simulate cooling
}
// Check water level using push button
waterLevelLow = digitalRead(WATER_LEVEL_PIN) == LOW;
// Check if water level has changed
if (waterLevelLow)
{
// Water level is low
lcd.setCursor(0, 0);
lcd.print("Please ADD WATER!!!");
delay(2000);
lcd.clear();
buzzerState = !buzzerState; // Toggle buzzer state
digitalWrite(BUZZER_PIN, buzzerState ? HIGH : LOW); // Turn on/off buzzer
}
else
{
// Water level is okay
if (buzzerState)
{
// Turn off buzzer if it was buzzing
buzzerState = false;
digitalWrite(BUZZER_PIN, LOW);
}
}
// Simulate day-night lighting cycle
int hour = (millis() / 1000 / 60) % 24; // Simulate hour of the day
if (hour >= 1 && hour < 2) {
digitalWrite(LIGHT_PIN, HIGH); // Daytime
}
else
{
digitalWrite(LIGHT_PIN, LOW); // Nighttime
}
// Read oxygen level from potentiometer
int oxygenSensorValue = analogRead(OXYGEN_SENSOR_PIN);
float oxygenLevel = map(oxygenSensorValue, 0, 1023, 0, 100); // Simulate oxygen level from 0 to 100%
// Update LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Water: ");
lcd.print(waterLevelLow ? "Low" : "Ok");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("O2: ");
lcd.print(oxygenLevel);
lcd.print("%");
// Simulate oxygen pump
digitalWrite(OXYGEN_LED_PIN, oxygenLevel > 80 ? LOW : HIGH); // Turn off the LED if oxygen level is above 50%
delay(2000); // Main loop delay
lcd.clear();
}
void feedFish()
{
lcd.setCursor(0, 0);
lcd.print("Feeding fish...");
delay(2000); // Main loop delay
feederServo.write(90); // Rotate servo to dispense food
delay(2000); // Adjust as needed
feederServo.write(0); // Return to initial position
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Done feeding.");
delay(2000);
lcd.clear();
}