While Loop help

#include <String.h>
#define RESET 0

byte buffer; 
char bufferString[20]; 
int index = 0;

void setup()
{
  Serial.begin(115200);
    pinMode(5, OUTPUT); 
    pinMode(6, OUTPUT);
}

void loop()
{
  // Read all available serial data
 if(Serial.available()>0) 
  {  memset(bufferString,0,index);
     index = RESET;
     while (Serial.available()>0 && index < 20 )
     {  
        delay(5);
        buffer = Serial.read();
        bufferString[index] = buffer; 
        index++; 
     } 
     bufferString[index] = '\0';
 
  // Start processing data
     if(strcmp(bufferString, "On")  == 0)
     {
		//  <<------------------------------------------------ Start
		do           
		{
			for (int brightness = 0; brightness < 255; brightness++) 
			{
		      analogWrite(5, brightness);
			  delay(2);
			} 
			for (int brightness = 255; brightness >= 0; brightness--) 
			{
			  analogWrite(5, brightness);
			  delay(2);
			} 
		}
		while(strcmp(bufferString, "On")  == 0);
		//  <<-------------------------------------------------- End
     } 
     else if(strcmp(bufferString, "Off")  == 0)
     {
		digitalWrite(5,LOW);
		analogWrite(5, 0);
     }
     else if(strcmp(bufferString, "Push")  == 0)
     {
		digitalWrite(6,HIGH);
     }
     else if(strcmp(bufferString, "Pull")  == 0)
     {
		digitalWrite(6,LOW);
     }
  }
}

I had problem for the code between the comment start/end region
After sending in a “On” command, Pin 5 will run according to the code but will never jump out from the while loop even i send in the “Off” command. The pin 5 LED keep running even i send the “Off” command. its seems like the program is ignoring and can’t make String comparison after jump into the while loop. After in the While loop, all my command was ignored. How to make this work? Please help.

http://gammon.com.au/serial

Hi Nick Gammon, Thanks for your time,

sorry, i can't figure it out. the link is for serial reading purposes? I understand that my method for reading serial byte was not appropriate. But my problem is the while loop. Please help. Thanks

You're not reading the serial port within your do...while loop, so you'll never read the string to terminate it.

JPgs: But my problem is the while loop. Please help. Thanks

It's not your only problem:

int index = 0;

...
   memset(bufferString,0,index);

This means you are setting 0 bytes to 0. What is the point of that?

Thanks for all,

You're not reading the serial port within your do...while loop, so you'll never read the string to terminate it.

ok ok... get it get it... any other methods that i can use to make it success without reading the serial port within the do...while loop?

Define “success”.
If you mean, “is there another way of inputting the string “on” without reading the serial port?”, then no.

JPgs: any other methods that i can use to make it success without reading the serial port within the do...while loop?

What was your problem with using my suggestion?

http://gammon.com.au/serial

hi Nick Gammon... your suggestion was very good... maybe I've made some confusion on my question...sorry... i will post a new question again when i think I'm ready... anyway... thanks for your time...