Printing formatted time to OLED board

I’m trying to display the current time on the OLED board attached to my Particle Photon.

void loop() {
  time_t time = Time.now();
  Time.format(time, '%Y-%m-%dT%H:%M:%S%z');
  displayString(time);
}

int displayString(String text) {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println(text);
  display.display();

  return 1;
}

I have confirmed that displayString works.

I’m getting the error Invalid conversion from int to const char*.

Hi @BenedictLewis

Try this instead:

void loop() {
  time_t time = Time.now();
  displayString(Time.format(time, '%Y-%m-%dT%H:%M:%S%z'));
}
1 Like

And if you are only displaying the current time you can even go with only

  displayString(Time.format("%Y-%m-%dT%H:%M:%S%z")); 

No need for the time_t time = Time.now(); as there is an overload for format() that only takes a string (or not even that for the default format, which can be set via Time.setFormat()) which works on the current time.

And you should use double quotes (") for strings. Single quotes (') are meant for single characters.


In your original code, you tried to pass a time_t variable as a String into your function, which can’t work.

3 Likes