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.