adding or subtracting an hour, rtc stuff

so first off im horrible at explaing myself

heres my thing

#include "RTClib.h"
#include <Wire.h>
#include <LiquidCrystal.h>
DS3231 rtc;






///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Pins  /////////////////////////////////////////////


//water pump relay pin
#define waterpump 5

//Light 1 pin
#define light1 3

//Light 2 pin
#define light2 4

#define circ 2

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Misc /////////////////////////////////////////////////







////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Water ///////////////////////////////////////


//amount of time water is on for; watering length
const int watertime = 5000;


//water pump time on first time
const int pumpOnHour1 = 23;
const int pumpOnMinute1 = 59;
const int pumpOnSecond1 = 1;

//water pump time on second time
const int pumpOnHour2 = 14;
const int pumpOnMinute2 = 22;
const int pumpOnSecond2 = 1;

//water pump time on third time
const int pumpOnHour3 = 14;
const int pumpOnMinute3 = 23;
const int pumpOnSecond3 = 1;

//water pump time on forth time
const int pumpOnHour4 = 14;
const int pumpOnMinute4 = 24;
const int pumpOnSecond4 = 1;





////////////////////////////////////////////////////////////////////////////////////////////////////////lights/////////////////////////////////////

//Light 1
//on
const int light1OnHour = 12;
const int light1OnMinute = 1;
//off
const int light1OffHour = 24;
const int light1OffMinute = 1;



//Light 2
//on
const int light2OnHour = 12;
const int light2OnMinute = 6;
//off
const int light2OffHour = 24;
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)

}





void loop() {
  water();
  lights();
  circulation();
}







void water()
{
  static unsigned long wateron = 0;
  DateTime now = rtc.now();
  // serial print out of time
  char buf[100];
  strncpy(buf, "DD.MM.YYYY  hh:mm:ss\0", 100);
  Serial.println(now.format(buf));


  if    ((now.hour() == pumpOnHour1 && now.minute() == pumpOnMinute1 && now.second() == pumpOnSecond1)
         || (now.hour() == pumpOnHour2 && now.minute() == pumpOnMinute2 && now.second() == pumpOnSecond2)
         || (now.hour() == pumpOnHour3 && now.minute() == pumpOnMinute3 && now.second() == pumpOnSecond3)
         || (now.hour() == pumpOnHour4 && now.minute() == pumpOnMinute4 && now.second() == pumpOnSecond4))
  {
    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");
    }
  }
}







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);
  }

}









void circulation()
{
  DateTime now = rtc.now();



  if    (now.hour()  == pumpOnHour1) && now.minute() == pumpOnMinute1)

  {
    digitalWrite(circ, HIGH);
  }


  if    (now.hour()  == pumpOffHour1 && now.minute() == pumpOffMinute1)

  {
    digitalWrite(circ, LOW);
  }
}

in this spot

void circulation()
{
  DateTime now = rtc.now();



  if    (now.hour()  == pumpOnHour1) && now.minute() == pumpOnMinute1)

  {
    digitalWrite(circ, HIGH);
  }


  if    (now.hour()  == pumpOffHour1 && now.minute() == pumpOffMinute1)

  {
    digitalWrite(circ, LOW);
  }
}

i want my circulation pump to turn on an hour before, and turn off 30 min after my water pump does

what i cant figure out is how to add a +1 or -30 into there respected place,

so like

