Why isn't my IRremote not resuming within switch cases?

This may be a stupid question but whenever i enter a switch case and resume the IRremote through irrecv.resume(), it does not detect the buttons i press. Here is my code. For the "CH" and "CH+" cases, despite resuming the irrecv.resume() i still cannot detect it within the case. Only once the case breaks, it starts detecting the buttons i press.

#include <IRremote.h>
#include <Stepper.h>
#include <Servo.h>
#include <elapsedMillis.h>

const float STEPS_PER_REV = 32;
const float STEPS_PER_REV_OUT = 2048;
const int RECV_PIN = 2;
const int BUTTON = 4;

int Steps = 1;
int buttonstate;
int angle = 0;
int changer = 1;

Servo servo;
Stepper steppermotor(STEPS_PER_REV,8,10,9,11);


IRrecv irrecv(RECV_PIN);
decode_results results;

elapsedMillis timeElapsed;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON,INPUT);
  servo.attach(3);
  irrecv.enableIRIn();
}

void loop() {
  if(irrecv.decode(&results)){
    switch(results.value){
        case 0xFFA25D: //CH- : HALF TURN SERVO
          Serial.print("CH-\nInitialising Servo Motor...\n");
           while(angle != 180){
              servo.write(angle);
              angle++;
              //delay(50);
              Serial.print(angle);
              Serial.print("\n");
           }
           Serial.print("Finished Servo Operation.\n");
           angle = 0;
         break;           
         case 0xFF629D: //CH : STEP MOTOR OPERATION
          Serial.print("CH\nInitialising Step Motor...\n");
          while(timeElapsed < 10000){
            buttonstate = digitalRead(BUTTON);
            if(buttonstate == HIGH ){ //if button pressed, reverse direction or + button pressed */
               Steps = Steps*(-1); 
               //delay(250);
            }
            irrecv.resume();
            if (irrecv.decode(&results)){
              Serial.print(results.value);
              Serial.print("\n");
              Serial.print("entered\n"); 
              if(results.value == 0xFFA857){
                Steps = Steps*(-1); 
                //delay(250);
              }               
            }
            if(Steps<0){
              Serial.print("CCW\n");  
            }    
            else{
              Serial.print("CW\n");  
            }        
            steppermotor.setSpeed(500);
            steppermotor.step(Steps);
          }
          timeElapsed = 0;
          Serial.print("Finished Step Operation.\n");  
          break;
        case 0xFFE21D: //CH+
          Serial.print("CH+\n");
          irrecv.resume();
          while(timeElapsed < 5000){
             if (irrecv.decode(&results)){
              Serial.print(results.value);
              Serial.print("\n");
              Serial.print("entered\n");             
             }
             irrecv.resume();
          }
          timeElapsed = 0;
          break;
        case 0xFF22DD: //|<<
          Serial.print("|<<\n");
          break;
        case 0xFF02FD: //>>|
          Serial.print(">>|\n");
          break;
        case 0xFFC23D: //>||
          Serial.print(">||\n");
          break;
        case 0xFFE01F: //-
          Serial.print("-\n");
          break;
        case 0xFFA857: //+
          Serial.print("+\n");
          break;
        case 0xFF906F: //EQ
          Serial.print("EQ\n");
          break;
        case 0xFF6897: //0
          Serial.print("0\n");
          break;
        case 0xFF9867: //100+
          Serial.print("100+\n");
          break;
        case 0xFFB04F: //200+
          Serial.print("200+\n");
          break;
        case 0xFF30CF: //1
          Serial.print("1\n");
          break;
        case 0xFF18E7: //2
          Serial.print("2\n");
          break;
        case 0xFF7A85: //3
          Serial.print("3\n");
          break;
        case 0xFF10EF: //4
          Serial.print("4\n");
          break;
        case 0xFF38C7: //5
          Serial.print("5\n");
          break;
        case 0xFF5AA5: //6
          Serial.print("6\n");
          break;
        case 0xFF42BD: //7
          Serial.print("7\n");
          break;
        case 0xFF4AB5: //8
          Serial.print("8\n");
          break;
        case 0xFF52AD: //9
          Serial.print("9\n");
          break;        
    }
    irrecv.resume();  
  }
}

IRRemote.ino (3.8 KB)

Please post your code in code tag so it looks like this:

//your code here

I did not look at your code but I suspect there is delay() in your code. To allow responsive program, you must get rid of delay() and use millis() for timming.

You have a loop

while(timeElapsed < 10000){

, and nothing inside that loop updates the timeElapsed variable. If timeElapsed is < 10000 when the loop starts, it will stay like that and the loop will never exit.

Oh, and there's more than one of 'em.

Please read "how to post in this forum". Your sketch is not long and you ought to have posted it in-line using
** **[code][/code]** **
tags.

I defined the timeElapsed variable as a elapsedMillis variable hence i don't think i need to update it. What else can it be. Also, thanks for your suggestion for using code tags, it looks so much nicer now.

You have delay() in your code. While delaying, the arduino will not do anything (i.e. read button state).

I commented out all the delays but to no avail. It still seems i cannot work any buttons on the IRremote whilst inside a case. Only when i leave the case, and press the button it prints out the button on the Serial monitor.

MrGadget:
I defined the timeElapsed variable as a elapsedMillis variable hence i don't think i need to update it.

Oh - you're right. I hadn't encountered that library before. Operator overloading, ugh.

Which leaves us with your inital problem. And I'm a bit confused as to what it is. You say that "whenever i enter a switch case and resume the IRremote through irrecv.resume(), it does not detect the buttons i press", which I take to mean "it never detects the buttons", but then you say "Only once the case breaks, it starts detecting the buttons i press". Are you trying to say that it never detects the first button press, but after that it works normally?

Or are you saying that it only ever detects the first button? I'm confused - you're saying that the 'not detecting buttons' happens whenever you irrecv.resume, and that only happens after you successfully catch a button press. But then you say once the case breaks, it does start detecting. But the case isn't going to 'break' until irrecv.decode detects a button.

IOW: if the easy fixes (you have delay problems) don't fix it, we have reached a point where you will need to explain the behaviour of your sketch more clearly.

Hi,

See: http://arduino-info.wikispaces.com/IR-RemoteControl

There are code examples there that use CASE and are known to work OK.. Maybe extract part of a working example..