hi, i want to control a servo motor by using a IR remote.
here is my code:
#include <IRremote.h>
#include <Servo.h>
const int irReceiverPin = 30;
IRrecv irrecv(irReceiverPin);
decode_results result;
const int ledPin = 13;
Servo myservo;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(ledPin, OUTPUT);
myservo.attach(10);
}
void showIRProtocol(decode_results *result)
{
Serial.print("Protocol: ");
switch(result->decode_type) {
case NEC:
Serial.print("NEC");
break;
case SONY:
Serial.print("SONY");
break;
case RC5:
Serial.print("RC5");
break;
case RC6:
Serial.print("RC6");
break;
default:
Serial.print("Unknown encoding");
}
Serial.print(", irCode: ");
Serial.print(result->value, HEX);
Serial.print(", bits: ");
Serial.println(result->bits);
}
void loop()
{
myservo.write(value);
if (irrecv.decode(&result)) {
showIRProtocol(&result);
irrecv.resume();
}
if(result.value == 0x490){
digitalWrite(ledPin, HIGH);
}
if(result.value == 0xC90){
myservo.write(130);
}
}
from the serial monitor, i can get the same IR code and protocol name every time pressing the same button.
for example : Protocol : $ony, IR code : AAAA
however, once i add this myservo.attach(10);
, the protocol become unknown and i cannot get the same IR code.
It becomes: Protocol: Unknown, IR code: FDSAS65
Protocol: Unknown, IR code: QWEW1451
and i delete the above code, the IRcode and protocol will back to normal.
pls help!!