0

I am sorry to ask this question as I think it has more to do with my mediocre C++ programming skill than with the arduino. I do have a project where I want to use an RTC DS2131. I started out by using all the separate examples of the modules I need and put them together. But the RTC give me a headache :-(

The example ran fine where it just output the date to the serial port. Somthing along this:

Serial.print(RTC.getDay());
Serial.print("-");
Serial.print(RTC.getMonth());

Now I want these values in different variables because I want to save them on an SD card as well.

Thus I came up with this simple test program:

#include <Wire.h>
#include <RTC.h>

static DS3231 RTC;

void setup()
{ 
  Serial.begin(9600);
  RTC.begin();
}

void loop()
{  
int yearNow; 
int monthNow, dateNow, hourNow, minuteNow, secondNow, weekNow;

if (RTC.isRunning())
  {
    yearNow=RTC.getYear();
    monthNow =RTC.getMonth();
    dateNow=RTC.getDay();
    weekNow=RTC.getWeek();
    hourNow=RTC.getHours();
    minuteNow=RTC.getMinutes();
    secondNow=RTC.getSeconds();
   
    Serial.print("Weekday: ");
    Serial.print(weekNow);
    Serial.print('  Time:');
    // Finally the hour, minute, and second
    Serial.print(hourNow);
    Serial.print('-');
    Serial.print(minuteNow);
    Serial.print('-');
    Serial.print(secondNow);
    Serial.print(" ##  ");    
    Serial.print("Date: ");
    Serial.print(yearNow);
    Serial.print('/');
    Serial.print(monthNow);
    Serial.print('/');
    Serial.println(dateNow);
    
  }
}

Now I would expect this output: Weekday: 4 Time:16-10-36 ## Date: 2020/7/28

But this is what I get: Weekday: 42591416-10-36 ## Date: 2020/7/28

As a sidenote the variables in this sketch are declared as "int". After having looked into the header files of the library I tried uint8_t resp. uint16_t for the year. But nothing changed.

I am sorry but I have no clue as what beginner mistake I am achieving this time?? :-(

Thanks for your help.

1

You used single quotes instead of double quotes. Single quotes are for single characters. Double quotes are for strings (or character arrays).

Serial.print('  Time:');

should be

Serial.print("  Time:");
| improve this answer | |
  • Thanks. I thought " and ' are interchangable. – Andreas K. Jul 28 at 15:22
  • They are in javascript or PHP, not in C/C++. – Gerben Jul 28 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.