how to save incoming serial monitor strings into variables

I'm working on a project where I'm displaying time (with the time library without external parts) on an I2C display. I want to save the most recent line from the serial monitor into a String for later use.

Here is the time library's print code:

Serial.print(hourFormat12());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  if (isPM() == 1) {
    Serial.print("PM");
  } else {
    Serial.print("AM");
  }
  Serial.print(" ");
  Serial.print(month());
  Serial.print("/");
  Serial.print(weekday());
  Serial.println();

I've tried using Serial.readString() but that for some reason makes it output once every 2 seconds:
11:10:22 to 11:10:24

Serial.readString() waits up to 1000 msec (the default timeout value) before returning so it is busy waiting and then when it returns, time has elapsed so you code appears to jump. Try testing for any characters available and then reading them in one by one instead. If you encounter the last character which is usually a newline, then process the input.

It's advisable to forget that String (capital S) exists. Heavy use of it will result in holes in your memory and as a result hard to find issues at run time.

Read Robin's Serial Input Basics thread for a solid approach to serial communication; it uses so-called c-strings instead of String.

sterretje:
Read Robin's Serial Input Basics thread for a solid approach to serial communication; it uses so-called c-strings instead of String.

I'm not looking for Serial input but rather at the stream produced by the time library every second from the Serial monitor. I want to save the most recent line to a variable. So far the examples I've seen have pertained to Serial input.

For some reason, I've dealt with a bunch of problems when trying to save the values within the program. There aren't any error messages, but it isn't giving out any values when I print it. I figured it would be easier to save the time values from the Serial monitor instead.

You figured wrong :wink:

Post your attempt of how you tried to save the values within the program.That way we can point out what you did wrong and suggest corrections.

Of you want a copy from serial monitor, you have to literally copy the received text, paste it in the 'send box' and click send.

Thanks for the help so far.

Here is a working example:

#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include <TimeLib.h>
#define TIME_HEADER  "T"   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

char* realval;
String displayvalue;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

void setup()  {
  Serial.begin(9600);
  u8g2.begin();
  Serial.println("Waiting for sync message");
}

void serialclock() {
  if (Serial.available()) {
    processSyncMessage();
  }
  if (timeStatus() != timeNotSet) {
    digitalClockDisplay();
  }
  if (timeStatus() == timeSet) {
    digitalWrite(13, HIGH); // LED on if synced
  } else {
    digitalWrite(13, LOW);  // LED off if needs refresh
  }
  delay(1000);
}

void digitalClockDisplay() {
  // digital clock display of the time
  Serial.print(hourFormat12());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(weekday());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(isPM()); //0 if true
  Serial.println();
  if (minute() < 10 && second() < 10) {
    displayvalue = String(hourFormat12()) + ":0" + String(minute()) + ":0" + String(second());
  } else if (minute() < 10 && second() >= 10) {
    displayvalue = String(hourFormat12()) + ":0" + String(minute()) + ":" + String(second());
  } else if (minute() >= 10 && second() < 10) {
    displayvalue = String(hourFormat12()) + ":" + String(minute()) + ":0" + String(second());
  } else {
    displayvalue = String(hourFormat12()) + ":" + String(minute()) + ":" + String(second());
  }
  realval = displayvalue.c_str();
}

void printDigits(int digits) {
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


void processSyncMessage() {
  unsigned long pctime;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

  if (Serial.find(TIME_HEADER)) {
    pctime = Serial.parseInt();
    if ( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
      setTime(pctime); // Sync Arduino clock to the time received on the serial port
    }
  }
}

time_t requestSync()
{
  Serial.write(TIME_REQUEST);
  return 0; // the time will be sent later in response to serial mesg
}

void loop() {
  serialclock();
  Serial.println(realval);
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_7x14_tf);
    u8g2.drawStr(5, 10, "Additional Text");
    u8g2.drawStr(5, 62, realval);
  } while ( u8g2.nextPage() );

}

However, if I add more lines to be printed, separate from the time variable (realval), it returns nothing.

void loop() {
  serialclock();
  Serial.println(realval);
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_7x14_tf);
    u8g2.drawStr(5, 10, "foo");
    u8g2.drawStr(5, 25, "foo1");
    u8g2.drawStr(5, 40, "foo2");
    u8g2.drawStr(5, 62, realval);
  } while ( u8g2.nextPage() );
}