How to Request time from RTC 1307

GolamMostafa:
(5) You can also implement your decrement function? How? the RTC is always clocking up and not down?

hmm... i think it would be ideal if i make a function that would have a for loop inside and delay will be 24 hours which will complete one cycle and then make a trigger for example with a press of a button reset value to 22 and decrement with 24 hour delay however this would be useless without the arduino actually remembering which number it was on the last time it turned off. so i thought about making a last recorded variable or maybe the for loop variable can be used itself. the variable will be stored in the eeprom of the arduino which with every wake up gets the value and resume the count from that. i make it sounds easy but i am kinda scared that it is going to take a lot of coding.

so that was my plan i came up with yesterday.

anyway. your code really helped cause of the comments. however there are some statements where the comment is missing if you don't mind could you clear it for me?

Lets for a second forget i am trying to display it to the lcd cause that makes it a little complicated for me. i want to start slow. i understand from the i2C bus system that i first have to send a call to the slave device which is the i2c address set to 0x68.. after that i have to select the register and then express how many bytes i want.. and after expressing that i will have to request the bytes and next step is the rtc stores in bcd so once the data is send i have to convert it to bcd. i hope i am on point till here and you are with me.

If i write the the most basic sketch which does only that. after trying, i came up with this:

I am trying to only read from the clock since it is already set. however if i understand the code i can also set it later without library btw will writing the code without library consume less space on the flash? moving on..

#define rtcAddress 0x68    //i am setting the the slave device address to send right after establishing wire 
                                        //  connection

#define secRegister 0x00  //defining the secs register in the slave device. from the datasheet i understand 
                                       //that this is the sec register.my question is don't i have to define each register? 
                                      // sec, hours, mins, etc?  

voide setup() {
 
Wire.begin();  //establishing a connection line between i2c device at default speed

serial.begin(9600);  //starting serial monitor to view the data for simplification purpose

Wire.beginTransmission(rtcAddress);   //sending out the address frame/bits to the slave device. if it is true 
                                                        // the slave device will send out an ack bit (if i am not wrong)

wire.write(secRegister);    //in here you are i believe writing to it does it work like if i start a write 
                                       //transmission it will also read from it? 

//And you said the other register are auto selected. i am referring to the (reading time from rtc part of //code) and then 

Wire.endTransmission(); //you said that it executes all the queued processes are going to be executed. so //does that mean if i dont want to set the time again n again i will still have to set it anyway? if for example //i dont queue those writing to the registers i can't select the seconds register? i am sorry i am little //confused here. 



lcd.write((bcdHours>>4) + 0x30);  // i am not sure how this converts bcd to decimal and what that 0x30 is

lcd.write((bcdHours & 0x0F) + 0x30);  //this confuses me even more if the above commands converts it //and display it do not know what this does. could it be that these are the lcd addresses? since you included //it in your code?



//i am forced to use the wire library now since whenever i try to print the time to it sometimes my code //works which works with the rtc library but sometimes it does not.

my current code is.

void setup() {
  // put your setup code here, to run once: 
  
  Serial.begin(9600); //estalblishes connection
  rtc.begin(); //starts rtc  (built in function in library)
  lcd.init(); 
  lcd.init();
  lcd.clear();
  }
void loop() {

  DateTime now = rtc.now();      //built in function necessary do not know why
  lcd.setCursor(0,0);    //setting starting cursor point 
  lcd.print("Time = ");   //printing the string time
  h= now.hour();        //puts the hour functions value into h local variable which 
  m= now.minute();      //puts into m 
  s= now.second();      //same 
  lcd.print(h);
  lcd.print(":");     
  lcd.print(m);       //printing the variables value whatever it is to the serial monitor
  lcd.print(":");
  lcd.print(s);
  delay(1000);     //the started to show flow like effect in lcd it kinda scrolled from top to bottom so i added 
                            this delay. 
  lcd.clear();       //clears the lcd to not print it again n again
  
}

I know this code is really bad this is like my first try ever. if you guys could suggest how to improve the code that would be awesome. also i tried to make a function out of this but i can't seem to figure out how i can print a single line which would contain all the variables in it.

My function looks the same as above which did not work. i am so embarrassed to share this but i guess that is how you learn. NOTE: i have no coding experience except digitalWrite(13, HIGH); XD

it does work when i type it like the above code. but when i try to call the function like this

Serial.print(ShowTime());

also i think it will be better to stick this function into a variable and then call it but i just do not know how to put 3 variables into a single variable.

My function:

void ShowTime() {               //handy little function custom made to just  call upon the rtc request time as 
  h, s, m and puts it into some variables to be used later
  DateTime now = rtc.now();      //built in function necessary do not know why
  lcd.print("Time = ");
  h= now.hour();        //puts the hour functions value into h variable 
  m= now.minute();      //puts into m 
  s= now.second();      //same 
  lcd.print(h);
  lcd.print(":");     
  lcd.print(m);       //printing the variables value whatever it is to the serial monitor
  lcd.print(":");
  lcd.print(s);
  
  }