problem with real clock time

Hi friend, I want to automize my acquarium so I’m working on this project.

  1. Automatic check of the temperature
    2)Automatic turn on and turn off illumination
    3)Other sensors to read some parameters
    But my real problem is the RTC. I can’t set the time and I don’t know how I can compare two times to turn on or turn off the illumination at a predeterminated hour.
    Can you help me, please?
    This is my writing:

#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,10,4,3,2);

int rele_out1 = 5;
int rele_out2 = 6;
int pulsantepiu = 0;
int pulsantemeno = 1;
int pulsanteok = 7;
int pulsantenext = 8;
int tmax;
int temperatura;
int piu=0;
int meno=0;
int ok=0;
int next=0;
int posizione=0;
char* menu_principale[6] = {“Pagina iniziale”,“Temperatura max”,“Temperatura min”,“Orario on”,“Orario off”,“Imposta ora”};

byte grado[8] = {
0b00010,
0b00101,
0b00010,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
};

void setup(){
Serial.begin(57600);

temperatura = analogRead(A3)*0.48828125; //Coversione temperatura rilevata dal sensore LM35
lcd.createChar(0, grado);
lcd.begin(16,2);
lcd.print( menu_principale[posizione] );
pinMode(rele_out1, OUTPUT);
pinMode(rele_out2, OUTPUT);
pinMode(pulsantepiu, INPUT);
pinMode(pulsantemeno, INPUT);
pinMode(pulsanteok, INPUT);
pinMode(pulsantenext, INPUT);
tmax=1;
}

void loop(){
next=digitalRead(pulsantenext);
if(next == HIGH){
posizione = posizione ++;
if(posizione == 2)
piu = digitalRead(pulsantepiu);
meno = digitalRead(pulsantemeno);
ok = digitalRead(pulsanteok);
if (piu == HIGH){
tmax = tmax ++;
lcd.clear();
lcd.print(“Temperatura di”);
lcd.setCursor(0, 1);
lcd.print("soglia: ");
lcd.setCursor(10, 1);
lcd.print(tmax,DEC);
lcd.write((byte)0);
lcd.print(“C”);
delay(1000);
}
if (meno == HIGH){
tmax = tmax --;
lcd.clear();
lcd.print(“Temperatura di”);
lcd.setCursor(0, 1);
lcd.print("soglia: ");
lcd.setCursor(10, 1);
lcd.print(tmax,DEC);
lcd.write((byte)0);
lcd.print(“C”);
delay(1000);
}
} else if(posizione == 3){
} else if(posizione == 4){
} else if(posizione == 5){
} else if(posizione == 1){
} else if(posizione == 6){
}
if(temperatura > (tmax + 1)){
lcd.clear();
lcd.print(“Termostato spento!”);
digitalWrite(rele_out1, LOW);
} else{
digitalWrite(rele_out1, HIGH);
}
}

But my real problem is the RTC. I can't set the time and I don't know how I can compare two times to turn on or turn off the illumination at a predeterminated hour.

Why can't you? Where are you trying? What kind of RTC is it?

I have erased that part, because I don't understand it, I use RTC DS1307.
In the "menu_principale[6]" I want to set the time. And then in the "menu_principale[4]" I want to compare the hours.
How can I make it?

First, download the RTC library from Adafruit:

Then add this code

#include <Wire.h>
#include <RTClib.h>

RTC_Millis rtc;
DateTime currentDateTime;
byte timeBuffer[8];


void setup()
{
  Wire.begin();                           // Initiate the Wire library and join the I2C bus as a master

 //---------------------COMMENT OUT THESE 4 STATEMENTS ON THE SECOND TIME -----
  Serial.begin(9600); // Initiate serial communication
  rtc.begin(DateTime(__DATE__, __TIME__));
  currentDateTime = rtc.now();
  SetStartingClockValues();
//--------------------------
}

/*****
  This function reads the system clock of the host PC and translates those time and date values
  for use by the RTC. This involves reading the date and time values from the PC via the now()
  method from the RTClib library and copying those values into the timeBuffer[] array. Because
  the DS1307 wants its data in BCD format, the timeBuffer[] data are converted to BCD format. The 
  RTClib library is available from: https://github.com/adafruit/RTClib.
  
  Parameter list:
    void
    
  Return value:
    void
    
  CAUTION: This function assumes that the DateTime variable named currentDateTime exists prior 
  to the function call.
*****/
void SetStartingClockValues()
{   
  timeBuffer[MONTH]    = currentDateTime.month();             // Month (1-12)
  timeBuffer[DATE]     = currentDateTime.day();               // Day (1-31)
  timeBuffer[YEAR]     = currentDateTime.year() - MILLENIA;   // Year (need as 0-99, no centuries)
  timeBuffer[DAY]      = currentDateTime.dayOfWeek();         // Day of week (1-7)
  timeBuffer[HOURS]    = currentDateTime.hour();              // Hour (0-23)
  timeBuffer[MINUTES]  = currentDateTime.minute();            // Minutes (0-59)
  timeBuffer[SECONDS]  = currentDateTime.second();            // Seconds (0-59)
 
  Wire.beginTransmission(RTCI2CADDRESS);        // Select RTC on the I2C bus
  Wire.write(0);                                // Reset internal register pointer                     
  Wire.write(DecToBcd(timeBuffer[SECONDS]));    // Second
  Wire.write(DecToBcd(timeBuffer[MINUTES]));    // Minute
  Wire.write(DecToBcd(timeBuffer[HOURS]));      // Hour
  Wire.write(DecToBcd(timeBuffer[DAY]));        // Weekday
  Wire.write(DecToBcd(timeBuffer[DATE]));       // Day
  Wire.write(DecToBcd(timeBuffer[MONTH]));      // Month (with century bit = 0)
  Wire.write(DecToBcd(timeBuffer[YEAR]));       // Year
  Wire.write(0);                        
  result = Wire.endTransmission();
  if (result) 
    SetError(RTCWRITEERROR);
}

