Notifications
Clear all

[Solved] Convert DS3231 RTC getDOWStr() String to other language?

16 Posts
3 Users
0 Likes
14.3 K Views
(@tibortosoki)
Member
Joined: 5 years ago
Posts: 10
Topic starter  

So I have been playing with the DS3231 RTC module and I have succesfully printed out the time, date, temp and day to my 1602LCD. However I want to print the day in Hungarian (short form). I have tried with  switch-case but it does no print out anything in the day "section" of my screen. This is what I got yet:

I have a "getDOWStrHun()" this is what I want to convert the English day to Hungarian with.

#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

DS3231 rtc(SDA, SCL);

const int en = 2, rw =1, rs=0, d4 = 4, d5 = 5, d6 = 6, d7 = 7, bl = 3;
const int display_i2c_addr = 0x27;
LiquidCrystal_I2C lcd (display_i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE);

void setup() {
rtc.begin();
lcd.begin(16,2);
}

void loop() {

//1st row - date and day of the week
lcd.setCursor(0,0);
lcd.print(rtc.getDateStr());
lcd.print(" ");
lcd.print(getDOWStrHun());

//2nd row - time, temp
lcd.setCursor(0,1);
lcd.print(rtc.getTimeStr());
lcd.print(" ");
lcd.print(round(rtc.getTemp()));
lcd.print(char(223));                               //degrees symbol
lcd.print("C");

delay(1000);
}

String getDOWStrHun(){
char dayEng = rtc.getDOWStr();
String dayHun;

switch (dayEng){
case 'MONDAY':
dayHun="HET";
break;
case 'TUESDAYDAY':
dayHun="KEDD";
break;
case 'WEDNESDAY':
dayHun="SZER";
break;                                      // ... and so on
}

return(dayHun);
}

This topic was modified 5 years ago by TiborTosoki

   
Quote
jscottbee
(@jscottbee)
Member
Joined: 5 years ago
Posts: 107
 

Do it with if compares. Switch/case in C/C++ does not do well with fill strings.


   
ReplyQuote
jscottbee
(@jscottbee)
Member
Joined: 5 years ago
Posts: 107
 

Or get the weekday number from the library and switch/case that instead of the string. 


   
ReplyQuote
(@tibortosoki)
Member
Joined: 5 years ago
Posts: 10
Topic starter  

How can I get that?


   
ReplyQuote
jscottbee
(@jscottbee)
Member
Joined: 5 years ago
Posts: 107
 

What DS3231 library are you using? There may be a getDow method or you can take the datetime and convert it to ts struct from time.h.


   
ReplyQuote
(@tibortosoki)
Member
Joined: 5 years ago
Posts: 10
Topic starter  

