Matrix to display time

Hi, I did a project to display the current time (via wifi with en esp8266-eps01 module) on a 4 8x8 led matrix driven by max7219. I got the time displayed on the matrix, but at the end, I have a strange symbol, I don't know where this symbol comes from and how I can get rid of it. Does somebody have an idea how to solve this?

The matrix is controlled by the Arduino which receives the time from the module via serial communication.

Esp module code:

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

#include <NTPClient.h>               // Include NTPClient library
#include <TimeLib.h>                 // Include Arduino time library

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

const char *ssid     = "TP-Link";
const char *password = "s1zfzjdqyv";

WiFiUDP ntpUDP;

// 'time.nist.gov' is used (default server) with +1 hour offset (3600 seconds) 60 seconds (60000 milliseconds) update interval
NTPClient timeClient(ntpUDP, "time.nist.gov", 3600, 60000);

char Time[] = "00:00";
//char Date[] = "DATE:00/00/2000";
byte last_second, second_, minute_, hour_;

void setup() {

  Serial.begin(115200);

  WiFi.begin(ssid, password);

  //Serial.print("Connecting.");
  while ( WiFi.status() != WL_CONNECTED ) {
    delay(500);
    //Serial.print(".");
  }
  //Serial.println("connected");

  timeClient.begin();
}

void loop() {

  timeClient.update();
  unsigned long unix_epoch = timeClient.getEpochTime();    // Get Unix epoch time from the NTP server

  second_ = second(unix_epoch);
  if (last_second != second_) {

    minute_ = minute(unix_epoch);
    hour_   = hour(unix_epoch);

    Time[4]  = minute_ % 10 + 48;
    Time[3]  = minute_ / 10 + 48;
    Time[1]  = hour_   % 10 + 48;
    Time[0]  = hour_   / 10 + 48;

    // Send time and date to serial monitor
    String s = String(Time);
    Serial.println(s);
    
    last_second = second_;
  }
}

Code for the Arduino:

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW

#define MAX_DEVICES 4
#define CS_PIN 10

MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

String Time;

void setup() {

  Serial.begin(115200);

  myDisplay.begin();
  myDisplay.setIntensity(0);
  myDisplay.displayClear();
}

void loop() {

  while (Serial.available()) {

    Time = Serial.readStringUntil('\n');// read the incoming data as string

    Serial.println(Time);
    myDisplay.setTextAlignment(PA_CENTER);
    myDisplay.print(Time);
  }
}

The output on the matrix:
Knipsel
The problem is that P looking symbol at the end.

Thank you in advance!!

how is your Serial monitor set up? (what do you send as end of line?)

try

void loop() {

  if (Serial.available()) {
    Time = Serial.readStringUntil('\n');// read the incoming data as string
    Time.strip(); // get rid of extra spaces 
    Serial.println(Time);
    myDisplay.setTextAlignment(PA_CENTER);
    myDisplay.print(Time);
  }
}

The P symbol looks like the character for a linefeed. does 'readStringUntil('\n')' also return the end of line characters?

Thanks for the suggestion I will try that out!! Do I need to add Serial.print('\n'); after Serial.println(s); or something as well in the code for the esp module or will adding the line you suggested to the code for the Arduino be sufficient?

only the time like 23:30 is displayed in the serial monitor. So I'm not sure if readStringUntil('\n') also return the end of line characters. But because it displays well in the serial monitor I'm a bit confused why it doesn't show it correct on the matrix

You should display the time on the serial monitor followed by a character like '|' or something else you recognise.

Serial.print(the time)
Serial.print("|");

That way if there else a newline in the string you will see the second '|' on a new line instead of the same as the time.

Alternatively you can display strlen(the_time) to see if the character count matches what you expect from visual inspection.

The serial stuff is only for debugging I suppose, so it does not matter much
Use one println()
In the monitor you don’t see the invisible characters like CR or LF

If they are there - trim() will clean that up

I programmed it with an '/n', but now only that symbol shows on the matrix

ok, so when you get the time string back you need to replace the last character in the string with the nul ('\0') character to terminate the string at that character.

time_string[strlen(time_string)-1] = '\0';

its leftover from centering without erasing existing text. Left or right align or pad with a blank.

If you use Strings then trim() does the necessary cleanup.

In general You don’t want to mess with the underlying representation like adding a null char somewhere as you don’t know (of course reading the code helps - but it’s not part of the spec) how the encapsulation works in terms of memory management. You could use methods to replace or truncate like subString etc

Thanks everyone!! I got it working by adding trim() and remove() to remove everything from starting at character 5.

Good point, I had missed this.

@ninanas98 FYI The Parola library required a c-string (nul terminated char array) not a String as the text to display, and this string needs to remain in scope for the duration of the animation, as the library does not copy the data into a private variable. I think .print() works because it is an extension of the Arduino .print() but if/when you call the class methods directly you will find it will break.

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