B4R Question SOLVED Library incompatibility for RTC and SD?

tigrot

Well-Known Member
Licensed User
Longtime User
Hi everybody,
I have a UNO and a datalogger shield(RTC and SD). I have this test program:
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Dim timer1 As Timer
    Dim y=2017,m=11,d=25,hh=13,mm=23,ss=00 As Int ' date and time fields with init time.
    Dim rtcres As Boolean ' result of native code calls (true=OK)
    Dim sd As SD
    Public buffer(200) As Byte
    Public raf As RandomAccessFile
    
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    sd.Initialize(10)
    If sd.Exists("/")=False Then
        Log("Nessuna scheda")
    End If
    For Each f As File In sd.ListFiles("/")
        Log(f.Name, TAB, Round(f.Size / 1024), "kb", TAB, f.IsDirectory)
        sd.OpenRead(f.name)
        Log("Lunghezza:")
        Log( sd.CurrentFile.Size)
        sd.Stream.ReadBytes(buffer, 0, buffer.Length)
        raf.CurrentPosition = 0
        'read the data from the buffer
        Log(buffer)
        sd.close
    Next
    Log("Fine")
End Sub
#if C
#include <DS1307RTC.h>
#include <Time.h>
//#include <Wire.h>
#include <TimeLib.h> 

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}   

void setRTC(B4R::Object* bho)
{
  tmElements_t tm;
  int Hour, Min, Sec;
  int Month;
  int Day, Year;
  //Serial.print("Start:");
 
 // if (Month>12 || Month==0) return false;
  tm.Hour = b4r_main::_hh;
  tm.Minute = b4r_main::_mm;
  tm.Second = b4r_main::_ss;
  tm.Day = b4r_main::_d;
  tm.Month = b4r_main::_m;
/*
  Serial.print("Written Time = ");
  print2digits(tm.Hour);
  Serial.write(':');
  print2digits(tm.Minute);
  Serial.write(':');
  print2digits(tm.Second);
  Serial.print(", Date (D/M/Y) = ");
  Serial.print(tm.Day);
  Serial.write('/');
  Serial.print(tm.Month);
  Serial.write('/');
  Serial.print(b4r_main::_y);
  Serial.println();
*/ 
  tm.Year = CalendarYrToTm(b4r_main::_y);

  bool res=RTC.write(tm);
  b4r_main::_rtcres=res;
  return res;
}


void read(B4R::Object* unused) {
  tmElements_t tm;
  if (RTC.read(tm)) {
    /*
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
   */
    b4r_main::_hh=tm.Hour;
    b4r_main::_mm=tm.Minute;
    b4r_main::_ss=tm.Second;
    b4r_main::_d=tm.Day;
    b4r_main::_m=tm.Month;
    b4r_main::_y=tmYearToCalendar(tm.Year);
    b4r_main::_rtcres=true;
    return true;
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Running SetTime");
      
      Serial.println();
      b4r_main::_rtcres=false;
      return false;
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
      b4r_main::_rtcres=false;
      return false;   
    }
    delay(9000);
  }
  delay(1000);
}
#end if

If I take off the unused C code the sd test works well, if i put the C code the SD is not detected.
The code for RTC works apart, but in this case it's not used.
Am I doing something wrong?
Thank you for help!
Best regards
Mauro Zanin
 

rtek1000

Active Member
Licensed User
Longtime User
Hello, I saw the reference code of the RTC DS1307,

Even if you have bought many of these, try to use the DS3231 or DS3232, it initially seems to be advantage the lowest price of the DS1307, but after in time of use of the DS1307, comes the repentance due to the variation of time, depending on the variation of temperature. The DS3231 / DS3232 has automatic temperature compensation, so it's much, much, much better.
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
The DS3231 / DS3232 has automatic temperature compensation, so it's much, much, much better.
Thank you Rtek1000, it's not a problem for me if the time is a few seconds +/-. I use and Android device to download data from the Arduino, so I can fix the RTC time any time I do download, automatically. This happens about every week, so...
The issue is that it's not simple to get better RTC in a Logger Shield, so at present I have to use it, maybe in the near future...

Regards
Mauro Zanin
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
I tried to test the library in Arduino environment. The result of RTC + SD libraries is really a few bytes from upper limit on UNO. So in B4R I'm sure it will give memory overflow for the little UNO, because addition of B4R support libraries.
 
Upvote 0
Top