I have a few RTC modules with a batteries. All of them have this same issue with my project.
They work fine when connected via USB. But if I disconnect the power then reconnect they reset to Jan 1 2000.
I've read several posts about similar problems. But, none seem to apply.
Here is my code:
#include <SFE_MicroOLED.h> // Include the SFE_MicroOLED library
#include <SPI.h>
#include <SD.h>
#include "Wire.h"
#include <RTClib.h>
RTC_DS1307 RTC;
#define BUTTON_PIN 0 // Button
#define LONGPRESS_LEN 25 // Min nr of loops for a long press
#define DELAY 20 // Delay per loop in ms
enum { EV_NONE=0, EV_SHORTPRESS, EV_LONGPRESS };
boolean button_was_pressed; // previous state
int button_pressed_counter; // press running duration
byte zero = 0x00; //workaround for issue #527
char* modes[]={"Walk", "Bike", "Subway", "Train", "Bus", "X-Bus", "Waiting", "Other"};
byte mode=0;
int modeRead;
// Chip Select for SD
#define chipSelect 10
//MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS);
MicroOLED oled(9, 8, 5); // SPI declaration
void setup()
{
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH); // pull-up
Serial.begin(9600);
button_was_pressed = false;
button_pressed_counter = 0;
SD.begin(chipSelect);
Wire.begin();
RTC.begin();
//RTC.adjust(DateTime(__DATE__, __TIME__));
oled.begin(); // Initialize the OLED
oled.clear(PAGE); // Clear the screen
oled.setFontType(1); // Set font to type 0
oled.setCursor(0, 0); // Set cursor to top-left
oled.println(F("co'mute"));
oled.display(); // Display what's in the buffer
}
int handle_button()
{
int event;
int button_now_pressed = !digitalRead(BUTTON_PIN); // pin low -> pressed
if (!button_now_pressed && button_was_pressed) {
if (button_pressed_counter < LONGPRESS_LEN)
event = EV_SHORTPRESS;
else
event = EV_LONGPRESS;
}
else
event = EV_NONE;
if (button_now_pressed)
++button_pressed_counter;
else
button_pressed_counter = 0;
button_was_pressed = button_now_pressed;
return event;
}
void printDate ()
{
oled.clear(PAGE); // Clear the screen
oled.setFontType(0); // Set font to type 0
oled.setCursor(0, 0); // Set cursor to top-left
DateTime now = RTC.now();
oled.print(now.year(), DEC);
oled.print('/');
oled.print(now.month(), DEC);
oled.print('/');
oled.print(now.day(), DEC);
oled.print(' ');
oled.print(now.hour(), DEC);
oled.print(':');
if (now.minute()<10) {
oled.print("0");
}
oled.print(now.minute(), DEC);
oled.print(':');
if (now.second()<10) {
oled.print("0");
}
oled.print(now.second(), DEC);
oled.println();
oled.setFontType(1); // Set font to type 0
oled.println(modes[mode]);
oled.display(); // Display what's in the buffer (splashscreen)
}
void writeFile()
{
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// If the file is available, write to it:
if (dataFile) {
DateTime now = RTC.now();
//print the date EG 3/1/11 23:59:59
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
printDate ();
oled.print("*SAVED*");
oled.display();
delay(3000);
}
}
void loop() // run over and over again
{
// handle button
boolean event = handle_button();
// do other things
switch (event) {
case EV_NONE:
oled.print(".");
break;
case EV_SHORTPRESS:
oled.print("S");
//modeRead = analogRead(0); // read the value from the sensor
writeFile();
break;
case EV_LONGPRESS:
oled.print("L");
break;
}
static int counter = 0;
if ((++counter & 0X016) == 0) {
printDate ();
}
modeRead = analogRead(A0); // read the value from the sensor
delay(DELAY);
int c=1020/(sizeof(modes)/sizeof(modes[0]));
for (int i = 0; i < c; i++) {
if (i*c < modeRead && (i+1)*c < modeRead) {
mode = i;
}
}
}
In this diagram the "pressure sensor" is really a OLED that operates via MOSI and MISO.


