Stuck in While loop when case is activated. Arduino I have an arduino uno that controls a light using an ir remote. The sketch works fine until I pressed button c which is case 0x10EF58A7. This case reads in values and dims the light accordingly but the only problem is i can not exit the case when another button on the ir remote is pressed! what should i do!?

#include &lt;SoftwareSerial.h&gt;

#include &lt;boarddefs.h&gt;
#include &lt;IRremote.h&gt;
#include &lt;IRremoteInt.h&gt;
#include &lt;ir_Lego_PF_BitStreamEncoder.h&gt;

#include &lt;TimerOne.h&gt;   

int RECV_PIN = 13;

IRrecv irrecv(RECV_PIN);
decode_results results;
volatile int i=0;               // Variable to use as a counter
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 7;   
int dim2 = 0;                   // led control
int dim = 128;                  // Dimming level (0-128)  0 = on, 128 = 0ff
int pas = 8;                   // step for count;

int freqStep = 65;
char incomingByte;

void setup() {                
  Serial.begin(9600);
  // initialize the digital pin as an output.
  pinMode(RECV_PIN, INPUT);  
  pinMode(AC_pin, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
  attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  pinMode(AC_pin, OUTPUT);
  Timer1.initialize(freqStep);                      // Initialize TimerOne library for the freq we need
  Timer1.attachInterrupt(dim_check, freqStep);      
  // Use the TimerOne Library to attach an interrupt
}

void zero_cross_detect() {    
  zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
  i=0;
  digitalWrite(AC_pin, LOW);
}                                 

// Turn on the TRIAC at the appropriate time
void dim_check() {                   
  if(zero_cross == true) {              
    if(i&gt;=dim) {                     
      digitalWrite(AC_pin, HIGH);  // turn on light       
      i=0;  // reset time step counter                         
      zero_cross=false;    // reset zero cross detection
    } 
    else {
      i++;  // increment time step counter                     
    }                                
  }    
}  

 void translateIR() // takes action based on IR code received 
{

  switch(results.value){

  case 0x10EFD827: 
  //power button 
   dim = 00;
    Serial.println(" full brightness "); 
    break;
  case 0x10EF20DF: 
  //circle button 
  dim = 128;
    Serial.println("off   "); 
    break;
  case 0x10EFF807: 
  // button A
  dim = 95;
    Serial.println(" dim level 1   "); 
    break;

   case 0x10EF7887: 
   //button B
   dim = 50; 
    Serial.println("dim level 2     "); 
    break;

 case 0x10EF58A7:
 // button C 
 {
  while ( Serial.available() &gt; 0) {
 unsigned int num = Serial.parseInt();
 num = map(num, 0, 255, 0, 128);
 unsigned int num1 = map(num, 0, 128, 128, 0);
// num1 = constrain( num1, 0,128);
 dim = num1 ;
   Serial.print(" auto   ");
   Serial.println(dim); 

   delay(800);
   Serial.flush();
    //break;
   }
  break;
}
 break;   

  case 0x10EF10EF:
//button left
    {
    if (dim&lt;127)  
   {
    dim = dim + pas;
    if (dim&gt;127) 
    {
     dim=128;
    }
    }
    }
    Serial.print(" dimming ammount=   " ); 
    Serial.println(dim);
    break;

  case 0x10EF807F:  

  //button right
   {
      {
  if (dim&gt;5)  
  {
     dim = dim - pas;
  if (dim&lt;0) 
    {
      dim=0;  // in vechiul sketch era 1
    }
   }
   }
   }
     Serial.print(" dimming ammount=   "); 
      Serial.println(dim);
    break;

   default: 
    //Serial.print(" unknown button   ");
    //Serial.println(results.value, HEX);
{}
  }

}

void loop(){
  int i=0;
   if (irrecv.decode(&amp;results)) {

   translateIR();
        irrecv.resume(); // Receive the next value

   } 
     delay(700);  
 }
The problem is that calling Serial.parseInt() does not remove data from the buffer. So, Serial.available() will not decrease in your case, but increase with every input you give.
You will need to call Serial.read() and then convert the received bytes to a number.

@Majenko generally you're right, but it's okay to use a while loop in this situation. If there is data in the buffer, this data has to be handled. And as the arduino is not capable of multitasking it will need to take the required time, no matter if in the main loop or in a subfunction.