if
(now.hour() == pumpOnHour1 -1)
{digitalWrite(circ, HIGH);

where, how and WHY do i put my time alteration ?

The simplest approach is to convert all times of day to seconds past midnight (as a long integer); especially if the RTC is in 24 hour mode.

Then it is easy to turn a pump on when the integer time is equal to or greater than a certain value, and off again at a later time.

i bet if you seen my face just now you woulda laughed,

i wont lie, im super weak in this stuff and im trying my hardest to learn from reverse engineering, from what i need to learn,

and what you said my friend went right over my head, kinda like switch case things.

i thought it would be as easy as like

if
((the time now = the time your pumps on) -10)

like what i wanted was just to set the pump on time and the circulation pump to use the time from the water pump and its modded value,

i could set the time for the circ pump but i felt my idea was more "user friendly" as i probably wont remember to set it,

so like,

if the time now is equal to the time the pump turns on (minus an hour) the circulation pump turns on

if the time now is equal to the time the pump turns off (plus 30 min) then the circulation pump turns off

Those are a lot of on times and off times. It seems that you should be using an array.

Also, I think that for midnight, you should not be using 24: you should be using 0. This is because the RTC (and, presumably, the RTC library as well) uses 0 for midnight, and you need to be consistent to get things to work.

I have several ways of dealing with time calculations. Any of them would involve repeating the calculation for each on and off time. Repeated calculations are difficult without using loops and arrays.

jokefox:
so like,

if the time now is equal to the time the pump turns on (minus an hour) the circulation pump turns on

if the time now is equal to the time the pump turns off (plus 30 min) then the circulation pump turns off

It appears that three of your "pump on" times (for the water pump) are one minute apart. So, when should your circulation pump turn on and off? Work this out on paper and you might see a problem.

ignore the times guys, they are just there to fill in the blanks and ill input ones that actually are needed later

i was just testing them with an led to see if everything would work

i did not know the thing about 24=0 though so thats good to know thanks

variable array confuses me but its something i want to get to later when i clean up,

for now i need to know how to minus or add X amount of time to the time my pump turns on value

Here is how to do it using arrays.

Declare pump times:

const int NUM_PUMP_ON_TIMES = 4;

const int pumpOnTime[NUM_PUMP_ON_TIMES][3] = {{23, 59, 1}, {14, 22, 1}, {14, 23, 1}, {14, 24, 1}};

Turn the water pump on at the right time:

  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();
    }
  }

Of course, you will still need the code to turn the water pump off at the right time, which you already have:

  if (digitalRead(waterpump) == HIGH)
  {
    if (millis() - wateron > watertime)
    {
      digitalWrite(waterpump, LOW);
      Serial.println("water pump is off");
    }
  }

thats actually so much better lol,

but how would i do the adding or subtracting time to the pumpon1 for the circulation pump

jokefox:
thats actually so much better lol,

but how would i do the adding or subtracting time to the pumpon1 for the circulation pump

The same way you would do it on paper.

Here is one way to do it:

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; // move time to one hour earlier
if (hrOn < 0) {
  // in case our subtraction came out negative
  hrOn = hrOn + 24;
}

// do arithmetic for the "off" time
minOff = minOff + 30;
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;
}

i guess he wasent kidding when he said it would be easier to convert to a number since midnight lol

i thought it would be easier because i seen a (DATE TIME) example where the tcompile time was 10 seconds to slow for the guy so he added a +10 to the seconds to have the exact time, i figured with enough () anything would be possbile

k so i updated my code, with the arrays, and saved alot of bytes actually,
i appreciate that. i also learnt a bit more about variables, now im starting to get this a bit,
i also understand 0 counts as a number with coding,

so now that its updated

if i was to inject odometers suggestion with the way to do it, ,
how would i add a digitalWrite (circpump,HIGH); to that?

and it would go in the "water void" or "circ void"

im assuming it would just be a sub section in the water void becuase its pulling variables from the array?

#include "RTClib.h"
#include <Wire.h>
#include <LiquidCrystal.h>
DS3231 rtc;






///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Pins  /////////////////////////////////////////////


//water pump relay pin
#define waterpump 5

//Light 1 pin
#define light1 3

//Light 2 pin
#define light2 4

#define circ 2

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////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)


}





void loop() {



  printtime();
  water();
  lights();
  circulation();
}



void printtime()
{
  // 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 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] = {{14, 58, 1}, {14, 59, 1}, {15, 0, 1}, {15, 1, 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");
    }

  }




}








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);
  }

}









void circulation()
{
}

what you said my friend went right over my head,

Strong hint: there are 60 seconds in a minute and 60 minutes in a hour.

That works out to 3600 seconds in an hour.

So 8:00 AM is 8 hours past midnight, or 8*3600 = 28800 seconds past midnight.

jremington:
Strong hint: there are 60 seconds in a minute and 60 minutes in a hour.

That works out to 3600 seconds in an hour.

So 8:00 AM is 8 hours past midnight, or 8*3600 = 28800 seconds past midnight.

i like odometers idea lot more, no offence or anything, but i like how i can change the - or + alot quicker in it

if i can figure out how to add the digitalWrite function where i need it to be then im done the circulation pump,

i can figure this out, i just probably need a lunch break,

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);
  }

}

jokefox:

int i = 0; // you will need to make a loop later

Where's your loop?
As things stand, the code for the circulation pump only sees the first time in the pumpOnTime array (because i is always 0). You will need to make it loop through all of the times in that array.

    if  (now.hour() == hrOff && now.minute())

I think you left out something important in that line.