DS 1338 setup with compiler time and date

Hi, I hope someone can help me with my problem and can describe how I can solve it but I have to say I am not good in programming.
My goal is to programm my RTC with the compiled time and date. So that I always have the current time and date saved in my rtc after setting it up the first time.
I already got so far that I have the date and time after compiling but I dont know how I can save the string in multiple pieces (variables) that I then can write into the command.

I am using this library but I think there is a problem in the month and year (at least in month because the year is in octal numbers):

#include <Wire.h>
#include <DS1338.h>

const char compile_date[] = __DATE__;
const char compile_time[] = __TIME__;


// A structure to hold the time read from the device
struct rtctime_t time;

// A character array to hold the string representation of the time
char time_str[20];



void setup() {
  Wire.begin();
  Serial.begin(74880);
  while(!Serial);
  //Serial.println(compile_date);
  //Serial.println(compile_time);

  ds1338_clean_osf();
  ds1338_disable_sqw();

  make_time(&time, 031, 05, 21, 11, 31, 40);

  // Set the clock
  if (ds1338_write_time(&time))
  {
    Serial.println("Unable to set the time--an I2C error has occured.");
  }
  else
  {
    Serial.println("Clock set successfully!");
  }
}

void loop() {
  // Attempt to read the time.
  if (ds1338_read_time(&time))
  {
    Serial.println("Unable to read time--an I2C error has occured.");
  }
  else
  {
    // The time has been read successfully--print it.
    format_time_str(&time, time_str);
    Serial.println(time_str);
  }
  delay(1000);
}

So don't use octal. Generally, avoid using a leading zero in numbers.

I used the example of the library and he already uses octal but didnt tell that it is octal.

I've just taken a fast flight over the data sheet and saw what I expected. There is nothing octal about the part and there should be nothing octal about using it.

Separate your problem. Saving values or strings or dates or times in variables or EEPROM has nothing to do with what is in them or what that content represents.

Figure out the RTC.

Select some way to store any information to survive power cycling, or whatever your end goal is.

Two completely separate issues. Neither difficult. Forget octal, you are confused or have been mislead.

a7

Regardless, there is no need to use octal and it just leads to confusion.

There are some examples of how to parse __DATE__ and __TIME__ for Arduino at https://stackoverflow.com/questions/1765014/convert-string-from-date-into-a-time-t

Then why do I get a completly wrong year when I select the decimal and when I use it I get the error that I cant use decimal in octal.

Post the error you are getting.

a7

#include <Wire.h>
#include <DS1338.h>

// A structure to hold the time read from the device
struct rtctime_t time;

// A character array to hold the string representation of the time
char time_str[20];

void setup()
{

  // Initialize the I2C bus
  Wire.begin();

  // Initialize the serial interface
  Serial.begin(74880);
  while (!Serial)
    ;

  // Once serial has been inialized, print a welcome message
  Serial.println("Maxim DS1338 RTC Example");

  ds1338_clean_osf();
  ds1338_disable_sqw();
  
  
  // Set the date of the time structure to June 27, 2014 and
  // the time to 18:45:00 (6:45:00 pm)
  make_time(&time, 017, 10, 27, 18, 28, 06);

  // Set the clock
  
  if (ds1338_write_time(&time))
  {
    Serial.println("Unable to set the time--an I2C error has occured.");
  }
  else
  {
    Serial.println("Clock set successfully!");
  }
  
}

void loop()
{

  // Attempt to read the time.
  if (ds1338_read_time(&time))
  {
    Serial.println("Unable to read time--an I2C error has occured.");
  }
  else
  {
    // The time has been read successfully--print it.
    format_time_str(&time, time_str);
    Serial.println(time_str);
  }

  delay(1000);
}

When I use his example then I get the this result:

grafik

When he really wanted to save this information in the RTC "June 27, 2014" then it didnt work.

When you use octal in a constant like you did…

… no one else knows or cares.

017 is 15, you could have written 0xf or, for that matter 9 + 6. Or 3 * 5.

It does not matter to the called function, it has no idea. Forget octal.

a7

So am I.

Those arguments are constants, it matters not how you choose to represent them. Decimal, octal, hex, the result of a computation, whatever.

a7

The comment doesn't match the code. 017 is 15 decimal.

To be honest, I would not put much faith in this library, it appears quite buggy. And DO NOT USE OCTAL!!

Okay now I did understood it.