pretty sure i figured it out, man im learning
heres the code updated if anyone ever comes across it.
even just the seconds timer was a huge accomplishment, to get this far is even more amazing,
special thanks to odometer for his help.
to to start the case switch stuff
/*
* hydoponic brain controller,
* started december 2017
*
* controller to control hydroponic system (lights, water, circulation of resevior, more to be updated)
*
* i started this project with no knowledge of arduino, this section is for the
* people who have contributed to this project. if anyone wants to help just send me a message, or help along the way
* big thanks to these people
*
* arduino.cc
* -odometer ** most of the "water (void)" is his coding
* -sherzaad
*/
#include <dht_nonblocking.h>
#include "RTClib.h"
#include <Wire.h>
#include <LiquidCrystal.h>
DS3231 rtc;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Pins /////////////////////////////////////////////
//water pump relay pin
#define waterpump 6
//Light 1 pin
#define light1 3
//Light 2 pin
#define light2 4
//circulation pin
#define circ 5
//dht temp&humid
#define DHT_SENSOR_TYPE DHT_TYPE_22
#define DHT_SENSOR_PIN 2
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Misc /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Water ///////////////////////////////////////
//amount of time water is on for; watering length
const int watertime = 17000;
////////////////////////////////////////////////////////////////////////////////////////////////////////lights/////////////////////////////////////
//Light 1
//on
const int light1OnHour = 12;
const int light1OnMinute = 1;
//off
const int light1OffHour = 0;
const int light1OffMinute = 1;
//Light 2
//on
const int light2OnHour = 12;
const int light2OnMinute = 6;
//off
const int light2OffHour = 0;
const int light2OffMinute = 6;
//////////////////////////////////////////////////////////////// other shit waiting to give me a headache///////////////////////////////////////////////////////////////////
/*Relay4
const int OnHour = ;
const int OnMinute = ;
const int OnSecond = ;
const int OffHour = ;
const int OffMinute = ;
const int OffSecond = ;
//Relay5
const int OnHour = ;
const int OnMinute = ;
const int OnSecond = ;
const int OffHour = ;
const int OffMinute = ;
const int OffSecond = ;
//Relay6
const int OnHour = ;
const int OnMinute = ;
const int OnSecond = ;
const int OffHour = ;
const int OffMinute = ;
const int OffSecond = ;
//Relay7
const int OnHour = ;
const int OnMinute = ;
const int OnSecond = ;
const int OffHour = ;
const int OffMinute = ;
const int OffSecond = ;
//Relay8
const int OnHour = ;
const int OnMinute = ;
const int OnSecond = ;
const int OffHour = ;
const int OffMinute = ;
const int OffSecond = ;
*/
void setup() {
Serial.begin(9600);
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(__DATE__, __TIME__));
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
//set pins (outputs)
pinMode(waterpump, OUTPUT);
pinMode(light1, OUTPUT);
pinMode(light2, OUTPUT);
pinMode(circ, OUTPUT);
//set pins (inputs)
}
//Set DHT22 nnblocking
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
/* Measure once every four seconds. */
if( millis( ) - measurement_timestamp > 4000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return( true );
}
}
return( false );
}
void loop() {
// printtime();
water();
lights();
DHT22();
}
void DHT22()
{
float temperature;
float humidity;
/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
if ( measure_environment( &temperature, &humidity ) == true )
{
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );
// serial print out of time
DateTime now = rtc.now();
char buf[100];
strncpy(buf, "DD.MM.YYYY hh:mm:ss\0", 100);
Serial.println(now.format(buf));
}
}
/*void printtime()
{
//// prints the time HH:MM:SS into the serial monitor (9600 baud)
int period = 5000;
unsigned long time_now = 0;
time_now = millis();
// serial print out of time
DateTime now = rtc.now();
char buf[100];
strncpy(buf, "DD.MM.YYYY hh:mm:ss\0", 100);
Serial.println(now.format(buf));
while (millis() < time_now + period) {}
}
*/
void water()
{
DateTime now = rtc.now();
static unsigned long wateron = 0;
///// number of pump on times
const int NUM_PUMP_ON_TIMES = 4;
//// times the pumps turn on (HH:MM:SS)
const int pumpOnTime[NUM_PUMP_ON_TIMES][3] = {{20, 25, 1}, {20, 26, 1}, {21, 27, 1}, {21,28, 1}};
for (int i = 0; i < NUM_PUMP_ON_TIMES; i++) {
// remember, array indices start from 0
if (now.hour() == pumpOnTime[i][0] && now.minute() == pumpOnTime[i][1] && now.second() == pumpOnTime[i][2])
{
Serial.println("water pump is on");
digitalWrite(waterpump, HIGH);
wateron = millis();
}
}
if (digitalRead(waterpump) == HIGH)
{
if (millis() - wateron > watertime)
{
digitalWrite(waterpump, LOW);
Serial.println("water pump is off");
}
}
int hrOn, minOn, secOn, hrOff, minOff, secOff;
int i = 0; // you will need to make a loop later
// copy the numbers
hrOn = pumpOnTime[i][0];
minOn = pumpOnTime[i][1];
secOn = pumpOnTime[i][2];
hrOff = pumpOnTime[i][0];
minOff = pumpOnTime[i][1];
secOff = pumpOnTime[i][2];
// do arithmetic for the "on" time
hrOn = hrOn - 1; // Change how much sooner the circulation pump turns on, use a negative value (-1 to -23)
if (hrOn < 0) {
// in case our subtraction came out negative
hrOn = hrOn + 24;
}
if (now.hour() == hrOn && now.minute() == minOn)
{
digitalWrite(circ, HIGH);
}
// do arithmetic for the "off" time
minOff = minOff + 30; // change how long the circulation pump stays on after the water pump turns off, use a positive value (+1-59)
if (minOff >= 60) {
// sixty minutes make one hour
minOff = minOff - 60;
hrOff = hrOff + 1;
}
if (hrOff >= 24) {
// in case we went past midnight
hrOff = hrOff - 24;
}
if (now.hour() == hrOff && now.minute())
{
digitalWrite(circ, LOW);
}
}
void lights()
{
DateTime now = rtc.now();
/////light 1//////////////
if (now.hour() == light1OnHour && now.minute() == light1OnMinute)
{ Serial.println("light 1 is on");
digitalWrite(light1, HIGH);
}
if (now.hour() == light1OffHour && now.minute() == light1OffMinute)
{ Serial.println("light 1 is off");
digitalWrite(light1, LOW);
}
////////////light2///////////
if (now.hour() == light2OnHour && now.minute() == light2OnMinute)
{ Serial.println("light 2 is on");
digitalWrite(light2, HIGH);
}
if (now.hour() == light2OffHour && now.minute() == light2OffMinute)
{ Serial.println("light 2 is off");
digitalWrite(light2, LOW);
}
}