Arduino won't start, serial print ???????...

Hello, I have strange problem with my sketch. There is no errors but if I uncomment that line (at the end of the sketch):

//    } else if (mode == "sa") {  //set alarm

Arduino won’t start and serial will print
setu???????????????????????
“?” is printed all the time

this is my code:

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

#define MAX_DEVICES 12
#define SPEED_TIME  75
#define PAUSE_TIME  0

#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10

#include <MD_DS3231.h>
#include <Wire.h>

MD_Parola P = MD_Parola(CS_PIN, MAX_DEVICES);

#define MAX_MESG  7

// Global variables
char curTime[MAX_MESG];    // mm:ss
bool tmActive = false;
bool tmAlarm = false;
int tmMin, tmSec;
int buzzerPin = 9;
int tmBeeps;

int toI(String s) {
  int v = 0;
  v = atoi(s.c_str());
  return(v);
}

String rn(int len) {
  String result;
  for (int i=1; i <= len; i++){
    result += (char) Serial.read();
    delay(5);
  }
  return result;
}

void wrTime() {
  RTC.yyyy = toI(rn(4));
  RTC.mm = toI(rn(2));
  RTC.dd = toI(rn(2));
  RTC.h = toI(rn(2));
  RTC.m = toI(rn(2));
  RTC.s = toI(rn(2));
  RTC.dow = toI(rn(1));
  RTC.writeTime();
}

void tm() {
  if (tmSec > 0) {
    tmSec--;
  } else if (tmSec == 0) {
    if (tmMin > 0) {
      tmSec = 59;
      tmMin--; 
    } else {
      if (tmBeeps%2 == 0) {
        tmAlarm = true;
      } else {
        tmAlarm = false;
        mute();
      }
      tmBeeps--;
      if (tmBeeps == 0) {
        tmActive = false;
        tmAlarm = false;
        mute();
      }
    }
  }
}

void alarm() {
  static uint32_t alStart = 0;
  if (millis() - alStart > 3000) {
    alStart = millis();
  }
  if (millis() - alStart < 10) {
    digitalWrite(buzzerPin, LOW);
  }
  if (millis() - alStart > 100) {
    mute();
  }
  if (millis() - alStart > 150) {
    alStart = millis();
  }
}

void mute() {
  digitalWrite(buzzerPin, HIGH);
}

void hMode() {
  int mode = toI(rn(1));
  if (mode == 0) {
    RTC.control(DS3231_12H, DS3231_OFF);
  } else {
    RTC.control(DS3231_12H, DS3231_ON);
  }
}

void setAlarm() {
  RTC.yyyy = RTC.mm = RTC.dd = 0;
  RTC.h = toI(rn(2));
  RTC.m = toI(rn(2));
  RTC.s = RTC.dow = 0;
  RTC.writeAlarm1(DS3231_ALM_HM);
}

void setup(void) {
  pinMode(buzzerPin, OUTPUT);
  digitalWrite(buzzerPin, HIGH);
  Serial.begin(9600);
  Serial.println("setup begin");
  
  P.begin(2);
  P.setZone(0, 0, 5);  //zegar dol
  P.setZone(1, 6, 11);  //zegar gora
  P.setFont(0, numericdown);
  P.setFont(1, numericup);

  P.displayZoneText(0, curTime, PA_RIGHT, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(1, curTime, PA_RIGHT, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  Serial.println("setup end");
}

void loop(void) {
  static uint32_t lastTime = 0; // millis() memory
  static bool flasher = false;  // seconds passing flasher
  static uint32_t lastTemp = 0; // millis() memory

  P.displayAnimate();
  if (P.getZoneStatus(0) && P.getZoneStatus(1)) {
    if (millis() - lastTime >= 1000) {
      lastTime = millis();
      if (tmActive == true) {
        sprintf(curTime, "%d%c%02d", tmMin, (flasher ? ':' : '#'), tmSec);
        tm();
      } else {
        RTC.readTime();
        sprintf(curTime, "%d%c%02d", RTC.h, (flasher ? ':' : '#'), RTC.m);
      }
      flasher = !flasher;
      P.setIntensity(0);
      P.displayReset();
      P.synchZoneStart();
    }
  }
  if (tmAlarm) {
    alarm();
  }
  
  String mode;
  while (Serial.available() > 0) {
    mode = rn(2);
    if (mode == "wt") {   //write time
      wrTime();
    } else if (mode == "ts") {   //timer start
      tmActive = true;
      tmMin = toI(rn(2));
      tmSec = toI(rn(2));
      tmBeeps = toI(rn(2)) * 2;
    } else if (mode == "tc") {   //timer cancel
      tmActive = false;
      mute();
    } else if (mode == "hm") {  //12h mode
      hMode();
//    } else if (mode == "sa") {  //set alarm
      setAlarm();
      tmBeeps = toI(rn(2)) * 2;
    }
  }
}

Sketch is using 84% of memory and variables 27%. Any ideas what is wrong with it?

My guess is you've stomped on some memory you don't own.

One thing I can suggest is to set the Warning level to "All" in Preferences. That sometimes provides warnings of 'iffy' code that might be causing your problem.

  while (Serial.available() > 0) {
    mode = rn(2);

How stupid!

Just because there is at lest 1 char in the buffer does not mean that there are as many as you want!

Mark

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R