Hi there!
I have a Tiny RTC (ds1307) module and i wrote a small tool on my computer that will send the date and time in case the battery fails. Now that tool already works, but i have a problem on the arduino side.
It just writes the wrong value to the RTC. What is wrong with my code? Please note the way the data is being transferred is with a 10 millisecond delay and then it expects the new data to arrive.
I hope somebody can help! ![]()
(This code also writes it to a OLED display, but it is not mandatory)
#include <RTClib.h>
#include <Wire.h>
RTC_DS1307 RTC;
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
int RTC_Configured;
int m;
char carray[6];
byte Month = 0;
byte Day = 0;
byte Year = 0;
byte Hour = 0;
byte Minute = 0;
byte Second = 0;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
byte decToBcd(byte val) {
return ((val/10*16) + (val%10));
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Wire.begin();
RTC.begin();
delay(100);
Serial.println("RTC DEMO OK");
//Dit wordt gebruikt voor de configurator om er zeker van te zijn dat het de Tiny RTC Demo is
delay(100);
if (! RTC.isrunning()) {
Serial.println("RTC DEMO NOT SET");
// following line sets the RTC to the date & time this sketch was compiled
RTC_Configured = 1;
delay(100);
}
}
void loop() {
// put your main code here, to run repeatedly:
if (stringComplete) {
if (isDigit(inputString.charAt(0))){
inputString.toCharArray(carray, sizeof(carray));
m = atoi(carray);
Month = m;
for( int i = 0; i < sizeof(carray); ++i )
carray[i] = (char)0;
delay(10);
inputString.toCharArray(carray, sizeof(carray));
m = atoi(carray);
Day = m;
for( int i = 0; i < sizeof(carray); ++i )
carray[i] = (char)0;
delay(10);
inputString.toCharArray(carray, sizeof(carray));
m = atoi(carray);
Year = m;
for( int i = 0; i < sizeof(carray); ++i )
carray[i] = (char)0;
delay(10);
inputString.toCharArray(carray, sizeof(carray));
m = atoi(carray);
Hour = m;
for( int i = 0; i < sizeof(carray); ++i )
carray[i] = (char)0;
delay(10);
inputString.toCharArray(carray, sizeof(carray));
m = atoi(carray);
Minute = m;
for( int i = 0; i < sizeof(carray); ++i )
carray[i] = (char)0;
delay(10);
inputString.toCharArray(carray, sizeof(carray));
m = atoi(carray);
Second = m;
for( int i = 0; i < sizeof(carray); ++i )
carray[i] = (char)0;
RTC.adjust(DateTime(Year, Month, Day, Hour, Minute, Second));
}
inputString = "";
stringComplete = false;
RTC_Configured = 0;
}
if (RTC_Configured != 1)
{
DateTime now = RTC.now();
Serial.print("DATE");
Serial.print(now.year(), DEC);
Serial.print('-');
Serial.print(now.month(), DEC);
Serial.print('-');
Serial.print(now.day(), DEC);
Serial.println();
delay(100);
Serial.print("TIME");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Tiny RTC Demo");
display.setCursor(0,8);
display.print("Date: ");
display.print(now.year(), DEC);
display.print("-");
display.print(now.month(), DEC);
display.print("-");
display.println(now.day(), DEC);
display.print("Time: ");
display.print(now.hour(), DEC);
display.print(":");
display.print(now.minute(), DEC);
display.print(":");
display.print(now.second(), DEC);
display.setCursor(0,56);
display.print("Gemaakt door: Sander");
display.display(); //Je MOET display.display doen of anders zal hij niks laten zien
delay(250); //Vaker dan 1 keer bijwerken om er zeker van te zijn dat de tijd op het beeldscherm klopt
}
else
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Use the Tiny RTC Demo");
display.println("Configurator to set");
display.println("the clock!");
display.display();
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
delay(3);
inputString += inChar;
stringComplete = true;
}
}