Why does my if keep triggering? I want my else!

Hi Guys, I have been around the barn on the this one and getting dizzy. Any insight would be appreciated.
I only want the if to trigger when the user simply hits enter (I.e. no other text) at the "Enter :" prompt. To my dismay, the if always triggers even if I enter text. My else feels so lonely. Uhg! I 'think' something is wrong with the way I'm trying to capture when the user simply hits enter "\r\n" at the "Enter : " prompt. I'm tried matching for '\r' and '\n', etc. In the serial monitor I've tried all four settings: no line ending, new line, carriage return, Both NL & CR. I'm just struggling.

String pedestalName;

void setup() {
  Serial.begin(9600);

  Serial.println("Enter : ");
  while (Serial.available() == 0) {
  }
  while (Serial.available() > 0) {
    pedestalName = Serial.readString(); // if string entered, else should trigger
    Serial.print("pedestalName = ");    // print out what is entered
    Serial.println(pedestalName);
    if (pedestalName = "\r\n") {          // user simply hits enter on keyboard
      char randomName[10];
      itoa(millis(), randomName, 16);   // https://fresh2refresh.com/c-programming/c-type-casting/c-itoa-function/
      Serial.print("randomName = ");
      Serial.println(randomName);
      pedestalName = String(randomName);

    }
    else {
      Serial.print("pedestalName = ");
      Serial.println(pedestalName);
    }
  }
}

void loop() {

}

if (pedestalName == "\r\n") {

OMG! I'm such an idiot! :blush: Thank you very much.

Or?
if (!pedestalName) { // user simply hits enter on keyboard

I like the concept but, when I try it and simply it enter, it drops to my else.

I would like to retract my
if (!pedestalName) {
and submit a modification of LarryD, removing the "\r"
if (pedestalName == "\n" {

I found both of our previous answers did not print the random number.

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