Programming oled display of the week day

I have an interesting problem with my program which, is simply a modification of an example code for the DS3231 and a 128x64 OLED display. The code all runs smoothly except for the printing of the oled.print(daysOfTheWeek[now.dayOfTheWeek()]). It pulls the proper day with the first three letters but then appends the rest of the 3 letter array days for the rest of the week on to it.

EXAMPLE: THUFRISAT

I tried to use the (F(daysOfTheWeek[now.dayOfTheWeek())]); which I did not expect it to work but tried anyway.

Any Help would be greatly appreciated.

Hardware: Windows 10, Arduino IDE 1.8.15,
Sunfounder MEGA 2560
BN: Arduino Mega or Mega 2560
VID: 2341
PID: 0042
SN: 955303438343514062B1

#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#include "RTClib.h"

RTC_DS3231 rtc;
//char daysOfTheWeek[7][4] = {"SUN", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
char daysOfTheWeek[7][3] = {"SUN", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
// OLED display size in pixels
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

#define OLED_RESET -1
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
DateTime now;

void setup() {
  Serial.begin(9600);
  delay(3000); // wait for console opening
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  initializeRTC();
  initializeOLED();

}

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

  displayDay();  // ****Section where the issue is occuring
  displayDate();
  displayTime();
  displayTemperature();
}

void initializeRTC() {
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}

void initializeOLED() {
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.clearDisplay();
  oled.display();
  oled.setTextColor(WHITE, BLACK);
  oled.setTextSize(1);

  oled.setCursor(117, 55);
  // Enable the Code Page 437
  // https://en.wikipedia.org/wiki/Code_page_437
  oled.cp437(true);
  // Then print the chosen characters
  // which in this case is the degrees symbol
  // https://www.ascii-codes.com/
  oled.write(248);

  oled.setCursor(0, 55);
  oled.print("TEMPERATURE =");
  oled.setCursor(122, 55);
  oled.print("C");
}

void displayDay() {  // Function in which I believe the issue occurs
  oled.setTextSize(1);
  oled.setCursor(5, 15);
  oled.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);  //Placed in code for Troublshooting
  Serial.println();
}

void displayDate() {
  char currentDate [16];
  uint8_t thisDay, thisMonth ;
  thisDay = now.day();
  thisMonth = now.month();
  sprintf (currentDate, "%02d/%02d/", thisDay, thisMonth); //add leading zeros to the day and month

  oled.setTextSize(1);
  oled.setCursor(62, 15);
  oled.print(currentDate);

  oled.setTextSize(1);
  oled.setCursor(102, 15);
  oled.print(now.year(), DEC);
}

void displayTime() {
  char buffer [16];
  char AmPm[3];
  uint8_t thisSec, thisMin, thisHour;
  thisSec = now.second();
  thisMin = now.minute();
  thisHour = now.hour();

  if (thisHour > 12) {
    thisHour = thisHour - 12;
    strcpy(AmPm, "PM");
  } else strcpy(AmPm, "AM");

  sprintf (buffer, "%02d:%02d:%02d", thisHour, thisMin, thisSec);

  oled.setTextSize(2);
  oled.setCursor(6, 32);
  oled.print(buffer);

  oled.setTextSize(1);
  oled.setCursor(110, 32);
  oled.print(AmPm);
}

void displayTemperature() {
  oled.setTextSize(1);
  oled.setCursor(82, 55);
  oled.print(rtc.getTemperature());
  oled.display();
}

You can't use a print function on something that isn't a string.

char daysOfTheWeek[7][4]

Should fix it.

Obviously, you should limit the names to the initial three letters

@gdsmither, your topic has been moved to a more suitable location on the forum.

Thanks for using code tags in your first pist.

That worked. But I do not understand why it failed with the full names? Arn't they strings already?

What location was it moved too?

A three character string requires four bytes to store because you have to allow for the terminating zero.

OK that makes perfect sense.

Thanks it is always the little things that trip you up.

The compiler should have been giving you a warning about the string being too long to fit, but it likely scrolled off screen before you noticed.

Programming Questions. Installation and Troubleshooting is not for questions about your project :wink: See About the IDE 1.x category.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.