Skip to content
Life is short Play more
  • AI技术
  • Computer
    • Cloud
    • DataBase
      • Mysql
      • Oracle
    • Operation system
      • Android
      • Linux
      • Windows
    • Programming
      • C , C++
      • Java
      • Javascript
      • Linux shell
      • Php
      • python
      • Web Programing
    • Security
    • Virtulization
    • Web hosting
      • Ad
  • Life Testing
  • My work
    • wrapEasyDevice
  • Network
    • HTTP
  • Note
  • Other
    • Embedded
    • Hardware
  • Share
    • Articles
    • Music
    • Tools
    • Video
  • Software Testing
    • Automation Testing
    • Cloud Testing
    • Functional Testing
    • Performance Testing
Life is short Play more
Life is short play more

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 针角。

此篇文章已被阅读5966 次

相关文章

  1. java的stack错误没有显示行号显示unknown source
  2. 记录arduino+ble调试成功(没有完整资料的日子真痛苦)
  3. android如何单元测试handler发消息
  4. 如何用java实现swap函数?
  5. adb logcat 如何针对应用包名进行过滤日志?

Related Posts

如何修改android 创建avd的路径
记录arduino+ble调试成功(没有完整资料的日子真痛苦)
cmake 找不到模块问题解决

About The Author

The Tester

技术交流,生活学习

© 2026 Life is short Play more | Home | Powered by WordPress & Customizable Blogily