Max7219 clock NTP 12 hour format

Hello, I am using an ESP32 to control a max7129, everything works as expected, except for I am trying to display the time in a 12 hour format and I cannot figure it out

#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_H

#define USE_12HR_FORMAT true

#define MAX_DEVICES 6
#define CLK_PIN   18 
#define DATA_PIN  23 
#define CS_PIN    5 
MD_Parola Display = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

const char* ssid     = "rmphone";
const char* password = "qcin1085";

String Time, hour, minute, second;
String Formatted_date;
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("Connecting.");
  }
  Serial.println("");
  Serial.println("WiFi connected.");

  timeClient.begin();
  timeClient.setTimeOffset(-21600);
  
  Display.begin();
  Display.setIntensity(5);
  Display.displayClear();

}

void loop()
 {
   obtainTime();
}

void obtainTime() {
  while(!timeClient.update()) {
    timeClient.forceUpdate();
  }

  currentMillis = millis();

  if (currentMillis - previousMillis > interval)  {
previousMillis = millis();

Formatted_date = timeClient.getFormattedDate();
Serial.println(Formatted_date);

hour = Formatted_date.substring(11, 13);
minute = Formatted_date.substring(14, 16);
second = Formatted_date.substring(17, 19);


Time = hour + ":" + minute + ":" + second;
Serial.println(Time);
Display.setTextAlignment(PA_CENTER);
Display.print(Time);

}
}   

Are you saying you don't know how 12 hour time works?
What's the problem?

(This isn't an installation or troubleshooting issue, please move it to programming or ask a moderator to do it for you)

  1. Never publish your wifi credentials.
  2. I don't see any attempt to deal with 12 hour time in the sketch you posted.

Installation and Troubleshooting is for Problems with the Arduino itself NOT your project. It says so in the description of the section. Therefore I have moved your post here. Please be more careful where you post in future.

You may want to read this before you proceed:-
how to get the best out of this forum

Try this line after separating the hour/minute/second.

hour = hour.toInt()%12;

looking like this:

    hour = Formatted_date.substring(11, 13);
    minute = Formatted_date.substring(14, 16);
    second = Formatted_date.substring(17, 19);
    hour  = hour.toInt()%12;

I tested it here and it worked correctly.

Thank you very much, it works well.

Are "String"s going to work in this application? :thinking:

Neither 12am nor 12pm is represented as 0 in the 12 hour system.

Hi,
in order to avoid future problems, modify your sketch to look like this:


    hour = Formatted_date.substring(11, 13);
    minute = Formatted_date.substring(14, 16);
    second = Formatted_date.substring(17, 19);

    if ((hour.toInt() == 0) or (hour.toInt() == 12)) {        // Use 12 instead of 0
      hour = String(12);
    }
    else {
      hour  = String(hour.toInt() % 12);                // Use String
    }

    Time = hour + ":" + minute + ":" + second;

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