[Arduino Serial Monitor User Input]

Hi there :slight_smile: I am using an Arduino Uno R3, a common cathode RGB LED and 3 seperate LEDs.

I want to create a user interface where a user can select different options using the Serial Monitor Send function as an input. When the user enters a valid code, the user will be authorized and he or she will be able to select multiple options in my user interface.

My code reads the user input and returns a menu.

The problem which I am currently facing is as follows. I have one large menu and it is
inconvenient to use for every function. Is it possible to make a sub menu for every function?

Example of what I mean:

if (Serial.available()) {
  char userInput = Serial.read();

  if (userInput == 'M' || userInput == 'm') {
    int musicOptions = Serial.read();
    Serial.println("Music option");
    if (musicOptions == '1' ) {
      Serial.println("Play the first song.");
    }
    if (musicOptions == '2' ) {
      Serial.println("Play the 2th song.");
    }
  }

  else if (userInput == 'S' || userInput == 's') {
    int sensorOptions = Serial.read();
    Serial.println("Sensor option");
    if (sensorOptions == '1' ) {
      Serial.println("Light Sensor");
    }
    if (sensorOptions == '2' ) {
      Serial.println("Temprature Sensor");
    }
  }
}

My code:

const int redLED = 9;
const int greenLED = 11;
const int blueLED = 10;

const int redLED2 = A0;
const int yellowLED = A1;
const int greenLED2 = A2;
int LED;

int menuCount = 0;

int count;
int enterCount = -1;
int enteredCode[] = {0, 0, 0, 0};
const int secretCode[] = {4, 2, 2, 1};
bool access = false;

void setup() {

  Serial.begin(9600);
  while (! Serial);

  pinMode(redLED2, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED2, OUTPUT);

  digitalWrite(redLED2, HIGH);
  digitalWrite(yellowLED, LOW);
  digitalWrite(greenLED2, LOW);

  welcomeMessage();
}

void welcomeMessage() {
  Serial.println("---------------------------------");
  Serial.println("Welcome please enter a valid PIN:");
  Serial.println("---------------------------------");
}

void blinkLED() {
  for (count = 0; count < 3; count++) {
    digitalWrite(LED, LOW);
    delay(900);
    digitalWrite(LED, HIGH);
    delay(900);
  }
  if (LED == greenLED2) {
    digitalWrite(redLED2, LOW);
    digitalWrite(yellowLED, LOW);
  }

  else if (LED == redLED2) {
    digitalWrite(yellowLED, LOW);
  }

  else if (LED == yellowLED) {
    digitalWrite(redLED2, LOW);
  }
}

void codeCheck() {
  if (Serial.available()) {
    int codeInput = Serial.read();

    if (codeInput == '1') {
      enterCount++;
      enteredCode[enterCount] = 1;
      Serial.print(enteredCode[enterCount]);
    }

    else if (codeInput == '2') {
      enterCount++;
      enteredCode[enterCount] = 2;
      Serial.print(enteredCode[enterCount]);
    }

    else if (codeInput == '3') {
      enterCount++;
      enteredCode[enterCount] = 3;
      Serial.print(enteredCode[enterCount]);
    }

    else if (codeInput == '4') {
      enterCount++;
      enteredCode[enterCount] = 4;
      Serial.print(enteredCode[enterCount]);
    }

    int rightAnswer = 0;
    if (enterCount == 3) {
      Serial.println("");
      Serial.println("Checking you PIN.");
      LED = yellowLED;
      blinkLED();
      for (count = -1; count < 5; count++) {
        if (enteredCode[count] == secretCode[count]) {
          rightAnswer++;

          if (rightAnswer < 4 && count == 3) {
            Serial.println("Access denied.");
            LED = redLED2;
            blinkLED();
            Serial.println("Resetting");
            for (count = 0; count < 5; count++) {
              enteredCode[count] = 0;
            }
            enterCount = -1;
            rightAnswer = 0;
            welcomeMessage();
          }

          else if (rightAnswer == 4) {
            Serial.println("Access is granted.");
            LED = greenLED2;
            blinkLED();
            access = true;
            rightAnswer++;
            enterCount++;
          }
        }
      }
    }
  }
}

void choiceMenu() {
  if (menuCount == 0) {
    Serial.println("-----------------------");
    Serial.println("Please enter an option:");
    Serial.println("[R] = Red");
    Serial.println("[G] = Green");
    Serial.println("[B] = Blue");
    Serial.println("[W] = Rainbow Wave");
    Serial.println("[N] = Turn off LEDs");
    Serial.println("[L] = Log out.");
    Serial.println("-----------------------");
  }
}

void setColourValue(int redVal, int greenVal, int blueVal) {
  analogWrite(redLED, redVal);
  analogWrite(greenLED, greenVal);
  analogWrite(blueLED, blueVal);
}

void loop() {

  codeCheck(); //authorisation check
  
  if (access == true) {
    choiceMenu();
    while (menuCount < 2) {
      menuCount++;
    }

    if (Serial.available()) {
      char userInput = Serial.read();

      if (userInput == 'R' || userInput == 'r') {
        setColourValue(255, 0, 0);
        Serial.println("Turning on the red LED.");
      }

      if (userInput == 'G' || userInput == 'g') {
        setColourValue(0, 255, 0);
        Serial.println("Turning on the green LED.");
      }

      if (userInput == 'B' || userInput == 'b') {
        setColourValue(0, 0, 255);
        Serial.println("Turning on the blue LED.");
      }

      if (userInput == 'W' || userInput == 'w') {
        Serial.println("Activating the rainbow wave mode.");
        setColourValue(255, 0, 0); // red
        delay(1500);
        setColourValue(250, 250, 0); // orange
        delay(1500);
        setColourValue(130, 200 , 0); // yellow
        delay(1500);
        setColourValue(0, 255, 0); // green
        delay(1500);
        setColourValue(0, 0, 255); // blue
        delay(1500);
        setColourValue(255, 0 , 150); // indigo
        delay(1500);
        setColourValue(170, 0 , 255); // violet
        delay(1500);
        setColourValue(0, 0 , 0); // Off
      }

      if (userInput == 'N' ||  userInput == 'n') {
        setColourValue(0, 0, 0);
        Serial.println("Turning off the LEDs.");
      }

      if (userInput == 'L' ||  userInput == 'l') {
        setColourValue(0, 0, 0);
        Serial.println("Logging out.");
      }
    }
  }
}

I appreciate your aid ;3.

You should read Serial Input Basics

There is an example of a simple user interface in Planning and Implementing a Program

...R