I see that when using RTClib to set the time in a ds1307, the time that's used to set it is the time retrieved on compile, which causes the rtc to lag real time by about 30 seconds.
So, in order to set the RTC more accurately, I thought to put the time adjust statement,
"rtc.adjust(DateTime(__DATE__, __TIME__));"
into a one-shot button press within the main loop.
But it appears that the time/date even there is always the time and date retrieved at compile, rather than the time/date at button press, that is, it just loads the time/date that was retrieved on compile every time the button is pressed instead of getting the current time and loading that time into the RTC.
How can I get the current time from the computer on button press to set that time into the 1307 ???
code is:
// Reads Date and time from a DS1307 RTC connected via I2C and Wire lib
// RTC plugged directly onto Uno, with A2 (D17) set as gnd, A3 (D16) set as +5 (high)
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
const int PwrToRTC = 17; // set this as OUTPUT, and HIGH to power RTC directly from plugged into Uno board
const int GndToRTC = 16; // set this as OUTPUT, and LOW for GROUND for RTC from plugged into Uno board
const int buttonPin = 6; // pin number of a normally CLOSED pushbutton pin
// switch debounce vars:
int val; // variable for reading the switch pin status
int val2; // variable for reading the switch pin status after a delay
int buttonState; // the current reading from the input pin
// one-shot var: allow to do action only ONCE when button pressed, don't do again until switch released and
// then pressed again
int oneShotHasFired; // one shot has fired, don't fire again until cleared
void setup () {
Serial.begin(57600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
pinMode(PwrToRTC, OUTPUT); // set analog pin A3 (digital 17 on Uno) as OUTPUT and HIGH for POWER rtc
digitalWrite(PwrToRTC, HIGH);
pinMode(GndToRTC, OUTPUT); // set analog pin A2 (digital 16 on Uno) as OUTPUT and LOW for GROUND rtc
digitalWrite(GndToRTC, LOW);
pinMode(buttonPin, INPUT); // set pin 6 as input for pushbutton switch
digitalWrite(buttonPin, HIGH); // turn on pullup resistors for pin 4 in order to use NC switch to ground
buttonState = digitalRead(buttonPin); // read the initial state
oneShotHasFired = 0; // hasn't fired yet
rtc.begin();
// Serial.println("setting the time/date");
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(__DATE__, __TIME__));
//Serial.println("finished setting the time/date?");
}
void loop () {
val = digitalRead(buttonPin); // read input value and store it in val
delay(10); // time delay for debounce switch
val2 = digitalRead(buttonPin); // read it again after delay, store in val2
if (val == val2) { // ok, the two readings are the same
if (val != buttonState) { // the button state has changed!
if (val == HIGH && oneShotHasFired == 0) { // check if the button is pressed
Serial.println("Button just pressed");
oneShotHasFired = 1;
rtc.adjust(DateTime(__DATE__, __TIME__));
DateTime now = rtc.now();
Serial.println();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
} else { // the button is RELEASED // -not- pressed...
// Serial.println(" Button just released");
oneShotHasFired = 0;
}
}
}