OSEPP 201 LED Serial Switch Issue *RESOLVED*

I just purchased the OSEPP 201 and the LED Serial Switch program is not functioning properly.

Instead of the light turning on and staying on it is flickering.

TLDR
Serial.available() gets a value of 2 when you put in a character making the switch run twice for each letter input without requiring additional input.
inByte always has a value of 10 on the second pass as well.

I created a function that clears Serial.available() out which only works when it is paired with three different Serial.println(Serial.available()).

Below is the code that comes with the kit.

/*
* Tutorial 3a: LED Serial Switch
* 
* Toggle LEDs over serial using the Switch case.
*
*
* To see this sketch in action open the serial monitor
* and send these characters to toggle the corresponding LED:
* - r = Red
* - y = Yellow
* - g = Green
* - w = white
* Send any other character to turn all four LEDs off.
*
* The circuit:
* - 4 LEDs attached to digital pins 2 through 5 with 330ohm resistors
*
* Author: Blaise Jarrett
*
*/

// The LEDs are connected to these pins
int LEDRedPin = 5;
int LEDYellowPin = 4;
int LEDGreenPin = 3;
int LEDWhitePin = 2;

void setup()
{
   // set up serial at 9600 baud
   Serial.begin(9600);

   // set all four LED pins to output mode
   pinMode(LEDRedPin, OUTPUT);
   pinMode(LEDYellowPin, OUTPUT);
   pinMode(LEDGreenPin, OUTPUT);
   pinMode(LEDWhitePin, OUTPUT);
}

void toggleLED(int LEDPin)
{
   // toggle the LED on the pin passed as an argument
   digitalWrite(LEDPin, !digitalRead(LEDPin));
}

void loop()
{
   if (Serial.available() > 0)
   {
       // read a single character over serial
       int inByte = Serial.read();

       // do something different for each character
       switch (inByte)
       {
           // if we receive r, y, g, or w
           // toggle the respective LED using our function
           case 'r':
               toggleLED(LEDRedPin);
               break;
           case 'y':
               toggleLED(LEDYellowPin);
               break;
           case 'g':
               toggleLED(LEDGreenPin);
               break;
           case 'w':
               toggleLED(LEDWhitePin);
               break;
           default:
               // if we receive any other character turn all
               // the LEDs off
               digitalWrite(LEDRedPin, LOW);
               digitalWrite(LEDYellowPin, LOW);
               digitalWrite(LEDGreenPin, LOW);
               digitalWrite(LEDWhitePin, LOW);
               break;
       }
   }
}

When trying to debug the issue, I found out that the Serial.available() gets a value of 2 when you put in a character making the switch run twice for each letter input. So it turns the corresponding light on then on the next loop around it is jumping to the default and turning the lights off.

I also noticed that the inByte always has a value of 10 on the second pass as well. I am not sure why 1.) the inByte is not being reset and why the Serial.available() is not resetting.

I tried to create a function that clears it out; however, that only works when it is paired with three different Serial.println(Serial.available()) Below is the code that works.

/*
* Tutorial 3a: LED Serial Switch
* 
* Toggle LEDs over serial using the Switch case.
*
*
* To see this sketch in action open the serial monitor
* and send these characters to toggle the corresponding LED:
* - r = Red
* - y = Yellow
* - g = Green
* - w = white
* Send any other character to turn all four LEDs off.
*
* The circuit:
* - 4 LEDs attached to digital pins 2 through 5 with 330ohm resistors
*
* Author: Blaise Jarrett
*
*/

// The LEDs are connected to these pins
int LEDRedPin = 5;
int LEDYellowPin = 4;
int LEDGreenPin = 3;
int LEDWhitePin = 2;

void setup()
{
   // set up serial at 9600 baud
   Serial.begin(9600);

   // set all four LED pins to output mode
   pinMode(LEDRedPin, OUTPUT);
   pinMode(LEDYellowPin, OUTPUT);
   pinMode(LEDGreenPin, OUTPUT);
   pinMode(LEDWhitePin, OUTPUT);
}

void toggleLED(int LEDPin)
{
   // toggle the LED on the pin passed as an argument
   digitalWrite(LEDPin, !digitalRead(LEDPin));
}

void serialFlush(){
 Serial.println(Serial.available());
 while(Serial.available() > 0) {
   char t = Serial.read();
   Serial.println(Serial.available());
 }
} 

void loop()
{

   Serial.println(Serial.available());
   if (Serial.available() > 0)
   {
       // read a single character over serial
       int inByte = Serial.read();
       serialFlush();

       // do something different for each character
       switch (inByte)
       {
           // if we receive r, y, g, or w
           // toggle the respective LED using our function
           case 'r':
               toggleLED(LEDRedPin);
               break;
           case 'y':
               toggleLED(LEDYellowPin);
               break;
           case 'g':
               toggleLED(LEDGreenPin);
               break;
           case 'w':
               toggleLED(LEDWhitePin);
               break;
           default:
               // if we receive any other character turn all
               // the LEDs off
               digitalWrite(LEDRedPin, LOW);
               digitalWrite(LEDYellowPin, LOW);
               digitalWrite(LEDGreenPin, LOW);
               digitalWrite(LEDWhitePin, LOW);
               break;
       }
   }
}

inByte always has a value of 10 on the second pass as well.

Change the Line ending setting of the Serial monitor from Newline to No line ending to prevent a newline character (10) being sent after the user entry

Change the Line ending setting of the Serial monitor from Newline to No line ending to prevent a newline character (10) being sent after the user entry

Thank you UKHeliBob! That fixed the issue.

It is embarrassing the amount of hours I have spent trying to figure this out.