Fan turning off when I only want to turn off the servo.

So here is my code:

#include <Servo.h>
#include <IRremote.h>
  
  int RECV_PIN = 11;
  int Fan = 5;
  int inc = 40;
  int servoPin = 9;
  int angle = 90;
  int change = 10;
  Servo servo;
  IRrecv irrecv(RECV_PIN);
  decode_results results;
  boolean FanOn = true;
  
  void setup()
  {
     pinMode(Fan,OUTPUT);
    Serial.begin(9600);
    irrecv.enableIRIn(); // Start the receiver
    analogWrite(Fan,0);
    servo.attach(servoPin);
     
  }
  
  void loop() {
    if (irrecv.decode(&results)) {
       if (results.value == 4105841032)
      {
        if (FanOn == true)
        {
          FanOn = false;
          Serial.println("Fan On");
          Serial.println("");
          analogWrite(Fan,inc);
          delay(50);
        }
        else{
          FanOn= true;
          Serial.println("Fan Off");
          Serial.println("");
          analogWrite(Fan,0);
          delay(50);
        }
  
      }
      else if (results.value == 1752382022 && FanOn==false){
        inc=inc+5;
        Serial.println("Fan Inc");
        analogWrite(Fan, inc);
        Serial.println(inc);
        Serial.println("");
      }
      else if (results.value == 2209452902 && FanOn==false){
        inc=inc-5;
        Serial.println("Fan Dec");
        analogWrite(Fan, inc);
        Serial.println(inc);
        Serial.println("");
      }
      else if (results.value == 3459683302)
      {
        // resets back to 0
        angle =90;
        Serial.println("reset");
      }
       else if (results.value == 1595074756)
      {
        angle = angle + change;
      }
      else if (results.value == 412973352) {
        
        angle = angle - change;
      }
      
      angle = constrain(angle, 0, 180); // limit value of angle 
      servo.write(angle);
      delay(100);
      irrecv.resume();
    }
 }

So I am using a Galaxy S4 which has an IR Blaster. Each one of those numbers after results.value represents a button, and when i press that button on my phone, it does what is in that if statement. The problem I am having is that when i press the button corresponding to "3459683302" ...it resets the servo back to zero, but it also turns off the fan. It disconnects the arduino from the computer but still supplies power. Ive tried switching the button corresponding to the reset action, and ive changed the pin but it still keeps doing that. I think it might have something to do with the code or constraint but I'm not sure. Any help is appreciated. I am using a HITEC HS-311 servo (3 pins) and Arduino Uno.

Oh and there are no errors

Are you making an electronic toilet?

Sounds like it's crashing.

One thing that I never liked is the use of delay. I new to Arduino but I'm very experienced with C and to just stop in the middle of a loop and spin without letting the CPU do anything else is asking for trouble. You won't log anything. If an interrupt is triggered it won't be handled. The computer won't be able to communicate with the Ardunio if it's sitting in delay(). I would do something like:

#define T1 100

uint32_t last_time = 0;

void loop()
{
uint32_t ctime = millis();

if (ctime > last_time + T1) {
last_time = ctime;

// this will never be called more frequently than T1 ms (unless ctime roll's over)
}

I bet if you just clean out all of those delay calls the whole thing will work better.

Hi,
How are you powering the servo? Servos can draw a lot of peak power when making a move , like back-to-zero..

If it's running on Arduino power, try running it on a separate 5V supply...

@terryking228 the power is being drawn from the arduino and what you said makes a greal deal of sense. I am about to switch to an external 9V supply for the servo and im pretty sure that she solve the problem. Ill post again once i have made that switch

So I have a spare 9V battery but the operating point for my servo is between 4.8 and 6 Volts. How do y ou suggest i moderate the voltage all the while it being connected to the arduino?

sahil_patel725:
So I have a spare 9V battery but the operating point for my servo is between 4.8 and 6 Volts. How do y ou suggest i moderate the voltage all the while it being connected to the arduino?

A 7805 regulator chip can reduce 9v to 5v. If the 9v battery is one of the small "smoke detector" types, the servo will probably kill it quickly. Use four 1.5v batteries (AA, AAA, etc.) in series instead for ~6v.