Searched the web, for days and days. there are no clear cut answers. Everyone teaches "How to upload the RTC library". I have already set the time of the RTC1307, now i just want to know if there is a function that i can call to import the time to the Arduino and basically sync with it everytime it resets.
If it's not necessary to import the time, and the Arduino does that automatically then i would like to know the function by which i can tell Arduino to display it on the serial monitor or the i2c lcd display. by seeing the example codes, i doubt there is going to be a single line function which does all of that. but if someone could guide me as to how to actually prompt the RTC to send time atleast, then that would be awesome for us beginners.
My project involves keeping the time, showing the time on demand and starting a countdown for 21 days after pressing a button which the Arduino should remember, the time left and time past from the trigger event until the time period of 21 days has passed.
I know it is a lot to ask for but just to give you guys the feel of the entire situation i gave a little more information than needed so that you guys could help me keeping this in mind.
UPDATE- SUCCESS:
Thanks to this forum and passionate people who put up with my noobness i finally figured out a piece of code that does what i wanted. and obviously hours and hours of reading. for people who have the same problem or want to go down the same path as me this is the code i came up with.
#include <Wire.h> //wire library
#include <LiquidCrystal_I2C.h> //i2c lcd library
LiquidCrystal_I2C lcd(0x3F, 16,2); //lcd address rows and cols
int s; //bucket for seconds
int mn; //bucket for mins
int hr; //bucket for hour
int DayOfTheWeek; //day of the week
int Mon; //cucket for month
int yr; //bucket for year
int d; //bucket for day
const int pin=7; //pin number
int buttonState; //either high or low button state
int lastButtonState; //records last button state trying to come with a way to activate certain function by judging the time they have it ON
//maybe create seperate functions to show date and time seperately
void setup() {
Wire.begin(); //starts the wire communication
Serial.begin(9600); //sets up the serial monitor for debugging
lcd.init(); //lcd starting command
lcd.init(); //lcd starting command
lcd.clear(); //lcd clear with every reset
lcd.backlight(); //turns on the backlight
}
void loop() {
buttonState=digitalRead(pin); //puts the read of the button either high or low into the button state variable
if(buttonState==HIGH) { //if the button is pressed then carry out the following instructions
ShowTime(); //show time on the first row
ShowDate(); //show date on the second
// will show the time as long as the delay has not expired specified in the function
};
__________________ Functions in a different tab make it little bit tidy
//____________Get time and set each variable to it in setup
void ShowTime() {
Wire.beginTransmission(0x68); //address
Wire.write(0x00); //register the next ones are going to be automatically selected apparently
Wire.endTransmission();
Wire.requestFrom(0x68, 7);
s= (bcdToDec(Wire.read() & 0x7f));
mn= (bcdToDec(Wire.read()));
hr= (bcdToDec(Wire.read() & 0x3f)); //settings all the reads to corresponding variable to be used
DayOfTheWeek=(bcdToDec(Wire.read()));
d=(bcdToDec(Wire.read()));
Mon= (bcdToDec(Wire.read()));
yr=(bcdToDec(Wire.read())+2000);
lcd.setCursor(0,0);
lcd.print("Time: ");
lcd.print(hr);
lcd.print(":");
lcd.print(mn);
lcd.print(":");
lcd.print(s);
};
//---show date function
void ShowDate() {
Wire.beginTransmission(0x68); //address
Wire.write(0x00); //register the next ones are going to be automatically selected apparently
Wire.endTransmission();
Wire.requestFrom(0x68, 7);
s= (bcdToDec(Wire.read() & 0x7f));
mn= (bcdToDec(Wire.read()));
hr= (bcdToDec(Wire.read() & 0x3f));
DayOfTheWeek=(bcdToDec(Wire.read()));
d=(bcdToDec(Wire.read()));
Mon= (bcdToDec(Wire.read()));
yr=(bcdToDec(Wire.read()));
lcd.setCursor(0,1);
lcd.print("Date: ");
lcd.print(d);
lcd.print(":");
lcd.print(Mon);
lcd.print(":");
lcd.print(yr);
delay(1000);
};
//important bcd to dec functions
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
};
void testBlink() {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);
};
This was an amazing experience. hope i didn't annoy anyone ![]()