The setup() code initializes the internal DS1307 registers with the host PC’s data and time data. Once you have compiled and uploaded that version of the code, comment out the 4 lines that begin with Serial.begin(9600); The reason is because any time you use the Serial communications link, it causes the DS1307 to see it as a reset and it freezes the register values. Therefore, if you don’t comment those lines out, when you apply power a second time, it recalls the old register values that were stored away when it thought there was a reset. If you comment those lines out, recompile and upload to your board, the program keeps the time and date values from the first compile in the DS1307 registers. Now when you remove power, it uses its onboard battery to power the chip. When you reapply power (e.g., from a wall wart), it calls up the register values that have been ticking away the entire time external power was removed.

Ok now I have an error, you can see in my attachement.
I have write this code:

#include <RTClib.h>
#include <Wire.h>
#include <LiquidCrystal.h>

RTC_Millis rtc;
DateTime currentDateTime;
byte timeBuffer[8];
LiquidCrystal lcd(12,11,10,4,3,2);

int rele_out1 = 5;
int rele_out2 = 6;
int pulsantepiu = 0;
int pulsantemeno = 1;
int pulsanteok = 7;
int pulsantenext = 8;
int tmax;
int temperatura;
int piu=0;
int meno=0;
int ok=0;
int next=0;
int posizione=0;
char* menu_principale[6] = {“Pagina iniziale”,“Temperatura max”,“Temperatura min”,“Orario on”,“Orario off”,“Imposta ora”};

byte grado[8] = {
0b00010,
0b00101,
0b00010,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
};

void setup(){
Wire.begin();
Serial.begin(9600);
rtc.begin(DateTime(DATE, TIME));
currentDateTime = rtc.now();
SetStartingClockValues();
temperatura = analogRead(A3)*0.48828125; //Coversione temperatura rilevata dal sensore LM35
lcd.createChar(0, grado);
lcd.begin(16,2);
lcd.print( menu_principale[posizione] );
pinMode(rele_out1, OUTPUT);
pinMode(rele_out2, OUTPUT);
pinMode(pulsantepiu, INPUT);
pinMode(pulsantemeno, INPUT);
pinMode(pulsanteok, INPUT);
pinMode(pulsantenext, INPUT);
tmax=1;
}

void SetStartingClockValues()
{
timeBuffer[MONTH] = currentDateTime.month(); // Month (1-12)
timeBuffer[DATE] = currentDateTime.day(); // Day (1-31)
timeBuffer[YEAR] = currentDateTime.year() - MILLENIA; // Year (need as 0-99, no centuries)
timeBuffer[DAY] = currentDateTime.dayOfWeek(); // Day of week (1-7)
timeBuffer[HOURS] = currentDateTime.hour(); // Hour (0-23)
timeBuffer[MINUTES] = currentDateTime.minute(); // Minutes (0-59)
timeBuffer[SECONDS] = currentDateTime.second(); // Seconds (0-59)

Wire.beginTransmission(RTCI2CADDRESS); // Select RTC on the I2C bus
Wire.write(0); // Reset internal register pointer
Wire.write(DecToBcd(timeBuffer[SECONDS])); // Second
Wire.write(DecToBcd(timeBuffer[MINUTES])); // Minute
Wire.write(DecToBcd(timeBuffer[HOURS])); // Hour
Wire.write(DecToBcd(timeBuffer[DAY])); // Weekday
Wire.write(DecToBcd(timeBuffer[DATE])); // Day
Wire.write(DecToBcd(timeBuffer[MONTH])); // Month (with century bit = 0)
Wire.write(DecToBcd(timeBuffer[YEAR])); // Year
Wire.write(0);
result = Wire.endTransmission();
if (result)
SetError(RTCWRITEERROR);
}

void loop(){
next=digitalRead(pulsantenext);
if(next == HIGH){ //Con questo ciclo incremento
posizione = posizione ++;
if(posizione == 2)
piu = digitalRead(pulsantepiu);
meno = digitalRead(pulsantemeno);
ok = digitalRead(pulsanteok);
if (piu == HIGH){
tmax = tmax ++;
lcd.clear();
lcd.print(“Temperatura di”);
lcd.setCursor(0, 1);
lcd.print("soglia: ");
lcd.setCursor(10, 1);
lcd.print(tmax,DEC);
lcd.write((byte)0);
lcd.print(“C”);
delay(1000);
}
if (meno == HIGH){
tmax = tmax --;
lcd.clear();
lcd.print(“Temperatura di”);
lcd.setCursor(0, 1);
lcd.print("soglia: ");
lcd.setCursor(10, 1);
lcd.print(tmax,DEC);
lcd.write((byte)0);
lcd.print(“C”);
delay(1000);
}
} else if(posizione == 3){
} else if(posizione == 4){
} else if(posizione == 5){
} else if(posizione == 1){
} else if(posizione == 6){
}
if(temperatura > (tmax + 1)){
lcd.clear();
lcd.print(“Termostato spento!”);
digitalWrite(rele_out1, LOW);
} else{
digitalWrite(rele_out1, HIGH);
}
}

There are several global variables and functions used in my code that you need to define somewhere. The RTCWRITEERROR is a symbolic constant:

#define RTCWRITEERROR   1

Variable result is just a byte variable defined globally, so it's defined before setup():

byte result;

You can write an empty setError() function:

void SetError() {
}

From your questions, it appears you need some basic help with programming. In a totally shameful plug, perhaps consider my book Beginning C for Arduino. You can read the reviews on Amazon.com.