How to control servo motor using switch case statement

I want to control the servo motor using switch case statement but i stuck. The servo something working fine when i enter the alphabet but sometimes its cannot detect when i enter the value. Please help me. i post my code here for review


#include <Servo.h>

Servo myservo;

//char myCol[20];
int pos = 0;

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  myservo.write(pos);
  Serial.println("Press E for easy level mode");
  Serial.println("Press N for normal level mode");
  Serial.println("Press H for hard level mode");

}

void loop() {
  if (Serial.available())
    Serial.readBytes(myCol, 1);
  //char level = Serial.readBytes(myCol, 1);
  //char level = Serial.read();
  int input = Serial.read();

  switch (input)
  {
    case 'E':    // easy level
      //(strcmp(myCol, "E") == 0);
      //pos = 30;
      for (pos = 0; pos <= 30; pos += 10){
        myservo.write(pos);}
      for (pos = 30; pos >= 0; pos -= 10){
        myservo.write(pos);
      break;

    case 'N':     // normal level
      //(strcmp(myCol, "N") == 0);
      //pos = 60;
      for (pos = 0; pos <= 60; pos += 10){
        myservo.write(pos);}
     for (pos = 60; pos >= 0; pos -= 10){
        myservo.write(pos);
      break;

    case 'H':     // hard level
      //(strcmp(myCol, "H") == 0);
      // pos = 90;
      for (pos = 0; pos <= 90; pos += 10){
        myservo.write(pos);}
      for (pos = 90; pos <= 0; pos -= 10){
        myservo.write(pos);
      break;
  }
     }
      }
  }
  }

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

You may wish to review your closing braces

Hi, @doreamon11
If you press [ CTRL ] [ T ] , auto format, to align your brackets, you will find the switch.. case format does not line up.
Here is your code formatted.

#include <Servo.h>

Servo myservo;

//char myCol[20];
int pos = 0;

void setup()
{
  Serial.begin(9600);
  myservo.attach(9);
  myservo.write(pos);
  Serial.println("Press E for easy level mode");
  Serial.println("Press N for normal level mode");
  Serial.println("Press H for hard level mode");
}

void loop()
{
  if (Serial.available())
    Serial.readBytes(myCol, 1);
  //char level = Serial.readBytes(myCol, 1);
  //char level = Serial.read();
  int input = Serial.read();
  switch (input)
  {
    case 'E':    // easy level
      //(strcmp(myCol, "E") == 0);
      //pos = 30;
      for (pos = 0; pos <= 30; pos += 10)
      {
        myservo.write(pos);
      }
      for (pos = 30; pos >= 0; pos -= 10)
      {
        myservo.write(pos);
        break;
      case 'N':     // normal level
        //(strcmp(myCol, "N") == 0);
        //pos = 60;
        for (pos = 0; pos <= 60; pos += 10)
        {
          myservo.write(pos);
        }
        for (pos = 60; pos >= 0; pos -= 10)
        {
          myservo.write(pos);
          break;
        case 'H':     // hard level
          //(strcmp(myCol, "H") == 0);
          // pos = 90;
          for (pos = 0; pos <= 90; pos += 10)
          {
            myservo.write(pos);
          }
          for (pos = 90; pos <= 0; pos -= 10)
          {
            myservo.write(pos);
            break;
          }
        }
      }
  }
}

Tom... :grinning: :+1: :coffee: :australia:

code is missing closing braces in several locations

code has commented out definition for myCol

there's a conditional check if serial data is available, but regardless of that check, the input is read and processed

another approach is simply to skip further processing is NO serial data is available

  if (! Serial.available())
      return;

  int input = Serial.read();