2

I have a problem with an RTC: it doesn't work correctly. When I try to read date or time, I only see the characters "àààààààà" or or the program is blocked.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>

DS1307 rtc;
DateTime rtcTime;

void setup() {
  //zainicjowanie generatora liczb losowych
  randomSeed(A0); 
  Wire.begin();
  rtc.begin();
}

void loop() {
  rtcTime = rtc.now();
  int mm = rtcTime.minute();
  Serial.println(mm);
  
  delay(500);
}

This function blocks my program:

String timeRTC(){
  DateTime now = rtc.now();
  String Hour = "0" + String(now.hour()); 
  String Minutes = "0" + String(now.minute()); 
  String Seconds = "0" + String(now.second());

  if (Hour.length() == 3) {
    Hour = Hour.substring(1);
  }
  if (Minutes.length() == 3) {
    Minutes = Minutes.substring(1);
  }
  if (Seconds.length() == 3) {
    Seconds = Seconds.substring(1);
  }

  return Hour+":"+Minutes+":"+Seconds;
}

I can't find what is causing this problem. I use an Arduino Uno in the WOKWI emulator.

String dateRTC(){
  DateTime now = rtc.now();
  String Day = "0" + String(now.day()); 
  String Month = "0" + String(now.month()); 
  String Year = String(now.year());

  if (Day.length() == 3) {
    Day = Day.substring(1);
  }
  if (Month.length() == 3) {
    Month = Month.substring(1);
  }

  return Day+"-"+Month+"-"+Year;
}

String timeRTC(){
  DateTime now = rtc.now();
  String Hour = "0" + String(now.hour()); 
  String Minutes = "0" + String(now.minute()); 
  String Seconds = "0" + String(now.second());

  if (Hour.length() == 3) {
    Hour = Hour.substring(1);
  }
  if (Minutes.length() == 3) {
    Minutes = Minutes.substring(1);
  }
  if (Seconds.length() == 3) {
    Seconds = Seconds.substring(1);
  }

  return Hour+":"+Minutes+":"+Seconds;
}

enter image description here

8
  • Could you provide a link to your WOKWI project? Commented Jan 23, 2024 at 10:22
  • I use free version of WOKWI. This is link for my project: file.io/y1XjwyqHjhNk Commented Jan 23, 2024 at 10:34
  • The link says “The transfer you requested has been deleted.” Commented Jan 23, 2024 at 10:41
  • I shared the file again: easyupload.io/b0e4w8 Commented Jan 23, 2024 at 10:53
  • What do you mean by "blocked my program"? Commented Jan 23, 2024 at 10:57

1 Answer 1

2

You don't have a problem with the RTC: if you call Serial.begin() in setup(), and remove everything in your loop() after Serial.println(mm);, the program works as expected. Your problem is elsewhere. Most likely you are corrupting memory, which leads to unpredictable behavior.

Side note: if you switch to Adafruit's RTClib (called simply “RTClib” in the library manager), you can simplify timeRTC() to this:

String timeRTC() {
    return rtc.now().timestamp(DateTime::TIMESTAMP_TIME);
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.