The on from Henning Karlsen ( http://www.rinkydinkelectronics.com/library.php?id=73), but I cant find such method.


   
ReplyQuote
jscottbee
(@jscottbee)
Member
Joined: 5 years ago
Posts: 107
 
Posted by: TiborTosoki

The on from Henning Karlsen ( http://www.rinkydinkelectronics.com/library.php?id=73), but I cant find such method.

The getTime method will return a ts line structure that has the dow in it. 

class Time
{
public:
uint8_t hour;
uint8_t min;
uint8_t sec;
uint8_t date;
uint8_t mon;
uint16_t year;
uint8_t dow;

Time();
};

With this number you can switch/case and return a string of the day.

This post was modified 5 years ago by jscottbee

   
ReplyQuote
(@tibortosoki)
Member
Joined: 5 years ago
Posts: 10
Topic starter  

Thanks I'll try it.


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

I don't see the point of rtc.begin(), as the clock is always running as soon as the battery is inserted.

You need to initiate an object first.

Try 

DS3231 Clock;

before void setup()

byte dayOfTheWeek = Clock.getDoW();

But you'll have to check the methods because I am not 100% sure.

There is a setDoW() method so I assume there will be a getDoW() to compliment it.


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

Just another point, the RTC clock does not return string for the day of the week just an unsigned integer (byte) of 0-6. You can choose which day the week starts with when setting the clock for the first time.

You can even start your week on Thursday if you want.

This is a typical string of data to set the clock.

1906263200000x

YYMMDDwHHMMSSx

If you set the clock today at 20:00 (as above) when w=3 the first day of the week is Sunday.

switch (dayOfTheWeek){
case 1:
dayHun="HET";
break;
etc. etc.
}

   
ReplyQuote
jscottbee
(@jscottbee)
Member
Joined: 5 years ago
Posts: 107
 
Posted by: Pugwash

I don't see the point of rtc.begin(), as the clock is always running as soon as the battery is inserted.

You need to initiate an object first.

Try 

DS3231 Clock;

before void setup()

byte dayOfTheWeek = Clock.getDoW();

But you'll have to check the methods because I am not 100% sure.

There is a setDoW() method so I assume there will be a getDoW() to compliment it.

The library he uses does not have the getDow method but does return the needed data in the Time class on a getTime call. 

There are a lot of different DS3231 libraries :/

Scott


   
ReplyQuote
(@tibortosoki)
Member
Joined: 5 years ago
Posts: 10
Topic starter  

Well, I have found a way without switch-case, everything works fine. Btw this library a little bit sucks, I mean it is really simple to use, but there are many methods missing compared to other libraries (getDOW, getHour, getMinute...). 

#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>


DS3231 rtc(SDA, SCL);

const int en = 2, rw =1, rs=0, d4 = 4, d5 = 5, d6 = 6, d7 = 7, bl = 3;
const int display_i2c_addr = 0x27;
LiquidCrystal_I2C lcd (display_i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE);


void setup() {
rtc.begin();
lcd.begin(16,2);
}

void loop() {

//1st row - date, time
lcd.setCursor(0,0);
lcd.print(rtc.getDateStr(FORMAT_LONG, FORMAT_BIGENDIAN)); //BIGENDIAN -> YYYY.MM.DD
lcd.print(" ");
lcd.print(rtc.getTimeStr(FORMAT_SHORT)); //SHORT -> HH:MM

//2nd row - day in hun, temp
lcd.setCursor(0,1);
lcd.print(getDOWStrHun());
lcd.setCursor(12,1);
lcd.print(round(rtc.getTemp()));
lcd.print(char(223)); //degrees symbol
lcd.print("C");

delay(1000);
}



String getDOWStrHun(){
String dayEng = rtc.getDOWStr();
String dayHun;

if(dayEng == "Monday"){dayHun = "Hetfo";} else
if(dayEng == "Tuesday"){dayHun = "Kedd";} else
if(dayEng == "Wednesday"){dayHun = "Szerda";} else
if(dayEng == "Thursday"){dayHun = "Csutortok";} else
if(dayEng == "Friday"){dayHun = "Pentek";} else
if(dayEng == "Saturday"){dayHun = "Szombat";} else
{dayHun = "Vasarnap";}

return(dayHun);
}

 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

Scott,

so I guess you are telling me/us/whoever that there are a lot of different libraries called DS3231.h, all with different behaviors i.e. methods.

My guess is also that you could do away with the libraries if you address the I2C directly, as when I looked at the I2C bus and an RTC module with an oscilloscope, the RTC module was returning byte arrays. And as the DS3231 chip always sends fundamentally the same sort of byte array information at each request, I deduce that all that the libraries are doing is repackaging the information it is getting into edible slices for human consumption.

Basically, keeping us all stupid!


   
ReplyQuote
jscottbee
(@jscottbee)
Member
Joined: 5 years ago
Posts: 107
 

Yeah, there are a lot of libraries for this module. There are duplicates of many Arduino libraries and some or good and some are bad. 

Sometimes it may be better to "roll your own" 🙂

Scott


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

I guess you are right!

The libraries make us lazy, but once you know how to address the registers, it is not as daunting as looks to get or set the chips at will.

Below is a small routine to change the frequency of the oscillator pin on the fly.

#include <Wire.h>

int ds3231_address = 0x68; //ds3231 address

void setup () {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(ds3231_address);
Wire.write(0x0E); //Control Register for SQW (0Eh)
Wire.write(0x10); // 0x00 = 1Hz; 0x08 = 1024Hz; 0x10 = 4096Hz; 0x18 = 8192Hz:
Wire.endTransmission();
Serial.println("Input 0 for 1Hz, 1 for 1024Hz, 4 for 4096Hz or 8 for 8192Hz");
Serial.println("Default Frequency is 4096 Hz");
}

void loop () {

while(Serial.available() > 0){

int incomingByte = Serial.read();

switch(incomingByte){
case 0x30:
Serial.println("");
Serial.println("Frequency changed to 1 Hz");
Serial.println("");
setFrequency(0);
break;
case 0x31:
Serial.println("");
Serial.println("Frequency changed to 1024 Hz");
Serial.println("");
setFrequency(8);
break;
case 0x34:
Serial.println("");
Serial.println("Frequency changed to 4096 Hz");
Serial.println("");
setFrequency(16);
break;
case 0x38:
Serial.println("");
Serial.println("Frequency changed to 8192 Hz");
Serial.println("");
setFrequency(24);
break;

default:
Serial.println("");
Serial.println("Frequency unchanged...");
Serial.println("Input 0 for 1Hz, 1 for 1024Hz");
Serial.println("4 for 4096Hz or 8 for 8192Hz");
Serial.println("");
break;
}
}
}

void setFrequency(byte var){

Wire.beginTransmission(ds3231_address);
Wire.write(0x0E); //Control Register for SQW (0Eh)
Wire.write(var); // 0x00 = 1Hz; 0x08 = 1024Hz; 0x10 = 4096Hz; 0x18 = 8192Hz:
Wire.endTransmission();

}

I didn't do this for any particular purpose, just find out whether I had fully understood how the I2C bus and the registers work.


   
ReplyQuote
Page 1 / 2