servo IR control help

Hello, the code that i have below lets my Arduino nano control a servo when the correct IR code is received by my IR receiver. I am using a seperate power supply for the servo with a common ground to the nano.
So this setup works, but not consistently. It is very weird how sometimes when i cut power or reset, it will work about 3 or 4 times in a row then stop working. When it stops working i can tell that the IR receiver is still receiving the signal from my remote because my Receiver is a small module that has a led that shows the power and when i transmit with my remote it flashes to let me know that it is receiving the IR signal.

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

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

const int RECV_PIN = 4;

IRrecv irrecv(RECV_PIN);
decode_results results;


void setup(){
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  
  irrecv.enableIRIn();
  

}


void loop(){
    if (irrecv.decode(&results)){

        switch(results.value){
          case 0x20DF40BF: 
     
 
         for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       //
        
    }
    irrecv.resume(); 
  }

}}

Could it be anything in the code that is causing the inconsistency?
Thanks

If you did over bluetooth you could do hand shaking where by you send "DONE" or whatever when the sketch has finished moving the servo and is ready to receive the next command from the sender.

Delta_G:
that's over 2 and a half seconds of ignoring your receiver. Could that be why it isn't responsive?

The second for loop there makes it over 5 seconds. Are you trying to send signals while it's moving?

it is not moving. could you expain more please?

Delta_G:
Oh, I thought you said it moves the first time and then stops receiving. The only way it works after the first time is if you wait until it is completely through moving before you send another command.

Delta_G:
Oh, I thought you said it moves the first time and then stops receiving. The only way it works after the first time is if you wait until it is completely through moving before you send another command.

I'm sorry for the confusion, when you asked if it was moving i thought that you meant is the whole setup moving (because i do plan on attaching it to an RC car)
but I am waiting for the servo to stop moving before i send the next signal.
Here is a video of it on youtube.
At first, it doesn't work, then i reset it and it works until i purposely press the wrong button then after continue to press the right button and nothing. Finally, even when its working and i am pressing the right button, it eventually stops working. Very inconsistent.

Delta_G:
Have you ever seen code written by a real coder? Notice how they always line up all the blocks nice and neat. They don't do that just to make it pretty. It helps you see where your blocks are.

Hit Control - T on your code and your issue will make itself apparent. What happens if it gets a code other than the one you have a case for? Would resume ever be called if you press the wrong button?

That makes sense, I didn't account for if the wrong button is pushed, but in the video, when i reset it i am only pressing the correct button and it works a couple of times then stops.

Delta_G:
Same problem. Same solution. What happens if the receiver gets it wrong once?
[/quote
ohhh, I get what you're saying. Even if it receives it wrong once it, or a repeat, or if the receiver didn't get it.
How would i even go about implementing code that would solve that?
It would have to say something like if the wrong code is received ignore.