this is the main sketch which i hope to clean up and integrate the receive side into .
it includes a water change element
a heater control
timers for all the usual stuff
led indicators for pretty much everything
and i will be trying to add a tft screen later on .
i am the first to admit this is a mess , it is my first attempt and im trying to learn as much as i can .
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
// Data wire is plugged into pin 50 on the Arduino
#define ONE_WIRE_BUS 50
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//12v Relay Unit
const int drainPin = 22; //12v relay module 1 pin 1
const int fillerPin = 24; //12v relay module 1 pin 2
const int moonlightPin = 26; //12v relay module 1 pin 3
const int coPin = 28; //12v relay module 1 pin 4
//240v Relay Unit
const int filterPin = 23; // relay module 2 pin 1
const int heaterPin = 25; //relay module 2 pin 2
const int daylightPin = 27; // relay module 2 pin 3
const int airpumpPin = 29; // relay module 2 pin 4
//Indicator Control Unit
const int buzzerPin = 31;
const int heatledPin = 32;
const int filterledPin = 34;
const int drainledPin = 36;
const int fillledPin = 38;
const int dayledPin = 40;
const int nightledPin = 33;
const int airledPin = 35;
const int coledPin = 37;
const int fullPin = 39;
const int lowPin = 41;
//Inputs
const int startPin = 44;
const int lowfloPin = 46;
const int highfloPin = 48;
const int dayonPin = 51;
const int dayoffPin = 53;
//52 temp
int startState = 0;
int overideState = 0;
int lowfloState = 0;
int highfloState = 0;
int dayonState = 0;
int dayoffState = 0;
void setup() {
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
Wire.begin();
rtc.begin();
if (! rtc.isrunning())
{
Serial.println("RTC is NOT running!");
}
pinMode(heaterPin, OUTPUT);
pinMode(filterPin, OUTPUT);
pinMode(drainPin, OUTPUT);
pinMode(fillerPin, OUTPUT);
pinMode(daylightPin, OUTPUT);
pinMode(moonlightPin, OUTPUT);
pinMode(airpumpPin, OUTPUT);
pinMode(coPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(heatledPin, OUTPUT);
pinMode(filterledPin, OUTPUT);
pinMode(drainledPin, OUTPUT);
pinMode(fillledPin, OUTPUT);
pinMode(dayledPin, OUTPUT);
pinMode(nightledPin, OUTPUT);
pinMode(airledPin, OUTPUT);
pinMode(coledPin, OUTPUT);
pinMode(fullPin, OUTPUT);
pinMode(lowPin, OUTPUT);
pinMode(startPin, INPUT_PULLUP);
pinMode(lowfloPin, INPUT_PULLUP);
pinMode(highfloPin, INPUT_PULLUP);
digitalWrite(dayonPin, INPUT_PULLUP);
digitalWrite(dayoffPin, INPUT_PULLUP);
digitalWrite(drainPin, HIGH);
digitalWrite(fillerPin, HIGH);
digitalWrite(heaterPin, HIGH);
digitalWrite(filterPin, HIGH);
}
void loop() {
startState = digitalRead(startPin);
lowfloState = digitalRead(lowfloPin);
highfloState = digitalRead(highfloPin);
if (startState == LOW){// Press a Start Button to start the cycle
digitalWrite(drainPin, LOW); //which operates the drain pump and valve
digitalWrite(drainledPin, HIGH);
Serial.println();
Serial.print("Starting Water Change");}
else{
heatfiltcont();
tempalert();
timersettings();}
if (lowfloState == LOW){ // when the water level drops the float switch closes
digitalWrite(drainPin, HIGH); //shutting off the drain
digitalWrite(drainledPin, LOW);
digitalWrite(fillerPin, LOW); // and opening the filler solenoid
digitalWrite(fillledPin, HIGH);
Serial.println();
Serial.print("Water Low - Filling");}
if (highfloState == HIGH){ // when the water level is restored
digitalWrite(fillerPin, HIGH); // the filler is shut off .
digitalWrite(fillledPin, LOW);
Serial.println();
Serial.print("Full");
}
if (highfloState == HIGH){
digitalWrite(fullPin, HIGH);
}else{
digitalWrite(fullPin, LOW);
}
if (lowfloState == LOW){
digitalWrite(lowPin, HIGH);
}else{
digitalWrite(lowPin, LOW);
}
}
void heatfiltcont() { //Switches the Heater and Filter off when the water level drops , and back on when the level is sufficient
highfloState = digitalRead(highfloPin);
if (highfloState == LOW) { //if the aquarium is not full the float switch closes
digitalWrite(heaterPin, HIGH); // the heater shuts off
digitalWrite(filterPin, HIGH); // the filter shuts off
digitalWrite(heatledPin,LOW);
digitalWrite(filterledPin, LOW);
Serial.println();
Serial.print("Heater and Filter Off");
}else{
digitalWrite(filterPin, LOW);
digitalWrite(filterledPin, HIGH);
}
}
void tempalert() { //Temperature sensor heater off if temp above 27 and on if below 25 unless high float switch is closed and buzzer if below 19 and high float is open
highfloState = digitalRead(highfloPin);
Serial.println();
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperature readings
Serial.println();
Serial.println("DONE");
Serial.print("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
// You can have more than one DS18B20 on the same bus.
// 0 refers to the first IC on the wire
Serial.println();
delay(10);
if(sensors.getTempCByIndex(0)>= 27){
digitalWrite(heaterPin, HIGH);//turns off=HIGH
digitalWrite(heatledPin, LOW);
Serial.print("Too Hot , Heater Off");
}
if((sensors.getTempCByIndex(0)<= 23) && (highfloState == HIGH)){
digitalWrite(heaterPin, LOW);//turns on=LOW
digitalWrite(heatledPin, HIGH);
Serial.println();
Serial.print("Too Cold , Heater On");
}
if((sensors.getTempCByIndex(0)<= 19) && (highfloState == HIGH)){
digitalWrite(buzzerPin, HIGH);
Serial.println();
Serial.print("WARNING!!! Temperature Very Low");
}
}
void timersettings() { //goal 3 timers(); Set timers for day/night lights and air/co2 with daylight override switch
DateTime now = rtc.now();
dayonState = digitalRead(dayonPin);
dayoffState = digitalRead(dayoffPin);
Serial.println();
Serial.println();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
delay(10);
//Daylights daylightPin dayledPin
if (now.hour() == 9 & now.minute() == 0 & now.second() == 0)
{
digitalWrite(daylightPin, LOW);
digitalWrite(dayledPin, HIGH);
}
else if (now.hour() == 18 & now.minute() == 0 & now.second() == 0)
{
digitalWrite(daylightPin, HIGH);
digitalWrite(dayledPin, LOW);
}
if (dayonState == LOW)
{
digitalWrite(daylightPin, LOW);
digitalWrite(dayledPin, HIGH);
}
if (dayoffState == LOW)
{
digitalWrite(daylightPin, HIGH);
digitalWrite(dayledPin, LOW);
}
//Nightlights moonlightPin nightledPin
if (now.hour() == 7 & now.minute() == 30 & now.second() == 0)
{
digitalWrite(moonlightPin, LOW);
digitalWrite(nightledPin, HIGH);
}
if (now.hour() == 9 & now.minute() == 0 & now.second() == 0)
{
digitalWrite(moonlightPin, HIGH);
digitalWrite(nightledPin, LOW);
}
if (now.hour() == 18 & now.minute() == 0 & now.second() == 0)
{
digitalWrite(moonlightPin, LOW);
digitalWrite(nightledPin, HIGH);
}
if (now.hour() == 23 & now.minute() == 0 & now.second() == 0)
{
digitalWrite(moonlightPin, HIGH);
digitalWrite(nightledPin, LOW);
}
//Airpump airpumpPin airledPin
if (now.hour() == 19 & now.minute() == 0 & now.second() == 0)
{
digitalWrite(airpumpPin, LOW);
digitalWrite(airledPin, HIGH);
}
if (now.hour() == 22 & now.minute() == 0 & now.second() == 0)
{
digitalWrite(airpumpPin, HIGH);
digitalWrite(airledPin, LOW);
}
//co2solenoid coPin coledPin
if (now.hour() == 7 & now.minute() == 0 & now.second() == 0)
{
digitalWrite(coPin, LOW);
digitalWrite(coledPin, HIGH);
}
if (now.hour() == 17 & now.minute() == 0 & now.second() == 0)
{
digitalWrite(coPin, HIGH);
digitalWrite(coledPin, LOW);
}
}