#include <Wire.h>
#include <I2C_RTC.h>
static DS3231 RTC;
int PumpOn = HIGH;
int PumpOff = LOW;
int ValveOpen = HIGH;
int ValveClosed = LOW;
int ValveA = 5;
int ValveB = 2;
int PumpRly = 6;
//create a container for events
struct event {uint8_t hour, uint8_t minute, uint8_t pin, uint8_t state};
// and an array of those events
event events[] = {
{6, 0, ValveA, ValveOpen}, //To open valve A at 6
{6, 15, ValveA, ValveClosed}, //To close valve A at 6:15
{7, 0, ValveB, ValveOpen}, //To open valve B
{7, 15, ValveB, ValveClosed}, //To close valve B
{8, 0, ValveA, ValveOpen}, //To open valve A
{8, 15, ValveA, ValveClosed}, //To close valve A
{9, 0, ValveB, ValveOpen}, //To open valve B
{9, 15, ValveB, ValveClosed}, //To close valve B
{10, 0, ValveA, ValveOpen}, //To open valve A
{10, 15, ValveA, ValveClosed}, //To close valve A
{11, 0, ValveB, ValveOpen}, //To open valve B
{11, 15, ValveB, ValveClosed} //To close valve B
{12, 0, ValveA, ValveOpen}, //To open valve A
{12, 15, ValveA, ValveClosed}, //To close valve A
{13, 0, ValveB, ValveOpen}, //To open valve B
{13, 15, ValveB, ValveClosed}, //To close valve B
{14, 0, ValveA, ValveOpen}, //To open valve A
{14, 15, ValveA, ValveClosed}, //To close valve A
{15, 0, ValveB, ValveOpen}, //To open valve B
{15, 15, ValveB, ValveClosed}, //To close valve B
{16, 0, ValveA, ValveOpen}, //To open valve A
{16, 15, ValveA, ValveClosed}, //To close valve A
{17, 0, ValveB, ValveOpen}, //To open valve B
{17, 15, ValveB, ValveClosed}, //To close valve B
{18, 0, ValveA, ValveOpen}, //To open valve A
{18, 15, ValveA, ValveClosed}, //To close valve A
{19, 0, ValveB, ValveOpen}, //To open valve B
{19, 15, ValveB, ValveClosed} //To close valve B
};
void setup() {
Serial.begin(9600);
RTC.begin();
pinMode(ValveA, OUTPUT);
pinMode(ValveB, OUTPUT);
pinMode(PumpRly, OUTPUT);
Serial.println("Hello! Setup is complete.");
}
void loop() {
int PinState = digitalRead(PumpRly); //the digital read must be aligned with the chosen definition of the pump state. for example, a HIGH might be on or off.
int LevelSense = analogRead(A0);
float voltage = LevelSense * (5.0 / 1023.0);
int Month = (RTC.getMonth());
int Day = (RTC.getDay());
int Hour = (RTC.getHours());
int Minute = (RTC.getMinutes());
int Second = (RTC.getSeconds());
bool printflag = 0;
//Pump control; turns pump on if level sensor drops below 2.00v and keeps it on until it rises to 2.40v
//only flags printout if something is changed
if (voltage <= 2.00) {
if (PinState == PumpOff) {
digitalWrite (PumpRly, PumpOn);
printflag = 1;
}//end of turnon
}//end of low voltage response
if (voltage > 2.4) {
if (PinState == PumpOn) {
digitalWrite (PumpRly, PumpOff);
printflag = 1;
}//end of shutoff
}//end of high voltage response
//end of pump control code
//valve event code:
for (int index = 0; index < sizeof(events) / sizeof(events[0][]); index++) { //loop through the array, process any line that matches the present time
//if the time specified matches current time and the output needs changing, assert the valve state and print a report
if (Hour == events[index].hour && Minute == events[index].minute && digitalRead(events[index].pin)] != events[index].state) {
digitalWrite(events[index][2], events[index][3]);
printflag = 1;
}//end of conditional code
}//end of for loop
//end of event code
if (printflag) printaction();
}//end loop
void printaction() {
Serial.print(Month);
Serial.print("/");
Serial.print(Day);
Serial.print(" ");
Serial.print(Hour);
Serial.print(":");
Serial.print(Minute);
Serial.print(":");
Serial.print(Second);
Serial.print(" A-");
Serial.print(digitalRead(ValveA));
Serial.print(" B-");
Serial.print(digitalRead(ValveB));
Serial.print(" Pmp-");
Serial.print(digitalRead(PumpRly));
Serial.print(" Lvl-");
Serial.print(voltage);
Serial.println();
}//end printaction