arduino 红外遥控陀机
Posted On 2015年11月30日
红外控制陀机向右(+号键),向左(-号键)旋转。
长按和短按时,遥控器发出的指令不同, 所以每次判断按键时, 记录上次短按的键,如果是长按,则认为是上次短按的继续。
+号键 NEC: 16754775
-号键 NEC: 16769055
长按时均为 NEC: 4294967295
decode_type为红外接受到的resutls的decode_type , 可以忽略。是由红外发送时,此按键写死的type。
#include <IRremote.h>
#include <Servo.h>
const int RECV_PIN = 7; // the ir recieve pin
IRrecv irrecv(RECV_PIN);
decode_results results;
Servo myservo; //创建一个舵机控制对象
int lastbutton = 0;
void setup() {
// put your setup code here, to run once:
//pinMode(7,INPUT);
Serial.begin(9600);
if(!Serial.available())
{
Serial.println("hi coming recieing...");
}
irrecv.enableIRIn(); // Start the receiver
irrecv.blink13(true);
myservo.attach(2);
Serial.println(myservo.read());
}
void loop() {
// put your main code here, to run repeatedly:
if (irrecv.decode(&results)) {
if (results.decode_type == NEC) {
Serial.print("NEC: ");
if(results.value == 16754775)
{
lastbutton = 1;
right();
}
else if(results.value == 16769055)
{
lastbutton = 2;
left();
}
else if(results.value == 4294967295)
{
if(lastbutton ==1) right();
else if(lastbutton ==2 ) left();
}
} else if (results.decode_type == SONY) {
Serial.print("SONY: ");
} else if (results.decode_type == RC5) {
Serial.print("RC5: ");
} else if (results.decode_type == RC6) {
Serial.print("RC6: ");
} else if (results.decode_type == UNKNOWN) {
Serial.print("UNKNOWN: ");
}
//Serial.println(results.value, HEX);
Serial.println(results.value);
irrecv.resume(); // Receive the next value
}
}
void right()
{
int pos = myservo.read();
Serial.println(pos);
if(pos < 174)
myservo.write(pos+5);
delay(15);
}
void left()
{
int pos = myservo.read();
Serial.println(pos);
if(pos > 5)
myservo.write(pos-5);
delay(50);
}
红色为电源, 棕色为地线, 黄色为控制线。 这里链接了arduino的2号pin 针角。


此篇文章已被阅读4955 次
About The Author
The Tester
技术交流,生活学习