Month/Day Names in Date/Time

My date and time display is working fine.
It displays on the LCD 16, 2 as:
“Time: 14:24:03”
“Date: 06/23/2019”

I would like to include abbreviated month and day name in the date line, to read:
“Time: 14:24:03”
“Sun, Jun 23/2019”

I assume I need to include an array:
char {“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”};
char {“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”};

I am not sure how to incorporate this into sketch.

Sketch:
#include <Arduino.h>
#include <RTClib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS3231 rtc;

char buf1[20];
char buf2[20];

void setup() {
Serial.begin (9600);
Wire.begin();

lcd.init();
lcd.backlight();

rtc.begin();
rtc.adjust(DateTime(F(DATE),F(TIME)));

}

void loop() {
DateTime now = rtc.now();

sprintf(buf1, "%02d:%02d:%02d ", now.hour(), now.minute(), now.second());
sprintf(buf2, “%02d/%02d/%02d”, now.month(), now.day(), now.year());

Serial.println(buf1);
Serial.println(buf2);

Serial.println();
lcd.setCursor(0, 0);
lcd.print(F("Time: "));
lcd.println(buf1);

lcd.setCursor(0, 1);
lcd.print(F("Date: "));
lcd.println(buf2);

delay(1000);

}

One of many fine tutorials on character arrays in C/C++.

Don't forget to place them all in progmem. Otherwise you run out of RAM really fast.

  // use code tags!!

char arrayName[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char otherArrayName = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"};

// char buf1[20];   <<=== you really don't need these wastes of RAM 
// char buf2[20];

//       and here, sprintf() adds a lot to your sketch size.
//  sprintf(buf1, "%02d:%02d:%02d  ", now.hour(), now.minute(), now.second());
//  sprintf(buf2, "%02d/%02d/%02d", now.month(), now.day(), now.year());
  
//  Serial.println(buf1);
//  Serial.println(buf2);
 

  // believe it or not, this generates less code and uses the serial output buffer
  // --before serial can get the 1st char completely sent, these lines below will 
  // have been queued into the output buffer.
 
  Serial.print( F( "Time: ));
  if ( now.hour < 10 )
  {
    Serial.print( "0" );
  }
  Serial.print( now.hour );
  Serial.print( ":" ); 

  if ( now.minute < 10 )
  {
    Serial.print( "0" );
  }
  Serial.print( now.minute );
  Serial.print( ":" ); 

  if ( now.second < 10 )
  {
    Serial.print( "0" );
  }
  Serial.println( now.second );

  Serial.print( arrayName[ x ] );  // where x is day of week
  Serial.print( ", " );
  Serial.print( otherArrayName[ now.month() ] );  // if you want leading 0's, do as with time 
  Serial.print( " " );
  Serial.print( now.day() );
  Serial.print( "/" );
  Serial.println( now.year() );

There is no need to specially prepare a buffered line just to print. The chars don’t all get in a car and go zoom away. They get put in the 64 char serial output buffer and from there 1 BIT at a time is transmitted even as other chars get added to the buffer.

At 115200 baud it takes 1388 cpu cycles to send just 1 char. Arduino can more than fill the buffer with text in that time. The biggest use of cycles in your lines are turning binary values into decimal text and those RTC function calls.

Thanks GoForSmoke, I will be trimming the fat off my sketch later today. For the moment I was able to succeed in placing abbreviated month name, however when I change PC date and time to June 30, 23:59 and upload, Jun still appears when the calendar rolls over to July.

So now the LCD display will correctly read:
06:45:36
Wed. Jun 26,2019

The problem now is month name will not roll over when I upload an end of the month date.

#include “RTClib.h”
#include <Wire.h>
#include “LiquidCrystal_I2C.h”

LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS3231 rtc;

char buf1[20];
char buf2[20], daysOfTheWeek[7][12] = {“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”};
char buf3[20], *monthNames = {"", “Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”};
int month;

void logname(char const *date, char *buff) {
int month, day, year;
sscanf(date, “%s %d”, buff, &month);
static const char *monthNames = {"", “Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”};
month = (strstr(*monthNames, buff) - *monthNames) / 3;

}

void setup() {
Serial.begin (9600);
Wire.begin();
Serial.print("");
char *filename;
logname(DATE, *monthNames);
Serial.print(*monthNames);
rtc.begin();
rtc.adjust(DateTime(F(DATE), F(TIME)));
lcd.init();
lcd.backlight();

}

void loop() {
DateTime now = rtc.now();
sprintf(buf1, " %02d:%02d:%02d ", now.hour(), now.minute(), now.second());
sprintf(buf2, " %02d,%02d ", now.day(), now.year());
sprintf(buf3, “%02d”, *monthNames[month]);
int month = 3;
Serial.print(buf1);
Serial.print(buf2);
Serial.print(buf3);
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
char *filename;
logname(DATE, *monthNames);
Serial.println(*monthNames);

//TIME
lcd.setCursor(0, 0);
lcd.print(F(""));
lcd.println(buf1);

//DATE
lcd.setCursor(0, 1);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(F(". "));
lcd.println(buf2);

//MONTH
lcd.setCursor(5, 1);
logname(DATE, *monthNames);
lcd.print(*monthNames);

delay(1000);

}

I’ve been tinkering with this project since June of 1019 and for the moment, putting closure on it. I assumed creating a simple time and date display with long names would be easy, however after realizing there are different versions of DS3231 libraries, it became a little confusing. I recently came across Arduino-DS3231-master library and have implemented this.

Due to space constraints on the 16x2 LCD display, long day name and long month name will not fit on one row (i.e. Wednesday, September 28). Day and month are on the left side of display, time and temp on the right side.

Temp reading is not very accurate as it is reading board temp and not room temp, but that will be another project incorporating a DS18B20 digital temperature sensor.

It has been working flawlessly with an external 5v power source.

Code:

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

DS3231 rtc;
int tempC;
int tempF;

DS3231 clock;
RTCDateTime dt;
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
  Serial.begin(9600);
  rtc.begin();
  clock.begin();
  lcd.init();
  lcd.backlight();
  //Set the RTC time_date from pc. Upload sketch THEN UPLOAD AGAIN WITH THIS LINE COMMENTED OUT
  clock.setDateTime(__DATE__, __TIME__);

}

void loop()
{
  //read time_date
  dt = clock.getDateTime();
  //read temperature
  tempC = rtc.readTemperature();
  tempF = (tempC * 1.8) + 24.0;  //adjusted temp from Celsius
  //print to serial monitor
  Serial.print(clock.dateFormat("l  H:i", dt));  //day of week_time
  Serial.println();
  Serial.print(clock.dateFormat("F d", dt));   //month_date
  Serial.print(" ");
  Serial.print(tempF);  //temp
  Serial.println("F");  //fahrenheit as F
  Serial.println();
  //print to lcd display
  lcd.setCursor(0, 0);
  lcd.print(clock.dateFormat("l ", dt));  //day of week
  lcd.setCursor(11, 0);
  lcd.print(clock.dateFormat("H:i", dt));  //time
  lcd.setCursor(0, 1);
  lcd.print(clock.dateFormat("F d", dt));   //month_date
  lcd.setCursor(13, 1);
  lcd.print(tempF);   //temp
  lcd.print("F");     //fahrenheit as F

}

See attached for LCD screenshot.

DS18B20 has gotten trashed a lot here. Every time, the DS18B30 is recommended for accuracy.