IR Receiver works but then stops

I am trying to control a fan with a digital pin and a IR receiver and remote that came with my kit. When I press a button on the remote the fan turns on as expected. I programmed another button to turn it off. Now when I press this 2nd button the fan does not turn off. I read the output from the receiver and it shows that it no longer is receiving any input from my remote and it is also outputting a bunch of random inputs that aren't from my remote. Any suggestions on how to fix this? Here is my code:

#include <IRremote.h>

const int RECV_PIN = 5;
int pinnum = 9;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup(){
  pinMode(9, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn();
  irrecv.blink13(true);
}

void loop()  
{
  if (irrecv.decode(&results)) 
  {
     switch(results.value){
  
        case 0xFF30CF: 
          digitalWrite(pinnum, HIGH);   
          Serial.println("ON"); 
          break;
        case 0xFF18E7: 
          digitalWrite(pinnum, LOW);
          Serial.println("OFF");     
          break;
        default: 
          Serial.println(results.value);
          break;
      
    }
    irrecv.resume();
  }  
  delay(500);
  
  
  
}

Ignore below :frowning:

Instead of:

break;
      
    }
    irrecv.resume();
  }  
  delay(500);

Try:

break;
    irrecv.resume();  
    }
    
  }  
  delay(500);

Your code works here.

Show us your wiring.

  if (irrecv.decode(&results))
  {
    Serial.println(results.value, HEX);  // <----<<<<  Add this line to see what is received

    switch (results.value)
    {
        . . .

Here is my wiring. To explain: I have the 5v output connected to my positive rail and ground to the ground rail. I then have an analog signal (to turn the fan on and off) connected to my positive end of the fan motor and the negative end of the fan motor is plugged into the ground rail. the IR receiver has the ground wire going to the ground rail and the positive going to the 5v rail with a 100 ohm resister between cause i heard that will clear up any noise and it seems to do so. My yellow data cable is plugged into pin 11 from the IR receiver. I am fairly sure all is correct. It just seems that once my fan turns on, the receiver no longer takes in signals and receives and bunch of random inputs. I can press other buttons on the remote and it works as it should.

Full page print.pdf (1.05 MB)

You need a kickback diode across the motor.

Applying 9vdc to a Arduino pin will burn up your Arduino, never do this!
A PP3 9v battery is a poor motor power supply.

If the battery shown, is only being used as a weighted platform to hold the motor, an Arduino cannot power a motor directly!
You must use an external power supply and a transistor driver.
Use a transistor to drive the motor, the Arduino drives the transistor.

Looks like you are using A4 to try to control the fan.
The software shown uses D09.
You do realize that A4 is either an analog ‘input’ or a digital ‘input/output’.

The software uses D05 as the IR signal.

I have never used a 100Ω resistor in series with Vcc on a IR receiver.

OP's image:

As mentioned, your software works fine.

My mistake I forgot to mention I WAS using the Digital 9 pin but tried to use the analog when I had issues and thought maybe the analog pin would fix it. Yes, the battery is just a weight to hold the fan down. The arduino powers the dc motor fan just fine and it came with my kit so im not sure why you say it can't power it? For the input signal I swapped that too. I was just moving pins around to see maybe if i had faulty pins but that doesn't seem to be the issue. The IR receiver receives inputs just fine UNTIL I use it to turn on the fan. Once the fan turns on, I get random inputs that don't come from my remote and any signals that do indeed come from my remote don't get picked up once the fan has been turned on. It is as if when the fan is powered on that the IR receiver is picking up rogue signals that interfere with my remotes signals?

So it sounds like you have found out for yourself why it's a bad idea to run motors direct from the Arduino. They tend to inject interference into the power supply and thus mess up the Arduino program.

Try running the fan from a separate battery and see what that does.

Steve

I'll try that. Thanks!